RE: Problem saving/reading using EncryptedPrivateKeyInfo to file

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

RE: Problem saving/reading using EncryptedPrivateKeyInfo to file

Ty Legette

I have looked though the archives and found similar question but nothing to solve my problem of being able to write out and read in an encrypted private key.

Since I’m new to crypto I’m sure it’s something I’m doing wrong like padding, etc.

Below is the code from the EncryptedPrivateKeyInfoTest regession test modified to read and write to/from a file.

 

Can someone tell me why I’m getting the exception….

Cannot retrieve the PKCS8EncodedKeySpec

      at javax.crypto.EncryptedPrivateKeyInfo.getKeySpec(DashoA12275)

      at security.jce.EncryptedPrivateKeyInfoTest.perform(EncryptedPrivateKeyInfoTest.java:113)

      at security.jce.EncryptedPrivateKeyInfoTest.main(EncryptedPrivateKeyInfoTest.java:173)

Caused by: javax.crypto.IllegalBlockSizeException: last block incomplete in decryption

      at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:612)

      at javax.crypto.Cipher.doFinal(DashoA12275)

 

        try

        {

            KeyPairGenerator fact = KeyPairGenerator.getInstance("RSA", "BC");

            fact.initialize(512, new SecureRandom());

            KeyPair keyPair = fact.generateKeyPair();

            PrivateKey  priKey = keyPair.getPrivate();

            PublicKey   pubKey = keyPair.getPublic();

            byte[]              salt = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            int                 iterationCount = 100;

            PBEParameterSpec    defParams = new PBEParameterSpec(salt, iterationCount);

            AlgorithmParameters params = AlgorithmParameters.getInstance(alg, "BC");

            params.init(defParams);

            char[]  password1 = { 'h', 'e', 'l', 'l', 'o' };

            PBEKeySpec          pbeSpec = new PBEKeySpec(password1);

            SecretKeyFactory    keyFact = SecretKeyFactory.getInstance(alg, "BC");

            Cipher cipher = Cipher.getInstance(alg, "BC");

            cipher.init(Cipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), params);

            byte[] wrappedKey = cipher.wrap(priKey);

            EncryptedPrivateKeyInfo pInfo = new EncryptedPrivateKeyInfo(params, wrappedKey);           

//----- my attempt to save and reload from a file ---------

            //write to file

            FileOutputStream sigfos = new FileOutputStream("f:\\test1");           

            sigfos.write(pInfo.getEncoded());

            sigfos.close();

            //read from file

            File f = new File("f:\\test1") ;

            int sizecontent = ((int) f.length());

            byte[] data = new byte[sizecontent]; 

              FileInputStream freader = new FileInputStream(f);

              freader.read(data, 0, sizecontent) ;

              freader.close();             

            pInfo = new EncryptedPrivateKeyInfo(params, data) ;

//-----------------------------------------------------------------------------------------------------------

            char[]  password2 = { 'h', 'e', 'l', 'l', 'o' };

            pbeSpec = new PBEKeySpec(password2);

            cipher = Cipher.getInstance(pInfo.getAlgName(), "BC");

            cipher.init(Cipher.DECRYPT_MODE, keyFact.generateSecret(pbeSpec), pInfo.getAlgParameters());

            PKCS8EncodedKeySpec keySpec = pInfo.getKeySpec(cipher);     *****  fails here ******

            if (!MessageDigest.isEqual(priKey.getEncoded(), keySpec.getEncoded()))            {

                return new SimpleTestResult(false, "Private key does not match");

            }

            pbeSpec = new PBEKeySpec(password1);

            keyFact = SecretKeyFactory.getInstance(alg, "BC");

            cipher = Cipher.getInstance(alg, "BC");

            cipher.init(Cipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), params);

            wrappedKey = cipher.wrap(priKey);

            pInfo = new EncryptedPrivateKeyInfo(cipher.getParameters(), wrappedKey);

            pbeSpec = new PBEKeySpec(password2);

            cipher = Cipher.getInstance(pInfo.getAlgName(), "BC");

            cipher.init(Cipher.DECRYPT_MODE, keyFact.generateSecret(pbeSpec), pInfo.getAlgParameters());

            keySpec = pInfo.getKeySpec(cipher);

            if (!MessageDigest.isEqual(priKey.getEncoded(), keySpec.getEncoded()))

            {

                return new SimpleTestResult(false, "Private key does not match");

            }           

            return new SimpleTestResult(true, getName() + ": Okay");

        }

        catch (Exception e)

        {

            e.printStackTrace();

            return new SimpleTestResult(false, getName() + ": exception - " + e.toString(), e);

        }

    }

 

 

-Ty

Loading...