Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/PKCS12Cipher.java
41161 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4893959 638320026* @summary basic test for PBEWithSHA1AndDESede, PBEWithSHA1AndRC2_40/12827* and PBEWithSHA1AndRC4_40/12828* @author Valerie Peng29* @key randomness30*/3132import java.io.*;33import java.util.*;34import java.security.*;35import javax.crypto.*;36import javax.crypto.spec.*;37import javax.crypto.interfaces.PBEKey;3839public class PKCS12Cipher {4041private static void runTest(String alg, byte[] plaintext,42char[] password, Provider p)43throws Exception {44Cipher cipher = Cipher.getInstance(alg, p);45PBEKeySpec pbeKeySpec = new PBEKeySpec(password);46SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);47AlgorithmParameters pbeParams = null;48SecretKey key = keyFac.generateSecret(pbeKeySpec);49cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);50byte[] enc1 = cipher.doFinal(plaintext);51byte[] enc2 = cipher.doFinal(plaintext);52if (Arrays.equals(enc1, enc2) == false) {53throw new Exception("Re-encryption test failed");54}55pbeParams = cipher.getParameters();56cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);57byte[] dec = cipher.doFinal(enc1);58if (Arrays.equals(plaintext, dec) == false) {59throw new Exception("decryption test for " + alg + " failed");60}6162PBEParameterSpec spec = (PBEParameterSpec)63pbeParams.getParameterSpec(PBEParameterSpec.class);64PBEKey key2 = new65MyPBEKey(password, spec.getSalt(), spec.getIterationCount());66cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);67byte[] dec2 = cipher.doFinal(enc1);68if (Arrays.equals(dec2, dec) == false) {69throw new Exception("Re-decryption test#1 failed");70}7172cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);73byte[] dec3 = cipher.doFinal(enc1);74if (Arrays.equals(dec3, dec) == false) {75throw new Exception("Re-decryption test#2 failed");76}7778System.out.println("passed: " + alg);79}8081public static void main(String[] argv) throws Exception {82byte[] input = new byte[1024];83new SecureRandom().nextBytes(input);84char[] PASSWD = { 'p','a','s','s','w','o','r','d' };85long start = System.currentTimeMillis();86Provider p = Security.getProvider("SunJCE");87System.out.println("Testing provider " + p.getName() + "...");88runTest("PBEWithSHA1AndDESede", input, PASSWD, p);89runTest("PBEWithSHA1AndRC2_40", input, PASSWD, p);90runTest("PBEWithSHA1AndRC2_128", input, PASSWD, p);91runTest("PBEWithSHA1AndRC4_40", input, PASSWD, p);92runTest("PBEWithSHA1AndRC4_128", input, PASSWD, p);93System.out.println("All tests passed");94long stop = System.currentTimeMillis();95System.out.println("Done (" + (stop - start) + " ms).");96}97}9899class MyPBEKey implements PBEKey {100char[] passwd;101byte[] salt;102int iCount;103MyPBEKey(char[] passwd, byte[] salt, int iCount) {104this.passwd = passwd;105this.salt = salt;106this.iCount = iCount;107}108public char[] getPassword() { return passwd.clone(); }109public byte[] getSalt() { return salt; }110public int getIterationCount() { return iCount; }111public String getAlgorithm() { return "PBE"; }112public String getFormat() { return "RAW"; }113public byte[] getEncoded() { return null; }114}115116117