Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Cipher/DES/DesAPITest.java
38889 views
/*1* Copyright (c) 1997, 2007, 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.*;34import com.sun.crypto.provider.*;3536public class DesAPITest {3738Cipher cipher;39IvParameterSpec params = null;40SecretKey cipherKey = null;4142public static byte[] key = {43(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,44(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef45};4647public static byte[] key3 = {48(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,49(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef,50(byte)0xf0,(byte)0xe1,(byte)0xd2,(byte)0xc3,51(byte)0xb4,(byte)0xa5,(byte)0x96,(byte)0x87,52(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,53(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};5455public static byte[] iv = {56(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,57(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};5859static String[] crypts = {"DES", "DESede"};60//static String[] modes = {"ECB", "CBC", "CFB", "OFB", "PCBC"};61static String[] modes = {"CFB24"};62//static String[] paddings = {"PKCS5Padding", "NoPadding"};63static String[] paddings = {"PKCS5Padding"};6465public static void main(String[] args) throws Exception {66DesAPITest test = new DesAPITest();67test.run();68}6970public void run() throws Exception {7172for (int i=0; i<crypts.length; i++) {73for (int j=0; j<modes.length; j++) {74for (int k=0; k<paddings.length; k++) {75System.out.println76("===============================");77System.out.println78(crypts[i]+" "+modes[j]+" " + paddings[k]);79init(crypts[i], modes[j], paddings[k]);80runTest();81}82}83}84}8586public void init(String crypt, String mode, String padding)87throws Exception {8889SunJCE jce = new SunJCE();90Security.addProvider(jce);9192KeySpec desKeySpec = null;93SecretKeyFactory factory = null;9495StringBuffer cipherName = new StringBuffer(crypt);96if (mode.length() != 0)97cipherName.append("/" + mode);98if (padding.length() != 0)99cipherName.append("/" + padding);100101cipher = Cipher.getInstance(cipherName.toString());102if (crypt.endsWith("ede")) {103desKeySpec = new DESedeKeySpec(key3);104factory = SecretKeyFactory.getInstance("DESede", "SunJCE");105}106else {107desKeySpec = new DESKeySpec(key);108factory = SecretKeyFactory.getInstance("DES", "SunJCE");109}110111// retrieve the cipher key112cipherKey = factory.generateSecret(desKeySpec);113114// retrieve iv115if ( !mode.equals("ECB"))116params = new IvParameterSpec(iv);117else118params = null;119}120121public void runTest() throws Exception {122123int bufferLen = 512;124byte[] input = new byte[bufferLen];125int len;126127// encrypt test128cipher.init(Cipher.ENCRYPT_MODE, cipherKey, params);129130// getIV131System.out.println("getIV, " + cipher.getIV());132byte[] output = null;133boolean thrown = false;134try {135input = null;136output = cipher.update(input, 0, -1);137} catch (IllegalArgumentException ex) {138thrown = true;139}140if (!thrown) {141throw new Exception("Expected IAE not thrown!");142}143byte[] inbuf = "itaoti7890123456".getBytes();144System.out.println("inputLength: " + inbuf.length);145output = cipher.update(inbuf);146147len = cipher.getOutputSize(16);148byte[] out = new byte[len];149output = cipher.doFinal();150System.out.println(len + " " + TestUtility.hexDump(output));151}152}153154155