Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Cipher/AES/TestISO10126Padding.java
38889 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;37import com.sun.crypto.provider.*;3839public class TestISO10126Padding {40private static final String ALGO = "AES";41private static final String TRANS = "AES/ECB";42private static final int KEYSIZE = 16; // in bytes4344private SecretKey key;4546private TestISO10126Padding() throws Exception {47// setup48KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");49kg.init(KEYSIZE*8);50key = kg.generateKey();51}5253private void runTest(int dataLength) throws Exception {54// setup55byte[] data = new byte[dataLength];56new SecureRandom().nextBytes(data);57System.out.println("Testing data length: " + dataLength);5859// TEST#1 --60// generate the cipher text using manually-supplied61// XML Encryption padding62Cipher ci = Cipher.getInstance(TRANS + "/NoPadding", "SunJCE");63ci.init(Cipher.ENCRYPT_MODE, key);64byte[] paddedData = new byte[ci.getBlockSize()];65System.arraycopy(data, 0, paddedData, 0, data.length);66int padValue = paddedData.length - data.length;67paddedData[paddedData.length-1] = (byte) padValue;68byte[] cipherText = ci.doFinal(paddedData);6970// decrypt using ISO10126Padding71ci = Cipher.getInstance(TRANS + "/ISO10126Padding", "SunJCE");72ci.init(Cipher.DECRYPT_MODE, key);73byte[] recovered = ci.doFinal(cipherText);74if (!Arrays.equals(data, recovered)) {75throw new Exception("TEST#1: decryption failed");76}77// TEST#2 --78// generate the cipher text using ISO10126Padding79ci = Cipher.getInstance(TRANS + "/ISO10126Padding", "SunJCE");80ci.init(Cipher.ENCRYPT_MODE, key);81cipherText = ci.doFinal(data);8283// decrypt using ISO10126Padding84ci.init(Cipher.DECRYPT_MODE, key);85recovered = ci.doFinal(cipherText);86if (!Arrays.equals(data, recovered)) {87throw new Exception("TEST#2: decryption failed");88}89}9091public static void main(String[] argv) throws Exception {92TestISO10126Padding test = new TestISO10126Padding();93for (int i = 0; i<16; i++) {94test.runTest(i);95}96System.out.println("Test Passed");97}98}99100101