First, the encryption process explained
Prerequisite: The sender is a and the receiving party is b
Keep in mind: RSA is asymmetric, and AES is symmetric-encrypted. Symmetric encryption, belonging to the traditional encryption technology, encryption and decryption of the key are the same, AES key length of 128, 192, 2,563 kinds. Asymmetric encryption belongs to the current encryption technology, encryption and decryption keys are different, one is the public key, one is the private key, commonly used for digital signatures and encryption keys.
1. b generates RSA key and sends the corresponding RSA public key to a
2, a obtains the RSA public key, generates an AES key randomly, encrypts the AES key with RSA public key and the RSA algorithm, writes the file header, encrypts the data with the AES key and the AES algorithm, writes the data to the file tail;
3, B receives data, uses RSA key and RSA decryption algorithm, decrypts the head, obtains the AES key, decrypts the remaining data using the AES key and the AES algorithm.
Second, the Code implementation
1. RSA Encryption
1String Mingwen = "haha encryption that's what it is";2 3 4 5 byte[] private_key;6 7 byte[] Private_key_encode=NULL ;8 9 byte[] mingwen_encrypt=NULL;;Ten One byte[] Mingwen_encrypt_encode=NULL; A - - the Try { - -Rsautil Rsautil =Rsautil.creatersautilwithrandomkeys (); - + - + A at //the private key after encryption - -Private_key =rsautil.getprivatekeybytes (); - -Private_key_encode = Base64.encode (Private_key, 1); - in - to //data after RSA and Base64 encryption + -Mingwen_encrypt =Rsautil.encrypt (Mingwen.getbytes ()); the *Mingwen_encrypt_encode = Base64.encode (Mingwen_encrypt, 1); $ Panax Notoginseng - the}Catch(Exception E2) { + A //TODO auto-generated Catch block the + e2.printstacktrace (); - $ } $ - - the Try { - Wuyi //Rsautil.genkeypair (); the -}Catch(Exception E1) { Wu - //TODO auto-generated Catch block About $ e1.printstacktrace (); - -}
View Code
2. RSA decryption
1 //BASE64 decoding of RSA private keys2 3 byte[] private_key_1 =base64.decode (private_key_encode,1);4 5 //example of generating Rsautil6 7Rsautil Rsautil_1 =NewRsautil (NULL, Private_key_1,NULL);8 9 Ten //BASE64 decryption of ciphertext One A byte[] Miwen_encrypt = Base64.decode (Mingwen_encrypt_encode, 1); - - the //decrypt ciphertext into RSA - - byte[] miwen=Rsautil_1.decrypt (miwen_encrypt); - + - + Try { A atLOG.E (Tag, "Mingwen is" +NewString (Miwen, "Utf-8")); - - - -}Catch(unsupportedencodingexception e) { - in //TODO auto-generated Catch block - to e.printstacktrace (); + -}
View Code
3. AES Decryption
1 //creating an instance of AES2 3Aesutil Aesutil =NewAesutil ("1234567812345678". GetBytes ());4 5 6 7 //Base64 Decoding instructions: Inside the parameters are first AES encryption, and then BASE648 9 byte[] Encdata = Base64.decode ("OO7KHHUMMGSW6DFPKU83C8BLEJDVUDBHF2II4C0ET5HOOIL5KCLQR7GJ/RP04FSJ", 0);Ten One A - //AES Decoding - the byte[] Decdata =Aesutil.decrypt (encdata); - - - + Try { - +Mmarque.settext (NewString (Decdata, "Utf-8")); A at}Catch(unsupportedencodingexception e) { - - e.printstacktrace (); - - Mmarque.settext (E.getmessage ()); - in}*/
View Code
24. AES RSA Encryption Processing record