I'm writing a program that uses RSA for various tasks.
I know how to generate and write the key pair to file, but I cannot load the encrypted (AES-256-CFB) key pair to a KeyPair object.So the question is: how do I load/decrypt an encrypted PEM key pair as a java.security.KeyPair object using the BouncyCastle library?Thanks.Generation/export code:\[code\]public void generateKeyPair(int keysize, File publicKeyFile, File privateKeyFile, String passphrase) throws FileNotFoundException, IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException { SecureRandom random = new SecureRandom(); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC"); generator.initialize(keysize, random); KeyPair pair = generator.generateKeyPair(); Key pubKey = pair.getPublic(); PEMWriter pubWriter = new PEMWriter(new FileWriter(publicKeyFile)); pubWriter.writeObject(pubKey); pubWriter.close(); PEMWriter privWriter = new PEMWriter(new FileWriter(privateKeyFile)); if (passphrase == null) { privWriter.writeObject(pair); } else { PEMEncryptor penc = (new JcePEMEncryptorBuilder("AES-256-CFB")) .build(passphrase.toCharArray()); privWriter.writeObject(pair, penc); } privWriter.close();}\[/code\]
I know how to generate and write the key pair to file, but I cannot load the encrypted (AES-256-CFB) key pair to a KeyPair object.So the question is: how do I load/decrypt an encrypted PEM key pair as a java.security.KeyPair object using the BouncyCastle library?Thanks.Generation/export code:\[code\]public void generateKeyPair(int keysize, File publicKeyFile, File privateKeyFile, String passphrase) throws FileNotFoundException, IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException { SecureRandom random = new SecureRandom(); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC"); generator.initialize(keysize, random); KeyPair pair = generator.generateKeyPair(); Key pubKey = pair.getPublic(); PEMWriter pubWriter = new PEMWriter(new FileWriter(publicKeyFile)); pubWriter.writeObject(pubKey); pubWriter.close(); PEMWriter privWriter = new PEMWriter(new FileWriter(privateKeyFile)); if (passphrase == null) { privWriter.writeObject(pair); } else { PEMEncryptor penc = (new JcePEMEncryptorBuilder("AES-256-CFB")) .build(passphrase.toCharArray()); privWriter.writeObject(pair, penc); } privWriter.close();}\[/code\]