guides/java/crypto/BC25519.java
public static int generateKeyPair(String pubkey_filename, String secret_filename, boolean truncate){
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

		//Create public and private keys
		try{
			KeyPairGenerator generator = KeyPairGenerator.getInstance("Ed25519","BC");

			//initialize SPRNG
			SecureRandom rand = new SecureRandom();
			generator.initialize(256, rand);

			KeyPair pair = generator.generateKeyPair();
			Key pubkey = pair.getPublic();
			Key secret = pair.getPrivate();

			System.out.printf("Encoding public key : %s\n",pubkey.getFormat()); //optional printouts
			Writer w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pubkey_filename),"utf-8"));
			w.write("-----BEGIN PUBLIC KEY-----\n"); //PEM format
			w.write(Base64.getEncoder().encodeToString(pubkey.getEncoded()));
			w.write("\n-----END PUBLIC KEY-----\n");
			w.close();

			/*
			 * ToraNova chia_jason96@live.com
			 * There are incompatibilities as there are additional 33bytes written to the private stream
			 * which I don't udnerstand serve what purpose
			 * to change back, edit truncatedKey -> originalKey on the write
			 */
			byte[] originalKey = secret.getEncoded();
			byte[] truncatedKey = new byte[ originalKey.length - 35 ]; //remove the last 33
			truncatedKey[0] = 0x30; //PLEASE DO NOT EDIT THIS!
			truncatedKey[1] = 0x2E; //PLEASE DO NOT EDIT THIS! 2E corresponds to 46 bytes which is the size including asn1 headers
			for(int i = 2;i < truncatedKey.length;i++){
				truncatedKey[i] = originalKey[i];
			}
			System.out.printf("Truncated keylength now %d from %d\n",truncatedKey.length,originalKey.length); //optional printouts

			System.out.printf("Encoding secret key : %s\n",secret.getFormat()); //optional printouts
			w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(secret_filename),"utf-8"));
			w.write("-----BEGIN PRIVATE KEY-----\n"); //PEM format
			if(truncate){
				w.write(Base64.getEncoder().encodeToString( truncatedKey )); //comment this out to use back the originalkey
			}else{
				w.write(Base64.getEncoder().encodeToString( originalKey ));
			}
			w.write("\n-----END PRIVATE KEY-----\n");
			w.close();

		}catch(Exception e){
			//exception has occurred
			System.out.println(e);
			return 1;
		}
		return 0;
	}
/*
	 * Keypair generation function. generate a 256bit Ed25519 keypair and output them to file
	 * set truncate to true if we wanna interface with openSSL
	 */
Keypair generation function. generate a 256bit Ed25519 keypair and output them to file
set truncate to true if we wanna interface with openSSL
Keypair generation function . generate a 256bit Ed25519 keypair and output them to file set truncate to true if we wanna interface with openSSL
public static int generateKeyPair(String pubkey_filename, String secret_filename, boolean truncate){
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		try{
			KeyPairGenerator generator = KeyPairGenerator.getInstance("Ed25519","BC");
			SecureRandom rand = new SecureRandom();
			generator.initialize(256, rand);
			KeyPair pair = generator.generateKeyPair();
			Key pubkey = pair.getPublic();
			Key secret = pair.getPrivate();
			System.out.printf("Encoding public key : %s\n",pubkey.getFormat()); 
			Writer w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pubkey_filename),"utf-8"));
			w.write("-----BEGIN PUBLIC KEY-----\n"); 
			w.write(Base64.getEncoder().encodeToString(pubkey.getEncoded()));
			w.write("\n-----END PUBLIC KEY-----\n");
			w.close();
			/*
			 * ToraNova chia_jason96@live.com
			 * There are incompatibilities as there are additional 33bytes written to the private stream
			 * which I don't udnerstand serve what purpose
			 * to change back, edit truncatedKey -> originalKey on the write
			 */
			byte[] originalKey = secret.getEncoded();
			byte[] truncatedKey = new byte[ originalKey.length - 35 ]; 
			truncatedKey[0] = 0x30; 
			truncatedKey[1] = 0x2E;  2E corresponds to 46 bytes which is the size including asn1 headers
			for(int i = 2;i < truncatedKey.length;i++){
				truncatedKey[i] = originalKey[i];
			}
			System.out.printf("Truncated keylength now %d from %d\n",truncatedKey.length,originalKey.length); 
			System.out.printf("Encoding secret key : %s\n",secret.getFormat()); 
			w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(secret_filename),"utf-8"));
			w.write("-----BEGIN PRIVATE KEY-----\n"); 
			if(truncate){
				w.write(Base64.getEncoder().encodeToString( truncatedKey )); 
			}else{
				w.write(Base64.getEncoder().encodeToString( originalKey ));
			}
			w.write("\n-----END PRIVATE KEY-----\n");
			w.close();
		}catch(Exception e){
			System.out.println(e);
			return 1;
		}
		return 0;
	}
