Path: blob/master/test/jdk/java/security/spec/PKCS8EncodedKeySpec/Algorithm.java
41155 views
/*1* Copyright (c) 2014, 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 804722326* @summary Add algorithm parameter to PKCS8EncodedKeySpec class27*/28import java.security.spec.PKCS8EncodedKeySpec;29import java.util.Base64;30import javax.crypto.EncryptedPrivateKeyInfo;31import javax.crypto.SecretKey;32import javax.crypto.SecretKeyFactory;33import javax.crypto.spec.PBEKeySpec;3435public class Algorithm {3637private static String PKCS8PrivateKey =38"MIICoTAbBgkqhkiG9w0BBQMwDgQIqQMPwbNEhOgCAggABIICgCwRkeLXVGdO7S1h\n" +39"FAFUiwj1HCzqYFF2x9+FzjlXNwEWecZsor5eoKQlTtJ9dsPajQ/wFgY76lkXDQXE\n" +40"hdm8ndWFgCwqFBshmAp4TOvO9GlaAloDTnLMUg715D5FujiElcV7vqIY2V/7uB21\n" +41"YRanKUa21sZAFJGj6Hom1+5+k0Q7Xi4kHgt+ZIPNLwrNFPWVovbTJdScZuJaDp6m\n" +42"Q1DJUIQOzthV11VI+MU/v5SSKhj/uCaxizazEi5lgdmR7rRGgMz2YipOIjXIsKgu\n" +43"jKX5LYFAZ8nYq1hy8Q1JPR5VPuWMFqeyofO/teXJb8gI/4TC1ZoED8hXj07jpJqG\n" +44"2NVO1Dwqab31qSAjfjBkSYHKun63BvZPq2mT+frJF1YzvQhCDnWN1zbMKFNTZJfd\n" +45"cUaecH/fgNKwKpeKGgX7UlWxo26/lS8pBiJ5ihtbyFfMUBtlwEN5uOHqVFOeZp1Z\n" +46"DwCc0o1JA7yOcazA2TtNT9pc58tFZ8pEeyLj7ZchOgv06N0hZJsI6AiwII4ljd+K\n" +47"4WKvs/xiSZU3tcHaWzqlf+6/M5kC3Pihm9GhZbKBmvrZYiKyTlJEeVI3pFRNSqbE\n" +48"nZUJgkmgzNT/ZfM2WsUJm03Rq0eNCU/FDscIZnCWSA6Bf/DJDQWmhMhg2QmTGzQM\n" +49"hw/vy77q7jxV67s36HGxxR1oe8uoZ2zugBBxHWEdqyQyrVwZXJukdjrc2S7pvMln\n" +50"/VSleEf91MEcDhztyhPSqlX+H95vMnVmh5oY2gwY+P0oD5Eki6/9K+BHfuqgtS4S\n" +51"LIna1iSyLr17pRO1lmNtvuCMwmUjeI8w3JhLmxxx//bl/WCAekqj3nMplrJHZ7xd\n" +52"6k0Stxo=";5354private static String keyAlg = "RSA";55private static String password = "password";5657/*58* This test checks that a PKCS8EncodedKeySpec is properly constructed59* from an encrypted private key and that the key algorithm name can be60* retrieved as expected.61*/62public static void main(String[] argv) throws Exception {63EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(64Base64.getMimeDecoder().decode(PKCS8PrivateKey));65PBEKeySpec pks = new PBEKeySpec(password.toCharArray());66SecretKeyFactory skf = SecretKeyFactory.getInstance(epki.getAlgName());67SecretKey sk = skf.generateSecret(pks);68PKCS8EncodedKeySpec keySpec = epki.getKeySpec(sk);6970// Get the key algorithm and make sure it's what we expect71String alg = keySpec.getAlgorithm();72if (!alg.equals(keyAlg)) {73throw new Exception("Expected: " + keyAlg + ", Got: " + alg);74}7576System.out.println("Test passed");77}78}798081