Java Bouncycastle Generate Rsa Key Pair
Join GitHub today
Java Bouncy Castle Generate Rsa Key Pair Chart
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Java generating all key value pairs from list.
Sign upBranch:master
The KeyPairGenerator class is used to generate pairs of public and private keys. Key pair generators are constructed using the getInstance factory methods (static methods that return instances of a given class). A Key pair generator for a particular algorithm creates a public/private key pair that can be used with this algorithm. But I need getEncoded method available in Java which is not available in BC library in C#. This getEncoded method is used to convert given key into X.509 encoded key.In case of Java, public key getting twice converted (getencoded and getBytes),I.
0 contributors
packageme.txedo.security; |
importjava.io.FileNotFoundException; |
importjava.io.IOException; |
importjava.security.Key; |
importjava.security.KeyPair; |
importjava.security.KeyPairGenerator; |
importjava.security.NoSuchAlgorithmException; |
importjava.security.NoSuchProviderException; |
importjava.security.Security; |
importjava.security.interfaces.RSAPrivateKey; |
importjava.security.interfaces.RSAPublicKey; |
importorg.apache.log4j.Logger; |
importorg.bouncycastle.jce.provider.BouncyCastleProvider; |
publicclassMain { |
protectedfinalstaticLoggerLOGGER=Logger.getLogger(Main.class); |
publicstaticfinalintKEY_SIZE=1024; |
publicstaticvoidmain(String[] args) throwsFileNotFoundException, IOException, NoSuchAlgorithmException, NoSuchProviderException { |
Security.addProvider(newBouncyCastleProvider()); |
LOGGER.info('BouncyCastle provider added.'); |
KeyPair keyPair = generateRSAKeyPair(); |
RSAPrivateKey priv = (RSAPrivateKey) keyPair.getPrivate(); |
RSAPublicKey pub = (RSAPublicKey) keyPair.getPublic(); |
writePemFile(priv, 'RSA PRIVATE KEY', 'id_rsa'); |
writePemFile(pub, 'RSA PUBLIC KEY', 'id_rsa.pub'); |
} |
privatestaticKeyPairgenerateRSAKeyPair() throwsNoSuchAlgorithmException, NoSuchProviderException { |
KeyPairGenerator generator =KeyPairGenerator.getInstance('RSA', 'BC'); |
generator.initialize(KEY_SIZE); |
KeyPair keyPair = generator.generateKeyPair(); |
LOGGER.info('RSA key pair generated.'); |
return keyPair; |
} |
privatestaticvoidwritePemFile(Keykey, Stringdescription, Stringfilename) |
throwsFileNotFoundException, IOException { |
PemFile pemFile =newPemFile(key, description); |
pemFile.write(filename); |
LOGGER.info(String.format('%s successfully writen in file %s.', description, filename)); |
} |
} |
Java Bouncycastle Generate Rsa Key Pair Using Openssl
Copy lines Copy permalink