Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with decryption of privateKey, NoSuchAlgorithmException.

843811Oct 30 2009 — edited Apr 17 2010
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 15 2010
Added on Oct 30 2009
6 comments
801 views