?
Key Generation
Generation of symmetric keys
??? Symmetric encryption uses symmetric encryption technology, which features that file encryption and decryption use the same key, that is, the encryption key can also be used as the decryption key. This method is called symmetric encryption algorithm in cryptography. symmetric encryption algorithms are simple and quick to use, have short keys, and are difficult to crack. Besides the Data Encryption Standard (DES ), another symmetric key encryption system is the International Data Encryption Algorithm (IDEA), which is better than DES encryption and has less requirements on computer functions. Idea encryption standards are used by PGP (pretty good privacy) systems.
// First import javax. crypto .*;
Secretkey key = NULL;
???? Try
{
// Specify the algorithm. Des is used here. If you want to use the blowfish algorithm, use getinstance ("Blowfish ")
// Bouncycastle basically supports all common standard Algorithms
Keygenerator keygen = keygenerator. getinstance ("des ");
// Specify the key length. The higher the length, the greater the encryption strength.
Keygen. INIT (56 );
// Generate a key
Key = keygen. generatekey ();
// Construct the output file. The directory here is dynamic. The directory is constructed based on the user name.
???? Objectoutputstream Keyfile = new objectoutputstream (New fileoutputstream
???????????? ("C: // security file //" + misclass. username + "// symmetric key // yhb. Des "));
???? Keyfile. writeobject (key );
???? Keyfile. Close ();
???? }
???? Catch (nosuchalgorithmexception E5)
{
// Generatekey () throws an exception
???? System. Out. Print ("No such algorithm ");
???? System. Exit (0 );
???? }
???? Catch (ioexception E4)
???? {
???? System. Out. Print ("error when generate the des key ");
???? System. Exit (0 );
}
Generation of asymmetric keys
In 1976, US scholars dime and Henman proposed a new key exchange protocol to solve the problems of public information transfer and key management, allowing communication Parties to exchange information on insecure media, securely reach an agreement on the key, which is the "public key system ". Compared with symmetric encryption algorithms, this method is also called asymmetric encryption algorithms ".
Unlike symmetric encryption algorithms, asymmetric encryption algorithms require two keys: public key and private key ). A public key is a pair of private keys. If a public key is used to encrypt data, only the corresponding private key can be used for decryption. If a private key is used to encrypt data, only the corresponding public key can be decrypted. Because encryption and decryption use two different keys, this algorithm is called asymmetric encryption algorithm.
// Key pair
??? Keypair keys = NULL;
??? Try
{
// Specify the algorithm
Keypairgenerator KPG = keypairgenerator. getinstance ("RSA ");
// Specify the length
??? KPG. initialize (1024 );
Keys = KPG. genkeypair ();
// Public Key
Byte [] key1 = keys. getpublic (). getencoded ();
// Private Key
??? Byte [] key2 = keys. getprivate (). getencoded ();
?
??? // Construct a public key file and write it into the Public Key
Fileoutputstream keyfile1 = new fileoutputstream
?????? ("C: // security file //" + misclass. username + "// asymmetric // your public/private key // yhb. Public ");
???? Keyfile1.write (key1 );
???? Keyfile1.close ();
??? // Construct the private key file and write it into the private key
??? Keyfile1 = new fileoutputstream
???? ("C: // security file //" + misclass. username + "// asymmetric // your public/private key // yhb. Private ");
??? Keyfile1.write (key2 );
??? Keyfile1.close ();
??? }
??? Catch (nosuchalgorithmexception E8)
{
// Algorithm exception
???? System. Out. Print ("No such algorithm ");
???? System. Exit (0 );
???? }
???? Catch (ioexception E9)
???? {
???? System. Out. Print ("error when generate the RSA key ");
???? System. Exit (0 );
???? }
?
The author is also known as hongsoft, research fields: 1) workflow-based BPM System Research 2) Java-based information security technology. Welcome to discuss Java-Related Issues hongbosoftware@163.com