jdk/test/com/sun/crypto/provider/Cipher/DES/TextPKCS5PaddingTest.java
changeset 32766 2322e6c2a032
equal deleted inserted replaced
32765:b65c2f5d4d01 32766:2322e6c2a032
       
     1 /*
       
     2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8048604
       
    27  * @summary This test checks boundary conditions for testing
       
    28  *          ShortBufferException.
       
    29  */
       
    30 import static java.lang.System.out;
       
    31 
       
    32 import java.security.AlgorithmParameters;
       
    33 import java.security.Provider;
       
    34 import java.security.Security;
       
    35 import javax.crypto.BadPaddingException;
       
    36 import javax.crypto.Cipher;
       
    37 import javax.crypto.KeyGenerator;
       
    38 import javax.crypto.SecretKey;
       
    39 
       
    40 public class TextPKCS5PaddingTest {
       
    41     /**
       
    42      * Test plain text.
       
    43      */
       
    44     private static final byte[] PLAIN_TEXT = {
       
    45         0b10001, 0b10001, 0b10001, 0b10001,
       
    46         0b10001, 0b10001, 0b11,    0b11
       
    47     };
       
    48 
       
    49     public static void main(String[] args) throws Exception {
       
    50         Provider provider = Security.getProvider("SunJCE");
       
    51         if (provider == null) {
       
    52             throw new RuntimeException("SunJCE provider not exist");
       
    53         }
       
    54         // generate no-padding cipher with secret key
       
    55         Cipher c = Cipher.getInstance("DES/CBC/NoPadding", provider);
       
    56         KeyGenerator kgen = KeyGenerator.getInstance("DES", provider);
       
    57         SecretKey skey = kgen.generateKey();
       
    58         // this is the improperly padded plaintext
       
    59 
       
    60         c.init(Cipher.ENCRYPT_MODE, skey);
       
    61         // encrypt plaintext
       
    62         byte[] cipher = c.doFinal(PLAIN_TEXT);
       
    63         AlgorithmParameters params = c.getParameters();
       
    64         // generate cipher that enforces PKCS5 padding
       
    65         c = Cipher.getInstance("DES/CBC/PKCS5Padding", provider);
       
    66         c.init(Cipher.DECRYPT_MODE, skey, params);
       
    67         try {
       
    68             c.doFinal(cipher);
       
    69             throw new RuntimeException(
       
    70                     "ERROR: Expected BadPaddingException not thrown");
       
    71         } catch (BadPaddingException expected) {
       
    72             out.println("Expected BadPaddingException thrown");
       
    73         }
       
    74 
       
    75     }
       
    76 }