Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/Test4513830.java
41161 views
/*1* Copyright (c) 2002, 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 451383026* @summary Verify the output size returned by AES cipher.getOutputSize27* method in DECRYPT mode does not add extra bytes for padding28* @author Valerie Peng29* @key randomness30*/31import java.io.PrintStream;32import java.security.*;33import java.security.spec.*;34import java.util.Random;3536import javax.crypto.*;37import javax.crypto.spec.*;38import java.security.Provider;3940public class Test4513830 {4142private static final String ALGO = "AES";43private static final String MODE = "ECB";44private static final String PADDING = "PKCS5Padding";45private static final int KEYSIZE = 16; // in bytes46private static final int TEXTLENGTHS[] = {4716, 17, 18, 19, 20, 21, 22, 23 };4849public boolean execute() throws Exception {50Random rdm = new Random();51byte[] plainText=new byte[125];52rdm.nextBytes(plainText);5354Cipher ci = Cipher.getInstance(ALGO+"/"+MODE+"/"+PADDING, "SunJCE");5556// TEST FIX 451383057KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");58kg.init(KEYSIZE*8);59SecretKey key = kg.generateKey();6061ci.init(Cipher.DECRYPT_MODE, key);62int recoveredTextLength = ci.getOutputSize(16);6364if (recoveredTextLength != 16) {65throw new Exception("output size should not increase when decrypting!");66}6768// BONUS TESTS69// 1. call getOutputSize with various lengths and see if70// the returned size is correct.71for (int i=0; i<TEXTLENGTHS.length; i++) {72ci.init(Cipher.ENCRYPT_MODE, key);73int cipherTextLength = ci.getOutputSize(TEXTLENGTHS[i]);74if (cipherTextLength != 32) {75throw new Exception("output size " + cipherTextLength76+ " for input size " + TEXTLENGTHS[i]77+ " when encrypting is wrong!");78}79}8081// passed all tests...hooray!82return true;83}8485public static void main (String[] args) throws Exception {86Test4513830 test = new Test4513830();87String testName = test.getClass().getName() + "[" + ALGO +88"/" + MODE + "/" + PADDING + "]";89if (test.execute()) {90System.out.println(testName + ": Passed!");91}92}93}949596