The example of this article tells the way Java generates asymmetric encryption public and private keys. Share it for everyone for your reference. The specifics are as follows:
Asymmetric encryption is very suitable for the secret communication between multiple clients and servers. The client uses the same public key to encrypted it. This public key cannot be decrypted in reverse. Decolling, this is a clear encryption transmission.
Asymmetric encryption also has its congenital disadvantages. The speed of encryption and decryption slowly restricts its play. If you have a lot of text, you need to be encrypted and transmitted. It is recommended that you distribute the symmetry 'key' to the client through asymmetric encryption. , Timely update the symmetry 'key'.
Keyrsa.java is as follows:
Import java.io.*; Import java.security.*; Import javax.crypto.*; Import javax.crypto.spec.*;/***RSA asymmetric encryption public key and private key*/place class. s Keyrsa {Private Keypairgenerator KPG = NULL; Private Keypair KP = NULL; Private Publickey Public_key = Null; Private PrivateKekey_Key = NULL; Private fileoutstream public_file_out = null; Private ObjectPutOutstream Public_Object_out = NULL; Private FileoutStream Private_file_out = Ull; Private ObjectPutstream Private_Object_out = NULL; / / *** Construction function* @param in specified key length (value range: 512 ~ 2048)* @throws nosuochalgorithmexception*/ Public Keyrsa (int in, String Address) Throws AlgorithMexception, FILENOTFOUNDEXCEPTION, IOEXCEPTION {kpg = Keypairgenrator.getInstance ("RSA"); // Create the 'Capital of the "generator kpg.initialize (in); // Specify the length of the key length (value range: 512 ~ 2048) kp = kpg.genkeypair (); The key is to ', which contains a public and a private information public_key = kp.getpublic (); // Get the public spoon private_key = kp.getprivate (); // Get private spoon // Save public_file_out = New FileoutPutstream (address + "/public_key.dat"); Public_object_out = New ObjectPutstream (PUBLIC_FILE_OUT); bky (public_key); // Save private spoons private_file_out = new fileoutPutstream (address + "/private_key.dat"); private_object_out = New ObjectPutStream (Private_file_out); Private_object_out.writeObject (Private_key);} Public Static Void Main (String [] args) {Try {NEW Keyrsa (1024, "c:/key_rsa");} Catch NoSuchalgorithmexception EX) {}}}}It is hoped that this article is helpful to everyone's Java program design.