0N/A/*
2362N/A * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 0000000
0N/A * @summary DesAPITest
0N/A * @author Jan Luehe
0N/A */
0N/Aimport java.io.*;
0N/Aimport java.security.*;
0N/Aimport java.security.spec.*;
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/Aimport com.sun.crypto.provider.*;
0N/A
0N/Apublic class DesAPITest {
0N/A
0N/A Cipher cipher;
0N/A IvParameterSpec params = null;
0N/A SecretKey cipherKey = null;
0N/A
0N/A public static byte[] key = {
0N/A (byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,
0N/A (byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef
0N/A };
0N/A
0N/A public static byte[] key3 = {
0N/A (byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,
0N/A (byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef,
0N/A (byte)0xf0,(byte)0xe1,(byte)0xd2,(byte)0xc3,
0N/A (byte)0xb4,(byte)0xa5,(byte)0x96,(byte)0x87,
0N/A (byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,
0N/A (byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};
0N/A
0N/A public static byte[] iv = {
0N/A (byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,
0N/A (byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};
0N/A
0N/A static String[] crypts = {"DES", "DESede"};
0N/A //static String[] modes = {"ECB", "CBC", "CFB", "OFB", "PCBC"};
0N/A static String[] modes = {"CFB24"};
0N/A //static String[] paddings = {"PKCS5Padding", "NoPadding"};
0N/A static String[] paddings = {"PKCS5Padding"};
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A DesAPITest test = new DesAPITest();
0N/A test.run();
0N/A }
0N/A
0N/A public void run() throws Exception {
0N/A
0N/A for (int i=0; i<crypts.length; i++) {
0N/A for (int j=0; j<modes.length; j++) {
0N/A for (int k=0; k<paddings.length; k++) {
0N/A System.out.println
0N/A ("===============================");
0N/A System.out.println
0N/A (crypts[i]+" "+modes[j]+" " + paddings[k]);
0N/A init(crypts[i], modes[j], paddings[k]);
0N/A runTest();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void init(String crypt, String mode, String padding)
0N/A throws Exception {
0N/A
0N/A SunJCE jce = new SunJCE();
0N/A Security.addProvider(jce);
0N/A
0N/A KeySpec desKeySpec = null;
0N/A SecretKeyFactory factory = null;
0N/A
0N/A StringBuffer cipherName = new StringBuffer(crypt);
0N/A if (mode.length() != 0)
0N/A cipherName.append("/" + mode);
0N/A if (padding.length() != 0)
0N/A cipherName.append("/" + padding);
0N/A
0N/A cipher = Cipher.getInstance(cipherName.toString());
0N/A if (crypt.endsWith("ede")) {
0N/A desKeySpec = new DESedeKeySpec(key3);
0N/A factory = SecretKeyFactory.getInstance("DESede", "SunJCE");
0N/A }
0N/A else {
0N/A desKeySpec = new DESKeySpec(key);
0N/A factory = SecretKeyFactory.getInstance("DES", "SunJCE");
0N/A }
0N/A
0N/A // retrieve the cipher key
0N/A cipherKey = factory.generateSecret(desKeySpec);
0N/A
0N/A // retrieve iv
0N/A if ( !mode.equals("ECB"))
0N/A params = new IvParameterSpec(iv);
0N/A else
0N/A params = null;
0N/A }
0N/A
0N/A public void runTest() throws Exception {
0N/A
0N/A int bufferLen = 512;
0N/A byte[] input = new byte[bufferLen];
0N/A int len;
0N/A
0N/A // encrypt test
0N/A cipher.init(Cipher.ENCRYPT_MODE, cipherKey, params);
0N/A
0N/A // getIV
0N/A System.out.println("getIV, " + cipher.getIV());
0N/A byte[] output = null;
0N/A boolean thrown = false;
0N/A try {
0N/A input = null;
0N/A output = cipher.update(input, 0, -1);
0N/A } catch (IllegalArgumentException ex) {
0N/A thrown = true;
0N/A }
0N/A if (!thrown) {
0N/A throw new Exception("Expected IAE not thrown!");
0N/A }
0N/A byte[] inbuf = "itaoti7890123456".getBytes();
0N/A System.out.println("inputLength: " + inbuf.length);
0N/A output = cipher.update(inbuf);
0N/A
0N/A len = cipher.getOutputSize(16);
0N/A byte[] out = new byte[len];
0N/A output = cipher.doFinal();
0N/A System.out.println(len + " " + TestUtility.hexDump(output));
0N/A }
0N/A}