Hi experts,
What I'm trying to do is loading a DER private key from a file, searching the forum I found this code for loading and decrypting a PrivateKey.
private static KeyPair obtenerLlavesFormatoDER(byte[] encryptedPKInfo, char[] password){
KeyPair parDeLlaves = null;
try {
java.security.Security.addProvider(new BouncyCastleProvider());
EncryptedPrivateKeyInfo ePKInfo = new EncryptedPrivateKeyInfo(
encryptedPKInfo);
Cipher cipher = Cipher.getInstance(ePKInfo.getAlgName(),"BC");
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
SecretKeyFactory skFac = SecretKeyFactory.getInstance(ePKInfo
.getAlgName(),"BC");
cipher.init(Cipher.DECRYPT_MODE,
skFac.generateSecret(pbeKeySpec), ePKInfo.getAlgParameters());
PKCS8EncodedKeySpec pkcs8Spec = ePKInfo.getKeySpec(cipher);
KeyFactory keyFact = KeyFactory.getInstance("RSA","BC");
RSAPrivateCrtKey privKey = (RSAPrivateCrtKey)keyFact.generatePrivate(pkcs8Spec);
RSAPublicKeySpec rsaPubKeySpec = new RSAPublicKeySpec(privKey.getModulus(), privKey.getPublicExponent());
RSAPublicKey rsaPubKey = (RSAPublicKey) keyFact.generatePublic(rsaPubKeySpec);
parDeLlaves = new KeyPair(rsaPubKey, privKey);
} catch (IOException ex) {
ex.printStackTrace();
}catch(NoSuchAlgorithmException ex){
ex.printStackTrace();
}catch(NoSuchPaddingException ex){
ex.printStackTrace();
}catch(NoSuchProviderException ex){
ex.printStackTrace();
}catch(InvalidAlgorithmParameterException ex){
ex.printStackTrace();
}catch(InvalidKeyException ex){
ex.printStackTrace();
}finally{
return parDeLlaves;
}
}
The exeption is launched in the line
Cipher cipher = Cipher.getInstance(ePKInfo.getAlgName(),"BC");
It's a
java.security.NoSuchAlgorithmException: No such algorithm: 1.2.840.113549.1.5.13
Can you please give me some advise over what can I do to successfully load the key ?? I'm using BouncyCastle as the JCE provider
Thanks in advance