public static int generateKeyPair ( String pubkey_filename , String secret_filename , boolean truncate ) { Security . addProvider ( new org . bouncycastle . jce . provider . BouncyCastleProvider ( ) ) ; try { KeyPairGenerator generator = KeyPairGenerator . getInstance ( "Ed25519" , "BC" ) ; SecureRandom rand = new SecureRandom ( ) ; generator . initialize ( 256 , rand ) ; KeyPair pair = generator . generateKeyPair ( ) ; Key pubkey = pair . getPublic ( ) ; Key secret = pair . getPrivate ( ) ; System . out . printf ( "Encoding public key : %s\n" , pubkey . getFormat ( ) ) ; Writer w = new BufferedWriter ( new OutputStreamWriter ( new FileOutputStream ( pubkey_filename ) , "utf-8" ) ) ; w . write ( "-----BEGIN PUBLIC KEY-----\n" ) ; w . write ( Base64 . getEncoder ( ) . encodeToString ( pubkey . getEncoded ( ) ) ) ; w . write ( "\n-----END PUBLIC KEY-----\n" ) ; w . close ( ) ; /*
			 * ToraNova chia_jason96@live.com
			 * There are incompatibilities as there are additional 33bytes written to the private stream
			 * which I don't udnerstand serve what purpose
			 * to change back, edit truncatedKey -> originalKey on the write
			 */ byte [ ] originalKey = secret . getEncoded ( ) ; byte [ ] truncatedKey = new byte [ originalKey . length - 35 ] ; truncatedKey [ 0 ] = 0x30 ; truncatedKey [ 1 ] = 0x2E ; for ( int i = 2 ; i < truncatedKey . length ; i ++ ) { truncatedKey [ i ] = originalKey [ i ] ; } System . out . printf ( "Truncated keylength now %d from %d\n" , truncatedKey . length , originalKey . length ) ; System . out . printf ( "Encoding secret key : %s\n" , secret . getFormat ( ) ) ; w = new BufferedWriter ( new OutputStreamWriter ( new FileOutputStream ( secret_filename ) , "utf-8" ) ) ; w . write ( "-----BEGIN PRIVATE KEY-----\n" ) ; if ( truncate ) { w . write ( Base64 . getEncoder ( ) . encodeToString ( truncatedKey ) ) ; } else { w . write ( Base64 . getEncoder ( ) . encodeToString ( originalKey ) ) ; } w . write ( "\n-----END PRIVATE KEY-----\n" ) ; w . close ( ) ; } catch ( Exception e ) { System . out . println ( e ) ; return 1 ; } return 0 ; }
Keypair generation function.
Keypair generation function .
//Create public and private keys //initialize SPRNG //optional printouts //PEM format //remove the last 33 //PLEASE DO NOT EDIT THIS! //PLEASE DO NOT EDIT THIS! 2E corresponds to 46 bytes which is the size including asn1 headers //optional printouts //optional printouts //PEM format //comment this out to use back the originalkey //exception has occurred