Two ways to read Java certificates __java

Source: Internet
Author: User

Direct point to the idea:

These two ways, originally is a way, here I put two kinds of cases mainly for different projects, the presentation is not the same

Here are the core ideas of two ways:

1, the parameters of the method. P12 certificate, password, KeyStore type, algorithm, protocol;
2, converts the string cipher to a character array;
3, KeyStore get the instance, getinstance (Keystoretype);
4, its call load (keystore,pwdchars);
5, Keymanagerfactory get the instance, getinstance (alogrithm);
6, its invocation init (KeyStore object, pwdchars);

7, Trustmanagerfactory get the instance, getinstance (alogrithm);
8, whose invocation initializes init (KeyStore (null));
9, Sslcontext get the instance, getinstance (protocol);
10, its initialization, init (Kf.getkeymanager (), Ktf.gettrustmanager (), null);
11, finally return Sslcontext.getsocketfactory ();

Combined with the content shown in Project one:

The first step: Create the class, which is the specified constant in the class:

Where we're using these three parameters:


Step Two: Create a method, mainly the display of core ideas, here to be very detailed


Introduction to the specific steps:

First step:

public static Socketfactory Createsocketfactory (InputStream keyStore, string password, string keystoretype, String Algorithm, String protocol) {}
KeyStore-----The contents of the certificate read by Java code as the output of the stream;
Password-----above. P12 the password entered when the certificate was created;
Keystoretype---"PKCS12";
Algorithm----"sunx509";
Protocol----"TLS";

Second step:

char[] Pwdchars = Password.tochararray ()
Converts a string cipher to a character array;

The third step:

KeyStore ks = keystore.getinstance (Keystoretype);
Get KeyStore instance;

Step Four:

Ks.load (KeyStore, pwdchars);
Load the certificate and its password using the KeyStore object;

Step Five:

Keymanagerfactory KF = keymanagerfactory.getinstance (algorithm);
Get Keymanagerfactory

Step Six:

Kf.init (KS, pwdchars);
Its invocation init (KeyStore object, Pwdchars)

Seventh Step:

Trustmanagerfactory TMF = trustmanagerfactory.getinstance (algorithm);
Obtain trustmanagerfactory;

Eighth Step:

Tmf.init ((KeyStore) null);
Initialize the Trustmanagerfactory object;

Nineth Step:

Sslcontext context = sslcontext.getinstance (protocol);
Get Sslcontext instance;

Tenth step:

Context.init (Kf.getkeymanagers (), tmf.gettrustmanagers (), NULL);
Initializes the Sslcontext object, containing the parameters kf.getkeymanagers (), tmf.gettrustmanagers ();

11th Step:

return Context.getsocketfactory ();

Returns the Socketfactory object

So far the first way of thinking and the introduction of the steps have been completed, the next direct look at the second way;

String password = ""; 
Get the password two ways: The 
 way: Directly write the configuration file, in the service layer to load the configuration file, access to the password, the applicable scene is relatively narrow, more passwords, inconvenient management;
 Mode two: The filename is named after the current password, by reading the full path, then intercepting, obtaining the current password;
FileInputStream FileInputStream = new FileInputStream ("XXXX.P12");
load. p12 certificate;
KeyStore KeyStore = keystore.getinstance ("PKCS12");
Get KeyStore instance;
Keystore.load (FileInputStream, Password.tochararray ());
Load method of loading KeyStore, parameter is. P12 certificate, and other corresponding password;
Keymanagerfactory keymanagerfactory = keymanagerfactory.getinstance ("sunx509");
Get keymanagerfactory instance;
Keymanagerfactory.init (KeyStore, Password.tochararray ());
initialization method of Keymanagerfactory;

The second way is the project to read the certificate of the display of code, but only part of the code, please apply to their own code, as appropriate;


Later also introduced on the app side read the certificate code flow, please pay attention to this blog, welcome to share;

Our blog slogan is to share, encourage, mutual promotion;






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.