Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/TestISO10126Padding.java
41161 views
/*1* Copyright (c) 2003, 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 492144326* @summary Ensure ISO10126Padding works correctly.27* @author Valerie Peng28* @key randomness29*/30import java.util.Arrays;31import java.security.*;32import java.security.spec.*;3334import javax.crypto.*;35import javax.crypto.spec.*;36import java.security.Provider;3738public class TestISO10126Padding {39private static final String ALGO = "AES";40private static final String TRANS = "AES/ECB";41private static final int KEYSIZE = 16; // in bytes4243private SecretKey key;4445private TestISO10126Padding() throws Exception {46// setup47KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");48kg.init(KEYSIZE*8);49key = kg.generateKey();50}5152private void runTest(int dataLength) throws Exception {53// setup54byte[] data = new byte[dataLength];55new SecureRandom().nextBytes(data);56System.out.println("Testing data length: " + dataLength);5758// TEST#1 --59// generate the cipher text using manually-supplied60// XML Encryption padding61Cipher ci = Cipher.getInstance(TRANS + "/NoPadding", "SunJCE");62ci.init(Cipher.ENCRYPT_MODE, key);63byte[] paddedData = new byte[ci.getBlockSize()];64System.arraycopy(data, 0, paddedData, 0, data.length);65int padValue = paddedData.length - data.length;66paddedData[paddedData.length-1] = (byte) padValue;67byte[] cipherText = ci.doFinal(paddedData);6869// decrypt using ISO10126Padding70ci = Cipher.getInstance(TRANS + "/ISO10126Padding", "SunJCE");71ci.init(Cipher.DECRYPT_MODE, key);72byte[] recovered = ci.doFinal(cipherText);73if (!Arrays.equals(data, recovered)) {74throw new Exception("TEST#1: decryption failed");75}76// TEST#2 --77// generate the cipher text using ISO10126Padding78ci = Cipher.getInstance(TRANS + "/ISO10126Padding", "SunJCE");79ci.init(Cipher.ENCRYPT_MODE, key);80cipherText = ci.doFinal(data);8182// decrypt using ISO10126Padding83ci.init(Cipher.DECRYPT_MODE, key);84recovered = ci.doFinal(cipherText);85if (!Arrays.equals(data, recovered)) {86throw new Exception("TEST#2: decryption failed");87}88}8990public static void main(String[] argv) throws Exception {91TestISO10126Padding test = new TestISO10126Padding();92for (int i = 0; i<16; i++) {93test.runTest(i);94}95System.out.println("Test Passed");96}97}9899100