Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/PBES2Test.java
41161 views
/*1* Copyright (c) 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 638320026* @summary PBE: need new algorithm support in password based encryption27*/28import java.security.*;29import java.util.Arrays;30import javax.crypto.*;31import javax.crypto.spec.*;3233public class PBES2Test {3435private static final String[] algos = {36"PBEWithHmacSHA1AndAES_128",37"PBEWithHmacSHA224AndAES_128",38"PBEWithHmacSHA256AndAES_128",39"PBEWithHmacSHA384AndAES_128",40"PBEWithHmacSHA512AndAES_128"41};42private static final byte[] ivBytes = {430x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,440x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,45};4647public static final void main(String[] args) throws Exception {48for (String algo : algos) {49test(algo, true); // salt, ic, IV supplied by the application50test(algo, false); // salt, ic, IV generated by the implementation51}52}5354private static final void test(String algo, boolean suppliedParams)55throws Exception {5657System.out.println("***********************************************");58System.out.println(algo +59(suppliedParams ? " [algorithm parameters are supplied]\n"60: " [algorithm parameters are generated]\n"));61int iterationCount = 1000;62byte[] salt = new byte[]{ 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 };6364// Create PBE key65PBEKeySpec pbeKeySpec = new PBEKeySpec("mypassword".toCharArray());66SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algo);67SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);68byte[] pbeKeyBytes = pbeKey.getEncoded();69System.out.println(" key[" + pbeKeyBytes.length + "]: " +70String.format("0x%0" + (pbeKeyBytes.length * 2) + "x",71new java.math.BigInteger(1, pbeKeyBytes)));7273// Create PBE cipher74System.out.println("Encrypting...");75Cipher pbeCipher = Cipher.getInstance(algo);76if (suppliedParams) {77pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey,78new PBEParameterSpec(salt, iterationCount,79new IvParameterSpec(ivBytes)));80} else {81pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey);82}8384// Encrypt85byte[] cleartext = "This is just an example".getBytes();86System.out.println(" text[" + cleartext.length + "]: " +87String.format("0x%0" + (cleartext.length * 2) + "x",88new java.math.BigInteger(1, cleartext)));8990byte[] ciphertext = pbeCipher.doFinal(cleartext);91System.out.println("c'text[" + ciphertext.length + "]: " +92String.format("0x%0" + (ciphertext.length * 2) + "x",93new java.math.BigInteger(1, ciphertext)));9495AlgorithmParameters aps = pbeCipher.getParameters();9697byte[] iv;98if (suppliedParams) {99iv = ivBytes;100} else {101PBEParameterSpec pbeSpec =102aps.getParameterSpec(PBEParameterSpec.class);103salt = pbeSpec.getSalt();104iterationCount = pbeSpec.getIterationCount();105IvParameterSpec ivSpec =106(IvParameterSpec) pbeSpec.getParameterSpec();107iv = ivSpec.getIV();108}109System.out.println(" salt[" + salt.length + "]: " +110String.format("0x%0" + (salt.length * 2) + "x",111new java.math.BigInteger(1, salt)));112System.out.println("iterationCount=" + iterationCount);113System.out.println(" iv[" + iv.length + "]: " +114String.format("0x%0" + (iv.length * 2) + "x",115new java.math.BigInteger(1, iv)));116117// Decrypt118System.out.println("Decrypting...");119Cipher pbeCipher2 = Cipher.getInstance(algo);120pbeCipher2.init(Cipher.DECRYPT_MODE, pbeKey, aps);121byte[] cleartext2 = pbeCipher2.doFinal(ciphertext);122System.out.println(" text[" + cleartext2.length + "]: " +123String.format("0x%0" + (cleartext2.length * 2) + "x",124new java.math.BigInteger(1, cleartext2)));125126if (Arrays.equals(cleartext, cleartext2)) {127System.out.println(128"\nPass: decrypted ciphertext matches the original text\n");129} else {130throw new Exception(131"Fail: decrypted ciphertext does NOT match the original text");132}133}134}135136137