Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/DES/TextPKCS5PaddingTest.java
41161 views
/*1* Copyright (c) 2001, 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 804860426* @summary This test checks boundary conditions for testing27* ShortBufferException.28*/29import static java.lang.System.out;3031import java.security.AlgorithmParameters;32import java.security.Provider;33import java.security.Security;34import javax.crypto.BadPaddingException;35import javax.crypto.Cipher;36import javax.crypto.KeyGenerator;37import javax.crypto.SecretKey;3839public class TextPKCS5PaddingTest {40/**41* Test plain text.42*/43private static final byte[] PLAIN_TEXT = {440b10001, 0b10001, 0b10001, 0b10001,450b10001, 0b10001, 0b11, 0b1146};4748public static void main(String[] args) throws Exception {49Provider provider = Security.getProvider("SunJCE");50if (provider == null) {51throw new RuntimeException("SunJCE provider not exist");52}53// generate no-padding cipher with secret key54Cipher c = Cipher.getInstance("DES/CBC/NoPadding", provider);55KeyGenerator kgen = KeyGenerator.getInstance("DES", provider);56SecretKey skey = kgen.generateKey();57// this is the improperly padded plaintext5859c.init(Cipher.ENCRYPT_MODE, skey);60// encrypt plaintext61byte[] cipher = c.doFinal(PLAIN_TEXT);62AlgorithmParameters params = c.getParameters();63// generate cipher that enforces PKCS5 padding64c = Cipher.getInstance("DES/CBC/PKCS5Padding", provider);65c.init(Cipher.DECRYPT_MODE, skey, params);66try {67c.doFinal(cipher);68throw new RuntimeException(69"ERROR: Expected BadPaddingException not thrown");70} catch (BadPaddingException expected) {71out.println("Expected BadPaddingException thrown");72}7374}75}767778