jdk/test/com/sun/crypto/provider/Cipher/PBE/TestCipherPBE.java
changeset 32411 d29d9a2a6f1f
child 42362 7b70c5bfe82e
equal deleted inserted replaced
32410:fbfb7fc9f5c3 32411:d29d9a2a6f1f
       
     1 /*
       
     2  * Copyright (c) 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 import static java.lang.System.out;
       
    25 
       
    26 import java.security.InvalidAlgorithmParameterException;
       
    27 import java.security.InvalidKeyException;
       
    28 import java.security.NoSuchAlgorithmException;
       
    29 import java.security.spec.AlgorithmParameterSpec;
       
    30 import java.security.spec.InvalidKeySpecException;
       
    31 import java.util.Arrays;
       
    32 
       
    33 import javax.crypto.BadPaddingException;
       
    34 import javax.crypto.Cipher;
       
    35 import javax.crypto.IllegalBlockSizeException;
       
    36 import javax.crypto.NoSuchPaddingException;
       
    37 import javax.crypto.SecretKey;
       
    38 import javax.crypto.SecretKeyFactory;
       
    39 import javax.crypto.ShortBufferException;
       
    40 import javax.crypto.spec.PBEKeySpec;
       
    41 import javax.crypto.spec.PBEParameterSpec;
       
    42 
       
    43 /*
       
    44  * @test
       
    45  * @bug 8048601
       
    46  * @summary Tests for PBE ciphers
       
    47  */
       
    48 public class TestCipherPBE {
       
    49 
       
    50     private static final String[] ALGORITHMS = {"PBEWithMD5AndDES",
       
    51         "PBEWithMD5AndDES/CBC/PKCS5Padding", "PBEWithMD5AndTripleDES",
       
    52         "PBEWithMD5AndTripleDES/CBC/PKCS5Padding"};
       
    53 
       
    54     private static final String KEY_ALGO = "pbeWithMD5ANDdes";
       
    55     private final byte[] SALT;
       
    56     private final byte[] PLAIN_TEXT;
       
    57 
       
    58     public TestCipherPBE() {
       
    59         SALT = generateBytes(8);
       
    60         PLAIN_TEXT = generateBytes(200);
       
    61     }
       
    62 
       
    63     public static void main(String[] args) throws Exception {
       
    64 
       
    65         new TestCipherPBE().runAll();
       
    66     }
       
    67 
       
    68     private void runAll() throws Exception {
       
    69         for (String algorithm : ALGORITHMS) {
       
    70             runTest(algorithm);
       
    71         }
       
    72     }
       
    73 
       
    74     private void runTest(String algorithm)
       
    75             throws InvalidKeySpecException, NoSuchAlgorithmException,
       
    76             InvalidAlgorithmParameterException, ShortBufferException,
       
    77             NoSuchPaddingException, IllegalBlockSizeException,
       
    78             BadPaddingException, InvalidKeyException {
       
    79 
       
    80         out.println("=> Testing: " + algorithm);
       
    81 
       
    82         try {
       
    83             // Initialization
       
    84             AlgorithmParameterSpec algoParamSpec
       
    85                     = new PBEParameterSpec(SALT, 6);
       
    86 
       
    87             SecretKey secretKey
       
    88                     = SecretKeyFactory.getInstance(KEY_ALGO).generateSecret(
       
    89                     new PBEKeySpec(("Secret Key Value").toCharArray()));
       
    90 
       
    91             Cipher ci = Cipher.getInstance(algorithm);
       
    92             ci.init(Cipher.ENCRYPT_MODE, secretKey, algoParamSpec);
       
    93 
       
    94             // Encryption
       
    95             byte[] cipherText = ci.doFinal(PLAIN_TEXT);
       
    96 
       
    97             // Decryption
       
    98             ci.init(Cipher.DECRYPT_MODE, secretKey, algoParamSpec);
       
    99             byte[] recoveredText = ci.doFinal(cipherText);
       
   100 
       
   101             if (algorithm.contains("TripleDES")) {
       
   102                 throw new RuntimeException(
       
   103                         "Expected InvalidKeyException exception uncaugh");
       
   104             }
       
   105 
       
   106             // Comparison
       
   107             if (!Arrays.equals(PLAIN_TEXT, recoveredText)) {
       
   108                 throw new RuntimeException(
       
   109                         "Test failed: plainText is not equal to recoveredText");
       
   110             }
       
   111             out.println("Test Passed.");
       
   112         } catch (InvalidKeyException ex) {
       
   113             if (algorithm.contains("TripleDES")) {
       
   114                 out.println("Expected InvalidKeyException raised");
       
   115             } else {
       
   116                 throw new RuntimeException(ex);
       
   117             }
       
   118         }
       
   119     }
       
   120 
       
   121     public static byte[] generateBytes(int length) {
       
   122         byte[] bytes = new byte[length];
       
   123         for (int i = 0; i < length; i++) {
       
   124             bytes[i] = (byte) (i & 0xff);
       
   125         }
       
   126         return bytes;
       
   127     }
       
   128 }