Ixi
In actual applications, symmetric encryption is not enough. In more cases, symmetric encryption and asymmetric encryption are used in combination. asymmetric encryption (public key encryption) features slow speed, different keys for encryption and decryption. The encryption method is as follows:
* Public key encryption-Private Key decryption
* Private key encryption-Public Key decryption
Here we will show you the first method, with one left for your own test.
Steps:
Obtain the keypairgenerator Instance Object and call its generatekeypair () method to create the keypair object.
Call the getprivate and getpublic methods of the keypair object to obtain the privatekey object and the publickey object respectively.
Obtain the cipher instance object, and call its Init () method to specify the privatekey object or publickey object, and specify whether to encrypt or decrypt the object.
Call the dofinal () method of the cipher object to complete encryption or decryption.
The Code is as follows:
Package COM. study. security; import Java. io. bytearrayoutputstream; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream; import Java. security. key; import Java. security. keypair; import Java. security. keypairgenerator; import Java. security. privatekey; import Java. security. publickey; import javax. crypto. cipher; import javax. crypto. ciphe Rinputstream; /*** asymmetric encryption-public key encryption * public key encryption-Private Key decryption * private key encryption-Public Key decryption * @ author **/public class publicsecret {/***@ param ARGs */public static void main (string [] ARGs) throws exception {publicencrypt (); privatecencrypt ();}/*** public key encryption * @ throws exception */Private Static void publicencrypt () throws exception {cipher = cipher. getinstance ("RSA"); // generate key pair keypairgenerator = keypairgenerator. getinst Ance ("RSA"); keypair = keypairgenerator. generatekeypair (); publickey = keypair. getpublic (); privatekey = keypair. getprivate (); cipher. init (cipher. encrypt_mode, publickey); // The default encoding is used before encryption. If the encrypted data is in Chinese, garbled characters will appear, now, test byte [] Results = cipher. dofinal ("I'm going to be encrypted! ". Getbytes ("UTF-8"); // Save the key, because it is a public key encryption, decryption must use the private key, so here to save the generated private key, "key_public.key"); savedata (results, "key_pubdata.data"); system. out. println ("encrypted data:" + new string (results);}/*** Private Key decryption * @ throws exception */Private Static void privatecencrypt () throws exception {// read key and datakey privatekey = readkey ("key_public.key"); byte [] Results = readdata ("key_pubdata.data"); cipher = cipher. getinstan Ce ("RSA"); cipher. init (cipher. decrypt_mode, privatekey); // do not forget to add the encoding format, otherwise garbled system. out. println ("decrypted data:" + new string (cipher. dofinal (results), "UTF-8"); // ============================== another way to read data, for example, the following ============================== fileinputstream FD = new fileinputstream ("key_pubdata.data "); cipherinputstream CIS = new cipherinputstream (FCM, cipher); bytearrayoutputstream arrayoutputstream = new bytearrayoutputstream (); int Len = 0; Te [] DATA = new byte [1, 1024]; while (LEN = CIS. Read (data ))! =-1) {arrayoutputstream. write (data, 0, Len);} byte [] result2 = arrayoutputstream. tobytearray (); arrayoutputstream. close (); CIS. close (); system. out. println ("decrypted data (another way to read data):" + new string (result2, "UTF-8 "));} /*** Method for saving data * @ Param results * @ Param dataname * @ throws exception */public static void savedata (byte [] Results, string dataname) throws exception {fileoutputstream fosdata = new fileoutputstream (dataname ); Fosdata. write (results); fosdata. close ();}/*** Method for reading data * @ Param dataname * @ return byte [] * @ throws exception */public static byte [] readdata (string dataname) throws exception {fileinputstream fisdat = new fileinputstream (dataname); // read binary data bytearrayoutputstream arrayoutputstream = new bytearrayoutputstream (); int Len = 0; byte [] DATA = new byte [1024]; while (LEN = fisdat. read (data ))! =-1) {arrayoutputstream. write (data, 0, Len);} byte [] result = arrayoutputstream. tobytearray (); arrayoutputstream. close (); fisdat. close (); return result;}/*** Method for saving the key * @ Param keyname * @ throws exception */public static void savekey (Key key, string keyname) throws exception {fileoutputstream foskey = new fileoutputstream (keyname); objectoutputstream OOS = new objectoutputstream (foskey); OOS. writeobject (key); OOS. close (); foskey. close ();}/*** Method for reading the key * @ Param keyname * @ return key * @ throws exception */public static key readkey (string keyname) throws exception {fileinputstream fiskey = new fileinputstream (keyname); objectinputstream oiskey = new objectinputstream (fiskey); Key key = (key) oiskey. readobject (); oiskey. close (); fiskey. close (); Return key ;}}
Running result: