?? securekeystorebuilder.java
字號(hào):
/*
* Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
* (license2)
* Initial Developer: H2 Group
*/
package org.h2.tools.security;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.util.Enumeration;
import org.h2.security.SecureSocketFactory;
import org.h2.util.ByteUtils;
/**
* Tool to generate source code for the SecureSocketFactory. First, create a
* keystore using:
* <pre>
* keytool -genkey -alias h2 -keyalg RSA -dname "cn=H2" -validity 25000
* -keypass h2pass -keystore h2.keystore -storepass h2pass
* </pre>
* Then run this application to generate the source code. Then replace the code
* in the function SecureSocketFactory.getKeyStore as specified
*/
public class SecureKeyStoreBuilder {
public static void main(String[] a) throws Exception {
String password = SecureSocketFactory.KEYSTORE_PASSWORD;
KeyStore store = SecureSocketFactory.getKeyStore(password);
printKeystore(store, password);
}
private static void printKeystore(KeyStore store, String password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, CertificateEncodingException {
System.out.println("KeyStore store = KeyStore.getInstance(\""+store.getType()+"\");");
System.out.println("store.load(null, password.toCharArray());");
//System.out.println("keystore provider="+store.getProvider().getName());
Enumeration en = store.aliases();
while (en.hasMoreElements()) {
String alias = (String) en.nextElement();
Key key = store.getKey(alias, password.toCharArray());
System.out.println("KeyFactory keyFactory = KeyFactory.getInstance(\"" + key.getAlgorithm() + "\");");
System.out.println("store.load(null, password.toCharArray());");
String pkFormat = key.getFormat();
String encoded = ByteUtils.convertBytesToString(key.getEncoded());
System.out.println(pkFormat + "EncodedKeySpec keySpec = new " + pkFormat + "EncodedKeySpec(getBytes(\""
+ encoded + "\"));");
System.out.println("PrivateKey privateKey = keyFactory.generatePrivate(keySpec);");
System.out.println("Certificate[] certs = new Certificate[]{");
Certificate[] certs = store.getCertificateChain(alias);
for (int i = 0; i < certs.length; i++) {
Certificate cert = certs[i];
System.out.println(" CertificateFactory.getInstance(\""+cert.getType()+"\").");
String enc = ByteUtils.convertBytesToString(cert.getEncoded());
System.out.println(" generateCertificate(new ByteArrayInputStream(getBytes(\""+enc+"\"))),");
// PublicKey pubKey = cert.getPublicKey();
// System.out.println(" pubKey algorithm="+pubKey.getAlgorithm());
// System.out.println(" pubKey format="+pubKey.getFormat());
// System.out.println(" pubKey format="+
// ByteUtils.convertBytesToString(pubKey.getEncoded()));
}
System.out.println("};");
System.out.println("store.setKeyEntry(\""+alias+"\", privateKey, password.toCharArray(), certs);");
}
}
// private void listCipherSuites(SSLServerSocketFactory f) {
// String[] def = f.getDefaultCipherSuites();
// for (int i = 0; i < def.length; i++) {
// System.out.println("default = " + def[i]);
// }
// String[] sup = f.getSupportedCipherSuites();
// for (int i = 0; i < sup.length; i++) {
// System.out.println("supported = " + sup[i]);
// }
// }
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -