Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8226824

NoSuchAlgorithmException: unrecognized algorithm name: PBEWithHmacSHA256AndAES_128

XMLWordPrintable

      javax.crypto.EncryptedPrivateKeyInfo does not recognize PBEWithHmacSHA256AndAES_128 and PBEWithHmacSHA256AndAES_256 although the documentation @
      https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#AlgorithmParameters sees to indicate it should. This error is also observed with JDK 11.

      import javax.crypto.*;
      import java.security.KeyPairGenerator;
      import java.security.KeyPair;
      import java.security.PrivateKey;
      import java.security.*;
      import javax.crypto.spec.*;

      public class Test {
        public static void main(String[] args) throws Exception {
              byte[] salt = new byte[]{0, 17, 34, 51, 68, 85, 102, 119};
              char[] pwd = new char[] {'p','a','s','s','w','o','r','d'};

              //AlgorithmParameters ap =
      AlgorithmParameters.getInstance("PBEWithHmacSHA256AndAES_256");
              AlgorithmParameters ap =
      AlgorithmParameters.getInstance("PBEWithHmacSHA256AndAES_128");
              PBEParameterSpec spec = new PBEParameterSpec(salt, 5);
              ap.init(spec);

              KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
              keyPairGenerator.initialize(2048);
              KeyPair keyPair = keyPairGenerator.generateKeyPair();
              PrivateKey privateKey = keyPair.getPrivate();

              //Cipher ciph = Cipher.getInstance("PBEWithHmacSHA256AndAES_256");
              Cipher ciph = Cipher.getInstance("PBEWithHmacSHA256AndAES_128");
              SecretKey sk = SecretKeyFactory.getInstance(
                      ap.getAlgorithm()).generateSecret(new PBEKeySpec(pwd, spec.getSalt(), spec.getIterationCount()));
              ciph.init(Cipher.ENCRYPT_MODE, sk, spec);
              byte[] result = ciph.doFinal(privateKey.getEncoded());

              EncryptedPrivateKeyInfo epk = new EncryptedPrivateKeyInfo(ap, result);
        }
      }

            ascarpino Anthony Scarpino
            shadowbug Shadow Bug
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: