AES encryption for Java and. NET Interoperability

Source: Internet
Author: User
Tags base64 pkcs7

When AES encryption is used. If cross-platform issues are involved. The notification encounters an issue where. NET is inconsistent with the Java encryption results. A solution has been found to be recorded.

 public class Aeshelper {public static string Encry (string text, string key) {String IV = Ke            Y if (key. Length >) {//IV for Merchant MD5 key after 16-bit IV = key. Substring (key.                LENGTH-16); The key for RES is the first 16 bits key of the merchant MD5 key = key.            Substring (0, 16);        } return Encodeaes (text, KEY,IV); }///&LT;SUMMARY&GT;AES encryption </summary>//<param name= "text" > Clear text </param>///< param name= "key" > key, String length 16 </param>//<param Name= "IV" > Offset, String length 16 </param>//                /<returns> redaction </returns> public static string Encodeaes (string text, string key, String iv) {            RijndaelManaged rijndaelcipher = new RijndaelManaged ();            Rijndaelcipher.mode = CIPHERMODE.CBC;            rijndaelcipher.padding = PADDINGMODE.PKCS7;            rijndaelcipher.keysize = 128; Rijndaelcipher.blocksize = 128;            byte[] pwdbytes = System.Text.Encoding.UTF8.GetBytes (key);            byte[] keybytes = new BYTE[16];            int len = pwdbytes.length;            if (len > keybytes.length) len = keybytes.length;            System.Array.Copy (Pwdbytes, Keybytes, Len);            Rijndaelcipher.key = keybytes;            RIJNDAELCIPHER.IV = Encoding.UTF8.GetBytes (IV);            ICryptoTransform transform = Rijndaelcipher.createencryptor ();            byte[] plaintext = Encoding.UTF8.GetBytes (text); Byte[] Cipherbytes = transform.            TransformFinalBlock (plaintext, 0, plaintext.length);        Return convert.tobase64string (cipherbytes); }///<summary>aes decryption </summary>//<param name= "text" > redaction </param>//< param name= "key" > key, String length 16 </param>//<param Name= "IV" > Offset, String length 16 </param>// /<returns> Clear Text </returns> public static string Decodeaes (string text, string key, String iv) {RijndaelManaged rijndaelcipher = new Rijn            Daelmanaged ();            Rijndaelcipher.mode = CIPHERMODE.CBC;            rijndaelcipher.padding = PADDINGMODE.PKCS7;            rijndaelcipher.keysize = 128;            rijndaelcipher.blocksize = 128;            byte[] EncryptedData = convert.frombase64string (text);            byte[] pwdbytes = System.Text.Encoding.UTF8.GetBytes (key);            byte[] keybytes = new BYTE[16];            int len = pwdbytes.length;            if (len > keybytes.length) len = keybytes.length;            System.Array.Copy (Pwdbytes, Keybytes, Len);            Rijndaelcipher.key = keybytes;            RIJNDAELCIPHER.IV = Encoding.UTF8.GetBytes (IV);            ICryptoTransform transform = Rijndaelcipher.createdecryptor (); Byte[] plaintext = transform.            TransformFinalBlock (EncryptedData, 0, encrypteddata.length); Return Encoding.UTF8.GetString (plaintEXT); }    }

The following are Java code

public class Aesutil {//Encrypt public static string Encry (string content, String key) throws Exception {string IV = Key;if (ke  Y.length () >) {//IV for Merchant MD5 key after 16-bit IV = key.substring (Key.length ()-+);//Res key is the first 16-bit key of the merchant MD5 key = key.substring (0, 16);} Return EncryptData (content, key, IV);}  Encrypt public static string Desencry (string content, String key) throws Exception {string IV = Key;if (Key.length () > 16) {//IV is the merchant MD5 key after 16-bit IV = key.substring (Key.length ()-+);//Res key is the first 16-bit key for merchant MD5 keys = key.substring (0, 16);} Return Decryptdata (content, key, IV);} /** * AES Encryption * * @param data * @return */public static string EncryptData (string data, String key, String IV) throws Excep tion {try {Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding"); byte[] databytes = data.getbytes ("UTF-8"); int Plaintextlength = databytes.length;//if (plaintextlength% blockSize! = 0) {//Plaintextlength = Plaintextlength + (block Size-(plaintextlength//% blockSize));//}byte[] plaintext = new Byte[plaintextleNgth]; System.arraycopy (databytes, 0, plaintext, 0, databytes.length); Secretkeyspec Keyspec = new Secretkeyspec (Key.getbytes (), "AES"), ivparameterspec ivspec = new Ivparameterspec ( Iv.getbytes ()); Cipher.init (Cipher.encrypt_mode, Keyspec, Ivspec); byte[] encrypted = cipher.dofinal (plaintext); return new String (Base64.encodebase64 (encrypted));} catch (Exception e) {throw e;}} /** * AES Decryption * * @param data * ciphertext * @return */public static string Decryptdata (string data, string key, String IV) throws Exception {try {byte[] encrypted1 = base64.decodebase64 (Data.getbytes ("UTF-8")); Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding"); Secretkeyspec Keyspec = new Secretkeyspec (Key.getbytes (), "AES"), ivparameterspec ivspec = new Ivparameterspec ( Iv.getbytes ()); Cipher.init (Cipher.decrypt_mode, Keyspec, Ivspec); byte[] Original = cipher.dofinal (encrypted1); String originalstring = new String (Original, "UTF-8"); return originalstring;} catch (Exception e) {throw e;}} public static void Main (STRIng[] args) throws Exception {String a = encry ("Test Electronics Co., Ltd.", "XJQ4RSY0ZCO937VK7VDRZFD7R40RQ5PG"); System.out.println (a);}}

  

AES encryption for Java and. NET Interoperability

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.