0N/A/*
2362N/A * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4893959
0N/A * @summary basic test for PBEWithSHA1AndDESede and
0N/A * PBEWithSHA1AndRC2_40
0N/A * @author Valerie Peng
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.security.*;
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/Aimport javax.crypto.interfaces.PBEKey;
0N/A
0N/Apublic class PKCS12Cipher {
0N/A
0N/A private static void runTest(String alg, byte[] plaintext,
0N/A char[] password, Provider p)
0N/A throws Exception {
0N/A Cipher cipher = Cipher.getInstance(alg, p);
0N/A PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
0N/A SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
0N/A AlgorithmParameters pbeParams = null;
0N/A SecretKey key = keyFac.generateSecret(pbeKeySpec);
0N/A cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
0N/A byte[] enc1 = cipher.doFinal(plaintext);
0N/A byte[] enc2 = cipher.doFinal(plaintext);
0N/A if (Arrays.equals(enc1, enc2) == false) {
0N/A throw new Exception("Re-encryption test failed");
0N/A }
0N/A pbeParams = cipher.getParameters();
0N/A cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
0N/A byte[] dec = cipher.doFinal(enc1);
0N/A if (Arrays.equals(plaintext, dec) == false) {
0N/A throw new Exception("decryption test for " + alg + " failed");
0N/A }
0N/A
0N/A PBEParameterSpec spec = (PBEParameterSpec)
0N/A pbeParams.getParameterSpec(PBEParameterSpec.class);
0N/A PBEKey key2 = new
0N/A MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
0N/A cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
0N/A byte[] dec2 = cipher.doFinal(enc1);
0N/A if (Arrays.equals(dec2, dec) == false) {
0N/A throw new Exception("Re-decryption test#1 failed");
0N/A }
0N/A
0N/A cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
0N/A byte[] dec3 = cipher.doFinal(enc1);
0N/A if (Arrays.equals(dec3, dec) == false) {
0N/A throw new Exception("Re-decryption test#2 failed");
0N/A }
0N/A
0N/A System.out.println("passed: " + alg);
0N/A }
0N/A
0N/A public static void main(String[] argv) throws Exception {
0N/A byte[] input = new byte[1024];
0N/A new SecureRandom().nextBytes(input);
0N/A char[] PASSWD = { 'p','a','s','s','w','o','r','d' };
0N/A long start = System.currentTimeMillis();
0N/A Provider p = Security.getProvider("SunJCE");
0N/A System.out.println("Testing provider " + p.getName() + "...");
0N/A runTest("PBEWithSHA1AndDESede", input, PASSWD, p);
0N/A runTest("PBEWithSHA1AndRC2_40", input, PASSWD, p);
0N/A System.out.println("All tests passed");
0N/A long stop = System.currentTimeMillis();
0N/A System.out.println("Done (" + (stop - start) + " ms).");
0N/A }
0N/A}
0N/A
0N/Aclass MyPBEKey implements PBEKey {
0N/A char[] passwd;
0N/A byte[] salt;
0N/A int iCount;
0N/A MyPBEKey(char[] passwd, byte[] salt, int iCount) {
0N/A this.passwd = passwd;
0N/A this.salt = salt;
0N/A this.iCount = iCount;
0N/A }
0N/A public char[] getPassword() { return passwd; }
0N/A public byte[] getSalt() { return salt; }
0N/A public int getIterationCount() { return iCount; }
0N/A public String getAlgorithm() { return "PBE"; }
0N/A public String getFormat() { return "RAW"; }
0N/A public byte[] getEncoded() { return null; }
0N/A}