001: //$Id: KeyStoreGenerator.java,v 1.2 2005/01/07 15:15:26 steview Exp $
002:
003: package org.jgroups.demos;
004:
005: import java.io.FileOutputStream;
006: import java.io.OutputStream;
007: import java.security.KeyStore;
008:
009: import javax.crypto.KeyGenerator;
010: import javax.crypto.SecretKey;
011:
012: /**
013: * Generates a keystore file that has a SecretKey in it. It is not possible to
014: * use the keytool JDk tool to achieve this. This is a simple way to generate
015: * a JCEKS format keystore and SecretKey.
016: *
017: * Usage is --alg ALGNAME --size ALGSIZE --storeName FILENAME --storePass PASSWORD --alias KEYALIAS
018: *
019: * Any of args are optional and will default to
020: * <ul>
021: * <li>ALGNAME = Blowfish
022: * <li>ALGSIZE = 56
023: * <li>FILENAME = defaultStore.keystore
024: * <li>PASSWORD = changeit
025: * <li>ALIAS = mykey
026: * </ul>
027: *
028: * @author S Woodcock
029: *
030: */
031: public class KeyStoreGenerator {
032:
033: static String symAlg = "Blowfish";
034: static int keySize = 56;
035: static String keyStoreName = "defaultStore.keystore";
036: static String storePass = "changeit";
037: static String alias = "myKey";
038:
039: public static void main(String[] args) {
040:
041: int i = 0, j;
042: String arg = null;
043: ;
044: boolean specified = false;
045:
046: while (i < args.length && args[i].startsWith("-")) {
047: arg = args[i++];
048: System.out.println("Found arg of " + arg);
049: if (arg.equalsIgnoreCase("--alg")) {
050: if (i < args.length) {
051: symAlg = args[i++];
052: } else {
053: System.out
054: .println("No Algorithm supplied using default of "
055: + symAlg);
056: }
057: } else if (arg.equalsIgnoreCase("--size")) {
058: if (i < args.length) {
059: keySize = Integer.parseInt(args[i++]);
060: } else {
061: System.out
062: .println("No Size supplied using default of "
063: + keySize);
064: }
065: } else if (arg.equalsIgnoreCase("--storeName")) {
066:
067: if (i < args.length) {
068: String temp = args[i++];
069: keyStoreName = temp;
070: } else {
071: System.out
072: .println("No keystore supplied using default of "
073: + keyStoreName);
074: }
075: } else if (arg.equalsIgnoreCase("--storePass")) {
076: if (i < args.length) {
077: storePass = args[i++];
078: } else {
079: System.out
080: .println("No password supplied using default of "
081: + storePass);
082: }
083: } else if (arg.equalsIgnoreCase("--alias")) {
084: if (i < args.length) {
085: alias = args[i++];
086: } else {
087: System.out
088: .println("No alias supplied using default of "
089: + alias);
090: }
091: }
092: }
093: System.out.println("Creating file '" + keyStoreName
094: + "' using Algorithm '" + symAlg + "' size '" + keySize
095: + "'");
096:
097: OutputStream stream = null;
098: try {
099: stream = new FileOutputStream(keyStoreName);
100: SecretKey key = initSymKey();
101: KeyStore store = KeyStore.getInstance("JCEKS");
102: store.load(null, null);
103: store
104: .setKeyEntry(alias, key, storePass.toCharArray(),
105: null);
106: store.store(stream, storePass.toCharArray());
107:
108: } catch (Exception e) {
109: e.printStackTrace();
110: } finally {
111: try {
112: stream.close();
113: } catch (Exception e) {
114:
115: }
116: }
117: System.out.println("Finished keystore creation");
118: }
119:
120: public static SecretKey initSymKey() throws Exception {
121: KeyGenerator keyGen = null;
122: // generate secret key
123:
124: keyGen = KeyGenerator.getInstance(getAlgorithm(symAlg));
125:
126: keyGen.init(keySize);
127: SecretKey secretKey = keyGen.generateKey();
128:
129: return secretKey;
130:
131: }
132:
133: private static String getAlgorithm(String s) {
134: int index = s.indexOf("/");
135: if (index == -1)
136: return s;
137:
138: return s.substring(0, index);
139: }
140: }
|