test/jdk/com/sun/crypto/provider/Cipher/AES/Padding.java
changeset 57538 445c32471dc6
parent 47216 71c04702a3d5
equal deleted inserted replaced
57513:6073b2290c0a 57538:445c32471dc6
     1 /*
     1 /*
     2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    46  */
    46  */
    47 public class Padding {
    47 public class Padding {
    48 
    48 
    49     private static final String ALGORITHM = "AES";
    49     private static final String ALGORITHM = "AES";
    50     private static final String PROVIDER = "SunJCE";
    50     private static final String PROVIDER = "SunJCE";
    51     private static final String[] MODES = { "ECb", "CbC", "PCBC", "OFB",
    51     private static final String[] MODES_PKCS5PAD = {
       
    52         "ECb", "CbC", "PCBC", "OFB",
    52         "OFB150", "cFB", "CFB7", "cFB8", "cFB16", "cFB24", "cFB32",
    53         "OFB150", "cFB", "CFB7", "cFB8", "cFB16", "cFB24", "cFB32",
    53         "Cfb40", "cfB48", "cfB56", "cfB64", "cfB72", "cfB80", "cfB88",
    54         "Cfb40", "cfB48", "cfB56", "cfB64", "cfB72", "cfB80", "cfB88",
    54         "cfB96", "cfb104", "cfB112", "cfB120", "OFB8", "OFB16", "OFB24",
    55         "cfB96", "cfb104", "cfB112", "cfB120", "OFB8", "OFB16", "OFB24",
    55         "OFB32", "OFB40", "OFB48", "OFB56", "OFB64", "OFB72", "OFB80",
    56         "OFB32", "OFB40", "OFB48", "OFB56", "OFB64", "OFB72", "OFB80",
    56         "OFB88", "OFB96", "OFB104", "OFB112", "OFB120", "GCM" };
    57         "OFB88", "OFB96", "OFB104", "OFB112", "OFB120" };
    57     private static final String PADDING = "PKCS5Padding";
    58     private static final String[] MODES_NOPAD = { "CTR", "CTS", "GCM" };
       
    59 
    58     private static final int KEY_LENGTH = 128;
    60     private static final int KEY_LENGTH = 128;
    59 
    61 
    60     public static void main(String argv[]) throws Exception {
    62     public static void main(String argv[]) throws Exception {
    61         Padding test = new Padding();
    63         Padding test = new Padding();
    62         for (String mode : MODES) {
    64         for (String mode : MODES_PKCS5PAD) {
    63             test.runTest(ALGORITHM, mode, PADDING);
    65             test.runTest(ALGORITHM, mode, "PKCS5Padding");
       
    66         }
       
    67         for (String mode : MODES_NOPAD) {
       
    68             test.runTest(ALGORITHM, mode, "NoPadding");
    64         }
    69         }
    65     }
    70     }
    66 
    71 
    67     public void runTest(String algo, String mo, String pad) throws Exception {
    72     public void runTest(String algo, String mo, String pad) throws Exception {
    68         Cipher ci = null;
    73         Cipher ci = null;
    90 
    95 
    91                 byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    96                 byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    92                 int offset = ci.update(plainText, 0, plainText.length,
    97                 int offset = ci.update(plainText, 0, plainText.length,
    93                         cipherText, 0);
    98                         cipherText, 0);
    94                 ci.doFinal(cipherText, offset);
    99                 ci.doFinal(cipherText, offset);
    95 
       
    96                 if (!mo.equalsIgnoreCase("ECB")) {
   100                 if (!mo.equalsIgnoreCase("ECB")) {
    97                     iv = ci.getIV();
   101                     iv = ci.getIV();
    98                     aps = new IvParameterSpec(iv);
   102                     aps = new IvParameterSpec(iv);
    99                 } else {
   103                 } else {
   100                     aps = null;
   104                     aps = null;