javax.crypto.interfaces.PBEKey Java Examples

The following examples show how to use javax.crypto.interfaces.PBEKey. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PKCS12Cipher.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #2
Source File: PKCS12Cipher.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #3
Source File: PKCS12Cipher.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #4
Source File: PKCS12Cipher.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #5
Source File: PKCS12Cipher.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #6
Source File: PKCS12Cipher.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #7
Source File: PKCS12Cipher.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #8
Source File: CryptoUtil.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static SecretKey createPBEKey(byte[] salt, String password) throws NoSuchAlgorithmException, InvalidKeySpecException {
	if (password == null || password.length() == 0) {
		throw new IllegalArgumentException(L10nUtil.getMessage(MessageCodes.PBE_PASSWORD_ZERO_LENGTH_ERROR, DefaultMessages.PBE_PASSWORD_ZERO_LENGTH_ERROR));
	}
	SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_KEY_ALGORITHM);
	PBEKey pbeKey = (PBEKey) factory.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_ITERATIONS, SYMMETRIC_KEY_LENGTH));
	return new SecretKeySpec(pbeKey.getEncoded(), SYMMETRIC_ALGORITHM);
}
 
Example #9
Source File: PKCS12Cipher.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #10
Source File: PKCS12Cipher.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #11
Source File: PKCS12Cipher.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #12
Source File: PKCS12Cipher.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #13
Source File: PKCS12Cipher.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
Example #14
Source File: PKCS12Cipher.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}