Help! trying to get private key from a der file but Exception: Missing required salt

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Help! trying to get private key from a der file but Exception: Missing required salt

Narayana, Mnvvs

Hi I am trying to get private key from a der file using bouncy castle but getting an exception.

Exception in thread "main" Missing required salt

       at org.bouncycastle.jcajce.provider.ProvPBEPBKDF1$FixedPBKDF1.engineGenerateSecret(Unknown Source)

       at javax.crypto.SecretKeyFactory.generateSecret(

       at test.ImportEncryptedPrivateKey.main(


    public static byte[] readPK8FromFile(String fileName) throws IOException


        File f = new File(fileName);

        DataInputStream dis = new DataInputStream(new FileInputStream(f));

        byte[] theData = new byte[(int) f.length()];


        return theData;



    public static void main(String[] args) throws IOException,

            NoSuchAlgorithmException, NoSuchPaddingException,

            InvalidKeySpecException, InvalidKeyException,



       Security.addProvider(new BouncyCastleFipsProvider());

        byte[] encryptedPKInfo = readPK8FromFile("C:\\Users\\NARMN01\\Downloads\\ca.der");

        EncryptedPrivateKeyInfo ePKInfo = new EncryptedPrivateKeyInfo(


        char[] password = { 'f', 'i', 'r', 'e', 'w', 'a', 'l', 'l' };

        Cipher cipher = Cipher.getInstance(ePKInfo.getAlgName());

        PBEKeySpec pbeKeySpec = new PBEKeySpec(password);

        // Now create the Key from the PBEKeySpec

        SecretKeyFactory skFac = SecretKeyFactory.getInstance(ePKInfo


        Key pbeKey = skFac.generateSecret(pbeKeySpec);

        // Extract the iteration count and the salt

        AlgorithmParameters algParams = ePKInfo.getAlgParameters();

        cipher.init(Cipher.DECRYPT_MODE, pbeKey, algParams);

        // Decrypt the encryped private key into a PKCS8EncodedKeySpec

        KeySpec pkcs8KeySpec = ePKInfo.getKeySpec(cipher);

        // Now retrieve the RSA Public and private keys by using an

        // RSA keyfactory.

        KeyFactory rsaKeyFac = KeyFactory.getInstance("RSA");

        // First get the private key

        RSAPrivateCrtKey rsaPriv = (RSAPrivateCrtKey) rsaKeyFac.generatePrivate(pkcs8KeySpec);

        // Now derive the RSA public key from the private key

        RSAPublicKeySpec rsaPubKeySpec = new RSAPublicKeySpec(rsaPriv.getModulus(), rsaPriv.getPublicExponent());

        RSAPublicKey rsaPubKey = (RSAPublicKey) rsaKeyFac.generatePublic(rsaPubKeySpec);