Get the Public key object idea:
1, first get the public key pair
Instantiates a Keypairgenerator object with the specified algorithm
Keypairgenerator keygen = keypairgenerator.getinstance ("DSA");
Initialize Keypairgenerator
Keygen.initialize (1024);
Generating KeyPair objects
KeyPair keys = Keygen.genkeypair ();
2. Get the public key byte array
byte[] publickeybytes = Keys.getpublic (). getencoded ();
Instantiating a X509encodedkeyspec object
3. Construct the public key object using the public key byte array.
X509encodedkeyspec KeySpec = new X509encodedkeyspec (publickeybytes);
Instantiates a Keyfactory object with the specified algorithm
Keyfactory keyfactory = keyfactory.getinstance ("DSA");
Get PublicKey Object
Public final PublicKey generatepublic (KeySpec KeySpec) generates a key object based on the provided key specification (keying material).
PublicKey PublicKey = Keyfactory.generatepublic (KeySpec);
The private key creation process is similar to the public key
Algorithmparametergenerator Object creation Process
1. Call the Algorithmparametergenerator.getinstance () method:
1) Call Security.getimpl (algorithm, "Algorithmparametergenerator", (String) null) inside Method getinstance (), and the method returns an array of objects
2) Create a new Algorithmparametergenerator object in the calling constructor with parameters to return
New Algorithmparametergenerator ((ALGORITHMPARAMETERGENERATORSPI) objs[0], (Provider) objs[1], algorithm);
2, initializes this parameter generator for a certain size.
Public final void init (int size)
GetInstance Source:
public static Algorithmparametergenerator getinstance (String algorithm,
String provider)
Throws NoSuchAlgorithmException, Nosuchproviderexception
{
if (Provider = = NULL | | provider.length () = = 0)
throw new IllegalArgumentException ("Missing provider");
object[] Objs = Security.getimpl (algorithm,
"Algorithmparametergenerator",
Provider);
return new Algorithmparametergenerator
(ALGORITHMPARAMETERGENERATORSPI) objs[0], (Provider) objs[1],
algorithm);
}
The internal implementation structure of the getinstance () method of Algorithmparameters, Algorithmparametergenerator, etc. is basically the same.
X509encodedkeyspec for building a public key specification, Pkcs8encodedkeyspec for building a private key specification
The Secretkeyspec interface is the Keyspec implementation class for building secret key specifications
Security Learning Note 1