Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/TextLength/TestCipherTextLength.java
41161 views
/*1* Copyright (c) 2015, 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.NoSuchAlgorithmException;26import java.security.spec.InvalidKeySpecException;27import java.util.Arrays;28import javax.crypto.Cipher;29import javax.crypto.NoSuchPaddingException;3031/*32* @test33* @bug 804860134* @summary Performs multiple-part encryption/decryption depending on the35* specified encryption mode and check if the results obtained by36* different ways are the same.37*/38public class TestCipherTextLength {3940/* Algorithms tested by DESCipherWrapper */41private static final String[] DES_ALGORITHMS = {"DES", "DESede",42"Blowfish"};43private static final String[] DES_MODES = {"ECB", "CBC", "PCBC"};44private static final String[] DES_PADDING = {"PKCS5Padding"};4546/* Algorithms tested by PBECipherWrapper */47private static final String[] PBE_ALGORITHMS = {"PBEWithMD5AndDES",48"PBEWithMD5AndDES/CBC/PKCS5Padding", "PBEWithMD5ANDTripleDES",49"PBEWithMD5AndTripleDES/CBC/PKCS5Padding", "PBEwithSHA1AndDESede",50"PBEwithSHA1AndDESede/CBC/PKCS5Padding", "PBEwithSHA1AndRC2_40",51"PBEwithSHA1Andrc2_40/CBC/PKCS5Padding", "PBEWithSHA1AndRC2_128",52"PBEWithSHA1andRC2_128/CBC/PKCS5Padding", "PBEWithSHA1AndRC4_40",53"PBEWithsha1AndRC4_40/ECB/NoPadding", "PBEWithSHA1AndRC4_128",54"PBEWithSHA1AndRC4_128/ECB/NoPadding", "PBEWithHmacSHA1AndAES_128",55"PBEWithHmacSHA224AndAES_128", "PBEWithHmacSHA256AndAES_128",56"PBEWithHmacSHA384AndAES_128", "PBEWithHmacSHA512AndAES_128",57"PBEWithHmacSHA1AndAES_256", "PBEWithHmacSHA224AndAES_256",58"PBEWithHmacSHA256AndAES_256", "PBEWithHmacSHA384AndAES_256",59"PBEWithHmacSHA512AndAES_256", "PBKDF2WithHmacSHA1",60"PBKDF2WithHmacSHA224", "PBKDF2WithHmacSHA256",61"PBKDF2WithHmacSHA384", "PBKDF2WithHmacSHA512"};62private static final String PBE_PASSWORD = "Hush, it's a secret!!";6364// Algorithm tested by PBKDF2Wrappter65private static final String PBKDF2 = "PBKDF2";6667// Algorithm tested by AESPBEWrapper68private static final String AES = "AES";6970public static void main(String[] args) throws Exception {71byte[] plainText = new byte[64];72for (int i = 0; i < 64; i++) {73plainText[i] = (byte) (i & 0xff);74}7576new TestCipherTextLength().runAll(plainText);77}7879public void runAll(byte[] plainText) throws Exception {8081// Testing DES/Blowfish Cipher82for (String algorithm : DES_ALGORITHMS) {83for (String desMode : DES_MODES) {84for (String padding : DES_PADDING) {85out.println("=>Testing: " + algorithm + "/" + desMode86+ "/" + padding);87DESCipherWrapper desCi = new DESCipherWrapper(algorithm,88desMode, padding);89desCi.execute(Cipher.ENCRYPT_MODE, plainText);90desCi.execute(Cipher.DECRYPT_MODE, desCi.getResult());91if (!Arrays.equals(plainText, desCi.getResult())) {92throw new RuntimeException(93"Plain and recovered texts are not same for:"94+ algorithm + "/" + desMode + "/"95+ padding);96}97}98}99}100101// Testing PBE Cipher102for (String algorithm : PBE_ALGORITHMS) {103int maxKeyLen = Cipher.getMaxAllowedKeyLength(algorithm);104boolean isUnlimited = maxKeyLen == Integer.MAX_VALUE;105if (!isUnlimited106&& (algorithm.contains("TripleDES") || algorithm107.contains("AES_256"))) {108out.println("Test " + algorithm + " will be ignored");109continue;110}111112out.println("=>Testing: " + algorithm);113PBECipherWrapper pbeCi = createWrapper(algorithm, PBE_PASSWORD);114pbeCi.execute(Cipher.ENCRYPT_MODE, plainText);115pbeCi.execute(Cipher.DECRYPT_MODE, pbeCi.getResult());116if (!Arrays.equals(plainText, pbeCi.getResult())) {117throw new RuntimeException(118"Plain and recovered texts are not same for:"119+ algorithm);120}121}122}123124private PBECipherWrapper createWrapper(String algo, String passwd)125throws InvalidKeySpecException, NoSuchAlgorithmException,126NoSuchPaddingException {127if (algo.contains(PBKDF2)) {128return new PBECipherWrapper.PBKDF2(algo, passwd);129} else if (algo.contains(AES)) {130return new PBECipherWrapper.AES(algo, passwd);131} else {132return new PBECipherWrapper.Legacy(algo, passwd);133}134}135}136137138