Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/DES/PaddingTest.java
41161 views
/*1* Copyright (c) 1997, 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*/2223/*24* @test25* @bug 0000000 6296075 633027526* @summary PaddingTest27* @author Jan Luehe28*/29import java.io.*;30import java.nio.file.Files;31import java.nio.file.Paths;32import java.security.spec.*;33import javax.crypto.*;34import javax.crypto.spec.*;35import java.util.Arrays;3637public class PaddingTest {3839Cipher cipher;40IvParameterSpec params = null;41SecretKey cipherKey = null;42String pinfile = null;43String cfile = null;44String poutfile = null;4546public static byte[] key = {47(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,48(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef49};5051public static byte[] key3 = {52(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,53(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef,54(byte)0xf0,(byte)0xe1,(byte)0xd2,(byte)0xc3,55(byte)0xb4,(byte)0xa5,(byte)0x96,(byte)0x87,56(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,57(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};5859public static byte[] iv = {60(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,61(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};6263static String[] crypts = {"DES", "DESede"};64static String[] modes = {"ECB", "CBC", "CFB", "OFB", "PCBC"};65static String[] paddings = {"PKCS5Padding", "NoPadding"};66static int numFiles = 11;67static final String currDir = System.getProperty("test.src", ".");68static String dataDir = currDir + "/inputData/";6970private String padding = null;7172public static void main(String argv[]) throws Exception {73PaddingTest pt = new PaddingTest();74pt.run();75}7677public PaddingTest() {78}7980public void run() throws Exception {8182for (int l=0; l<numFiles; l++) {83pinfile = dataDir + "plain" + l + ".txt";84for (int i=0; i<crypts.length; i++) {85for (int j=0; j<modes.length; j++) {86for (int k=0; k<paddings.length; k++) {87System.out.println88("===============================");89System.out.println90(crypts[i]+" "+modes[j]+" " + paddings[k]+ " " +91"plain" + l + " test");92cfile = "c" + l + "_" +93crypts[i] + "_" +94modes[j] + "_" +95paddings[k] + ".bin";96poutfile = "p" + l +97"_" + crypts[i] + modes[j] + paddings[k] + ".txt";9899init(crypts[i], modes[j], paddings[k]);100padding = paddings[k];101runTest();102}103}104}105}106}107108public void init(String crypt, String mode, String padding)109throws Exception {110111KeySpec desKeySpec = null;112SecretKeyFactory factory = null;113114StringBuffer cipherName = new StringBuffer(crypt);115if (mode.length() != 0)116cipherName.append("/" + mode);117if (padding.length() != 0)118cipherName.append("/" + padding);119120cipher = Cipher.getInstance(cipherName.toString(), "SunJCE");121if (crypt.endsWith("ede")) {122desKeySpec = new DESedeKeySpec(key3);123factory = SecretKeyFactory.getInstance("DESede", "SunJCE");124} else {125desKeySpec = new DESKeySpec(key);126factory = SecretKeyFactory.getInstance("DES", "SunJCE");127}128129// retrieve the cipher key130cipherKey = factory.generateSecret(desKeySpec);131132// retrieve iv133if (!mode.equals("ECB"))134params = new IvParameterSpec(iv);135else136params = null;137}138139public void runTest() throws Exception {140141int bufferLen = 512;142byte[] input = new byte[bufferLen];143int len;144int totalInputLen = 0;145146try {147try (FileInputStream fin = new FileInputStream(pinfile);148BufferedInputStream pin = new BufferedInputStream(fin);149FileOutputStream fout = new FileOutputStream(cfile);150BufferedOutputStream cout = new BufferedOutputStream(fout)) {151cipher.init(Cipher.ENCRYPT_MODE, cipherKey, params);152153while ((len = pin.read(input, 0, bufferLen)) > 0) {154totalInputLen += len;155byte[] output = cipher.update(input, 0, len);156cout.write(output, 0, output.length);157}158159len = cipher.getOutputSize(0);160161byte[] out = new byte[len];162len = cipher.doFinal(out, 0);163cout.write(out, 0, len);164}165166try (FileInputStream fin = new FileInputStream(cfile);167BufferedInputStream cin = new BufferedInputStream(fin);168FileOutputStream fout = new FileOutputStream(poutfile);169BufferedOutputStream pout = new BufferedOutputStream(fout)) {170cipher.init(Cipher.DECRYPT_MODE, cipherKey, params);171172byte[] output = null;173while ((len = cin.read(input, 0, bufferLen)) > 0) {174output = cipher.update(input, 0, len);175pout.write(output, 0, output.length);176}177178len = cipher.getOutputSize(0);179byte[] out = new byte[len];180len = cipher.doFinal(out, 0);181pout.write(out, 0, len);182}183184diff(pinfile, poutfile);185} catch (IllegalBlockSizeException ex) {186if ((totalInputLen % 8 != 0) && (padding.equals("NoPadding"))) {187return;188} else {189System.out.println("Test failed!");190throw ex;191}192}193}194195private static void diff(String fname1, String fname2) throws Exception {196if (!Arrays.equals(Files.readAllBytes(Paths.get(fname1)),197Files.readAllBytes(Paths.get(fname2)))) {198throw new Exception(199"files " + fname1 + " and " + fname2 + " differ");200}201}202}203204205