JKS CER certificate generation

Source: Internet
Author: User
Tags asymmetric encryption

    • 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
  1. 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

  2. Export public key CER certificate, generated CER file in desktop--My document root directory
    aliases file name Password file name . cer
  3. 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
  4. 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
    1. 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
    2. 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

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.