1.keytool tool generates KeyStore file
Keytool-genkey-alias csdn-keypass 123456-keyalg rsa-keysize 1024-validity 3650-keystore Orepass 888999
Copy of the document to the project
Code Snippets
Package com.demo.test;
Import Java.io.FileInputStream;
Import Java.security.KeyStore;
Import Java.security.PrivateKey;
Import Java.security.PublicKey;
Import Java.security.cert.Certificate;
Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder; /** * @author Administrator * Use the following command to generate the KeyStore file * Alias: Alias Csdn * Keypass: alias password 123456 * Storepass: Secret key Library password: 888999 * ke Ytool-genkey-alias csdn-keypass 123456-keyalg rsa-keysize 1024-validity 365-keystore Pass 888999 */public class Testkeystore {public static void main (string[] args) throws Exception {//read KeyStore file converted to
KeyStore KeyStore object FileInputStream fis = new FileInputStream ("/csdn_server.keystore");
Because the type of certificate generated is JKS there are other formats KeyStore KeyStore = keystore.getinstance ("JKS");
The KeyStore's password "888999" String storepass= "888999";
Keystore.load (FIS, Storepass.tochararray ());
Fis.close (); Read the certificate and private key from the KeyStore string alias = "CSDN";//alias string keypass = "123456"; Alias Password certificate cerTificate = Keystore.getcertificate (alias);
Read Public Key object PublicKey PublicKey = Certificate.getpublickey ();
System.out.println ("The extracted public key is ___:\n" +encodebase64 (publickey.tostring ()));
Read the private Key object Privatekey Privatekey = (privatekey) Keystore.getkey (Alias,keypass.tochararray ());
System.out.println ("The extracted private key is ___:\n" +encodebase64 (privatekey.tostring ())); //pair character password encryption public static string EncodeBase64 (String str) throws Exception {//1. Converts a passed in string cipher to a byte array
The Base64 encryption tool produces a cryptographic string base64str = new Base64encoder (). Encode (Str.getbytes ("UTF-8"));
return base64str; ///Decrypt the ciphertext string public static string DecodeBase64 (String base64str) throws Exception {///based on a cryptographic string
Use the Base64 decryption tool to get the original plaintext string password byte[] bytes = new Base64decoder (). Decodebuffer (BASE64STR);
String generalstr = new String (bytes, "UTF-8");
return generalstr;
}
}
Run results