Path: blob/master/test/jdk/javax/crypto/EncryptedPrivateKeyInfo/GetAlgName.java
41149 views
/*1* Copyright (c) 2003, 2007, 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 494159626* @summary Test the EncryptedPrivateKeyInfo.getAlgName(...) methods.27* @author Valerie Peng28*/29import java.util.*;30import java.nio.*;31import java.io.*;32import java.security.*;33import java.util.Arrays;34import java.security.spec.*;35import javax.crypto.*;36import javax.crypto.spec.*;3738public class GetAlgName {39private static String PASSWD = "password";4041private static final String[] ALGOS = {42"PBEWithMD5AndDES", "PBEWithSHA1AndDESede", "PBEWithSHA1AndRC2_40"43};44private static final byte[] BYTES = new byte[20];4546public static void main(String[] argv) throws Exception {47boolean status = true;48PBEKeySpec ks = new PBEKeySpec(PASSWD.toCharArray());49EncryptedPrivateKeyInfo epki;50for (int i = 0; i < ALGOS.length; i++) {51String algo = ALGOS[i];52// generate AlgorithmParameters object53SecretKeyFactory skf =54SecretKeyFactory.getInstance(algo, "SunJCE");55SecretKey key = skf.generateSecret(ks);56Cipher c = Cipher.getInstance(algo, "SunJCE");57c.init(Cipher.ENCRYPT_MODE, key);58c.doFinal(BYTES); // force the parameter generation if not already5960AlgorithmParameters ap = c.getParameters();61epki = new EncryptedPrivateKeyInfo(ap, BYTES);62if (!epki.getAlgName().equalsIgnoreCase(algo)) {63System.out.println("...expect: " + algo);64System.out.println("...got: " + epki.getAlgName());65status = false;66}67}68if (!status) {69throw new Exception("One or more tests failed");70} else {71System.out.println("Test Passed");72}73}74}757677