Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/TestCipherPBE.java
41161 views
/*1* Copyright (c) 2015, 2016, 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*/2223import static java.lang.System.out;2425import java.security.InvalidAlgorithmParameterException;26import java.security.InvalidKeyException;27import java.security.NoSuchAlgorithmException;28import java.security.spec.AlgorithmParameterSpec;29import java.security.spec.InvalidKeySpecException;30import java.util.Arrays;3132import javax.crypto.BadPaddingException;33import javax.crypto.Cipher;34import javax.crypto.IllegalBlockSizeException;35import javax.crypto.NoSuchPaddingException;36import javax.crypto.SecretKey;37import javax.crypto.SecretKeyFactory;38import javax.crypto.ShortBufferException;39import javax.crypto.spec.PBEKeySpec;40import javax.crypto.spec.PBEParameterSpec;4142/*43* @test44* @bug 804860145* @summary Tests for PBE ciphers46*/47public class TestCipherPBE {4849private static final String[] ALGORITHMS = {"PBEWithMD5AndDES",50"PBEWithMD5AndDES/CBC/PKCS5Padding", "PBEWithMD5AndTripleDES",51"PBEWithMD5AndTripleDES/CBC/PKCS5Padding"};5253private static final String KEY_ALGO = "pbeWithMD5ANDdes";54private final byte[] SALT;55private final byte[] PLAIN_TEXT;5657public TestCipherPBE() {58SALT = generateBytes(8);59PLAIN_TEXT = generateBytes(200);60}6162public static void main(String[] args) throws Exception {6364new TestCipherPBE().runAll();65}6667private void runAll() throws Exception {68for (String algorithm : ALGORITHMS) {69runTest(algorithm);70}71}7273private void runTest(String algorithm)74throws InvalidKeySpecException, NoSuchAlgorithmException,75InvalidAlgorithmParameterException, ShortBufferException,76NoSuchPaddingException, IllegalBlockSizeException,77BadPaddingException, InvalidKeyException {7879out.println("=> Testing: " + algorithm);8081boolean isUnlimited =82(Cipher.getMaxAllowedKeyLength(algorithm) == Integer.MAX_VALUE);8384try {85// Initialization86AlgorithmParameterSpec algoParamSpec87= new PBEParameterSpec(SALT, 6);8889SecretKey secretKey90= SecretKeyFactory.getInstance(KEY_ALGO).generateSecret(91new PBEKeySpec(("Secret Key Value").toCharArray()));9293Cipher ci = Cipher.getInstance(algorithm);94ci.init(Cipher.ENCRYPT_MODE, secretKey, algoParamSpec);9596// Encryption97byte[] cipherText = ci.doFinal(PLAIN_TEXT);9899// Decryption100ci.init(Cipher.DECRYPT_MODE, secretKey, algoParamSpec);101byte[] recoveredText = ci.doFinal(cipherText);102103if (algorithm.contains("TripleDES") && !isUnlimited) {104throw new RuntimeException(105"Expected InvalidKeyException not thrown");106}107108// Comparison109if (!Arrays.equals(PLAIN_TEXT, recoveredText)) {110throw new RuntimeException(111"Test failed: plainText is not equal to recoveredText");112}113out.println("Test Passed.");114} catch (InvalidKeyException ex) {115if (algorithm.contains("TripleDES") && !isUnlimited) {116out.println("Expected InvalidKeyException thrown");117} else {118throw new RuntimeException(ex);119}120}121}122123public static byte[] generateBytes(int length) {124byte[] bytes = new byte[length];125for (int i = 0; i < length; i++) {126bytes[i] = (byte) (i & 0xff);127}128return bytes;129}130}131132133