Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/DES/DesAPITest.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 000000026* @summary DesAPITest27* @author Jan Luehe28*/29import java.io.*;30import java.security.*;31import java.security.spec.*;32import javax.crypto.*;33import javax.crypto.spec.*;3435public class DesAPITest {3637Cipher cipher;38IvParameterSpec params = null;39SecretKey cipherKey = null;4041public static byte[] key = {42(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,43(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef44};4546public static byte[] key3 = {47(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,48(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef,49(byte)0xf0,(byte)0xe1,(byte)0xd2,(byte)0xc3,50(byte)0xb4,(byte)0xa5,(byte)0x96,(byte)0x87,51(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,52(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};5354public static byte[] iv = {55(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,56(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};5758static String[] crypts = {"DES", "DESede"};59//static String[] modes = {"ECB", "CBC", "CFB", "OFB", "PCBC"};60static String[] modes = {"CFB24"};61//static String[] paddings = {"PKCS5Padding", "NoPadding"};62static String[] paddings = {"PKCS5Padding"};6364public static void main(String[] args) throws Exception {65DesAPITest test = new DesAPITest();66test.run();67}6869public void run() throws Exception {7071for (int i=0; i<crypts.length; i++) {72for (int j=0; j<modes.length; j++) {73for (int k=0; k<paddings.length; k++) {74System.out.println75("===============================");76System.out.println77(crypts[i]+" "+modes[j]+" " + paddings[k]);78init(crypts[i], modes[j], paddings[k]);79runTest();80}81}82}83}8485public void init(String crypt, String mode, String padding)86throws Exception {8788KeySpec desKeySpec = null;89SecretKeyFactory factory = null;9091StringBuffer cipherName = new StringBuffer(crypt);92if (mode.length() != 0)93cipherName.append("/" + mode);94if (padding.length() != 0)95cipherName.append("/" + padding);9697cipher = Cipher.getInstance(cipherName.toString(), "SunJCE");98if (crypt.endsWith("ede")) {99desKeySpec = new DESedeKeySpec(key3);100factory = SecretKeyFactory.getInstance("DESede", "SunJCE");101}102else {103desKeySpec = new DESKeySpec(key);104factory = SecretKeyFactory.getInstance("DES", "SunJCE");105}106107// retrieve the cipher key108cipherKey = factory.generateSecret(desKeySpec);109110// retrieve iv111if ( !mode.equals("ECB"))112params = new IvParameterSpec(iv);113else114params = null;115}116117public void runTest() throws Exception {118119int bufferLen = 512;120byte[] input = new byte[bufferLen];121int len;122123// encrypt test124cipher.init(Cipher.ENCRYPT_MODE, cipherKey, params);125126// getIV127System.out.println("getIV, " + cipher.getIV());128byte[] output = null;129boolean thrown = false;130try {131input = null;132output = cipher.update(input, 0, -1);133} catch (IllegalArgumentException ex) {134thrown = true;135}136if (!thrown) {137throw new Exception("Expected IAE not thrown!");138}139byte[] inbuf = "itaoti7890123456".getBytes();140System.out.println("inputLength: " + inbuf.length);141output = cipher.update(inbuf);142143len = cipher.getOutputSize(16);144byte[] out = new byte[len];145output = cipher.doFinal();146System.out.println(len + " " + TestUtility.hexDump(output));147}148}149150151