Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/Test4626070.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 462607026* @summary Verify that AES cipher can wrap and unwrap keys27* @author Valerie Peng28*/29import java.security.*;30import javax.crypto.*;31import java.util.*;323334public class Test4626070 {35private static final String ALGO = "AES";36private static final int KEYSIZE = 16; // in bytes3738public void execute(String mode, String padding) throws Exception {39String transformation = ALGO + "/" + mode + "/" + padding;40Cipher ci = Cipher.getInstance(transformation, "SunJCE");41KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");42kg.init(KEYSIZE*8);43SecretKey key = kg.generateKey();4445AlgorithmParameters params = ci.getParameters();4647// TEST FIX 462607048ci.init(Cipher.WRAP_MODE, key, params);49byte[] wrappedKeyEncoding = ci.wrap(key);50params = ci.getParameters();51ci.init(Cipher.UNWRAP_MODE, key, params);52Key recoveredKey = ci.unwrap(wrappedKeyEncoding, "AES",53Cipher.SECRET_KEY);54if (!key.equals(recoveredKey)) {55throw new Exception(56"key after wrap/unwrap is different from the original!");57}58System.out.println(transformation + ": Passed");59}6061public static void main (String[] args) throws Exception {62Test4626070 test = new Test4626070();63test.execute("CBC", "PKCS5Padding");64test.execute("GCM", "NoPadding");65}66}676869