- JKS is the abbreviation for Java KeyStore, is the Java Digital Certificate Library, the view Certificate private key needs the password, avoids the private key one article form to appear in the code
- Scenario One: Client signature, server check, determine if it is one of our own
- Using the Keytool build Jks,keytool tool in the Java->jdk->bin directory, use the command line, PS: Red part please replace it yourself
aliases file name . jks
After a complex input, the generated JKs file is in the desktop--my document root, and this jks contains the auto-generated private key
- Export public key CER certificate, generated CER file in desktop--My document root directory
aliases file name Password file name . cer
Use the private key to sign the data, here only the core code, time to complement the full
1KeyStore KS = keystore.getinstance ("JKS");2FIS =NewFileInputStream ("Path\\file name. JKs ");3String Storepassword = "the password when generating jks above";4String Storealias = "aliases when generating jks above";5 ks.load (FIS, Storepassword.tochararray ());6Privatekey Prikey =(Privatekey) Ks.getkey (Storealias,Storepassword.tochararray ());
7 String prikeystr = Base64.getencoder (). Encodetostring (prikey.getencoded ());//view private key with
8 System.out.println ("Private key:" +PRIKEYSTR);//View private key
9//Sign with private key
- The server reads the public key with a CER file, and the client transmits the data for verification
- Scenario Two: Client-side encryption, server decryption, encryption of transmitted data
- Reads the server CER certificate as the public key, then encrypts the plaintext with the public key, transmits the ciphertext to the server
1 New FileInputStream ("path \ \ filename. cer"
2 certificatefactory CF = Certificatefactory.getinstance ("the"
3 X509Certificate cert = (x509certificate) cf.generatecertificate (IS);
4 PublicKey publickey=cert.getpublickey ();
5 String pubkeystr= Base64.getencoder (). Encodetostring (publickey.getencoded ());//View public key
6 System.out.println ("Public key:" +PUBKEYSTR);//View public key
7//With public key encryption
The server uses the private key to solve the plaintext
Note: The general clear text is longer, the asymmetric encryption and decryption is slow, so the general first encrypts the plaintext with symmetric encryption, and then encrypts the symmetric encryption with asymmetric cryptography .
JKs CER certificate generation