Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/PBEParametersTest.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 4944783 638320026* @summary ensure that the AlgorithmParameters object returned by27* PBE ciphers have the matching algorithm name.28* @author Valerie Peng29*/30import java.security.AlgorithmParameters;31import java.security.spec.*;32import javax.crypto.*;33import javax.crypto.spec.*;3435public class PBEParametersTest {3637private static final char[] PASSWORD = { 'p', 'a', 's', 's' };38private static final String[] PBE_ALGOS = {39"PBEWithMD5AndDES",40"PBEWithSHA1AndDESede",41"PBEWithSHA1AndRC2_40",42"PBEWithSHA1AndRC2_128",43"PBEWithSHA1AndRC4_40",44"PBEWithSHA1AndRC4_128",45// skip "PBEWithMD5AndTripleDES" since it requires Unlimited46// version of JCE jurisdiction policy files.47"PBEWithHmacSHA1AndAES_128",48"PBEWithHmacSHA224AndAES_128",49"PBEWithHmacSHA256AndAES_128",50"PBEWithHmacSHA384AndAES_128",51"PBEWithHmacSHA512AndAES_128"52// skip "PBEWithHmacSHAxxxAndAES_256" since they require Unlimited53// version of JCE jurisdiction policy files.54};55public static void main(String[] args) throws Exception {56PBEKeySpec ks = new PBEKeySpec(PASSWORD);57for (int i = 0; i < PBE_ALGOS.length; i++) {58String algo = PBE_ALGOS[i];59SecretKeyFactory skf = SecretKeyFactory.getInstance(algo);60SecretKey key = skf.generateSecret(ks);61Cipher c = Cipher.getInstance(algo, "SunJCE");62c.init(Cipher.ENCRYPT_MODE, key);63c.doFinal(new byte[10]); // force the generation of parameters64AlgorithmParameters params = c.getParameters();65if (!params.getAlgorithm().equalsIgnoreCase(algo)) {66throw new Exception("expect: " + algo +67", but got: " + params.getAlgorithm());68}69System.out.println(algo + "...done...");70}71System.out.println("Test Passed");72}73}747576