Getting started with the Java operations KeyStore

Source: Internet
Author: User

Getting started with the Java operations KeyStore


1. Using Java Keytool This key and certificate management tool, create a key and deposit it into the. keystore file, for example, there will be an entry with an alias of "MyKey" by default, or you can create it yourself.
2. Create a KeyStore instance, encrypt the KeyStore from the. keystore file, and then obtain the corresponding entry through the alias, making sure that you have the correct password.
public class Keystoretest {public static void main (string[] args) throws exception{//load KeyStore file FileInputStream in = new file InputStream ("C:\\data\\.keystore"); System.out.println (Keystore.getdefaulttype ()); KeyStore ks = Keystore.getinstance (Keystore.getdefaulttype ());//load KeyStore with correct password//otherwise java.io.IOException:Keystore was tampered with, or password is incorrectks.load (in, "Vonzhou". ToCharArray ());//Get the alias "MyKey" corresponding to the private key Privatekey key = ( Privatekey) Ks.getkey ("MyKey", "Vonzhou". ToCharArray ());//or so protectionparameter protection = new Keystore.passwordprotection ("Vonzhou". ToCharArray ()); Keystore.privatekeyentry pkentry = (keystore.privatekeyentry) ks.getentry ("MyKey", protection); Privatekey Key2 = Pkentry.getprivatekey (); System.out.println (Arrays.tostring (key.getencoded ())); System.out.println (Arrays.tostring (key2.getencoded ())); In.close ();}}



Reference:

1.keytool




Getting started with the Java operations KeyStore

Related Article

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.