Java Security Architecture ___ Read the key pair and certificate information for the KeyStore file

Source: Internet
Author: User

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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.