StoreSecretKeyTest.java revision 6427:1da93663f8f3
1/* 2 * Copyright (c) 2013, 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/* 25 * @test 26 * @bug 8005408 27 * @summary KeyStore API enhancements 28 */ 29 30import java.io.*; 31import java.security.*; 32import java.util.*; 33import javax.crypto.*; 34import javax.crypto.spec.*; 35 36// Store a secret key in a keystore and retrieve it again. 37 38public class StoreSecretKeyTest { 39 private final static String DIR = System.getProperty("test.src", "."); 40 private static final char[] PASSWORD = "passphrase".toCharArray(); 41 private static final String KEYSTORE = "keystore.p12"; 42 private static final String ALIAS = "my secret key"; 43 44 public static void main(String[] args) throws Exception { 45 46 new File(KEYSTORE).delete(); 47 48 try { 49 50 KeyStore keystore = KeyStore.getInstance("PKCS12"); 51 keystore.load(null, null); 52 53 // Set entry 54 keystore.setEntry(ALIAS, 55 new KeyStore.SecretKeyEntry(generateSecretKey("AES", 128)), 56 new KeyStore.PasswordProtection(PASSWORD)); 57 58 System.out.println("Storing keystore to: " + KEYSTORE); 59 keystore.store(new FileOutputStream(KEYSTORE), PASSWORD); 60 61 System.out.println("Loading keystore from: " + KEYSTORE); 62 keystore.load(new FileInputStream(KEYSTORE), PASSWORD); 63 System.out.println("Loaded keystore with " + keystore.size() + 64 " entries"); 65 KeyStore.Entry entry = keystore.getEntry(ALIAS, 66 new KeyStore.PasswordProtection(PASSWORD)); 67 System.out.println("Retrieved entry: " + entry); 68 69 if (entry instanceof KeyStore.SecretKeyEntry) { 70 System.out.println("Retrieved secret key entry: " + 71 entry); 72 } else { 73 throw new Exception("Not a secret key entry"); 74 } 75 } finally { 76 new File(KEYSTORE).delete(); 77 } 78 } 79 80 private static SecretKey generateSecretKey(String algorithm, int size) 81 throws NoSuchAlgorithmException { 82 KeyGenerator generator = KeyGenerator.getInstance(algorithm); 83 generator.init(size); 84 return generator.generateKey(); 85 } 86} 87