Java, C # bilingual version of AES Plus decryption example

Source: Internet
Author: User
Tags pkcs7

Here the use of encryption and decryption using the Base64 transcoding method, ECB mode, pkcs5padding fill, password must be 16 bits, otherwise it will error!

Mode: The ECB of Java corresponds to C # System.Security.Cryptography.CipherMode.ECB

Fill method: Java pkcs5padding corresponds to C#SYSTEM.SECURITY.CRYPTOGRAPHY.PADDINGMODE.PKCS7

Java and C # version of the encryption and decryption is interoperability, that is, can be decrypted with each other, the code explicitly specifies the use of UTF-8, there is need for other coding methods, please expand themselves

Java Edition

 PackageNb.tmall.util;Importjava.security.NoSuchAlgorithmException;ImportJava.security.SecureRandom;ImportJavax.crypto.*;ImportJavax.crypto.spec.SecretKeySpec;ImportSun.misc.*; @SuppressWarnings ("Restriction") Public classEncryptutil { Public StaticString Aesencrypt (String str, string key)throwsException {if(str = =NULL|| Key = =NULL)return NULL; Cipher Cipher= Cipher.getinstance ("aes/ecb/pkcs5padding"); Cipher.init (Cipher.encrypt_mode,NewSecretkeyspec (Key.getbytes ("Utf-8"), "AES")); byte[] bytes = cipher.dofinal (str.getbytes ("Utf-8")); return NewBase64encoder (). Encode (bytes); }     Public StaticString Aesdecrypt (String str, string key)throwsException {if(str = =NULL|| Key = =NULL)return NULL; Cipher Cipher= Cipher.getinstance ("aes/ecb/pkcs5padding"); Cipher.init (Cipher.decrypt_mode,NewSecretkeyspec (Key.getbytes ("Utf-8"), "AES")); byte[] bytes =NewBase64decoder (). Decodebuffer (str); Bytes=cipher.dofinal (bytes); return NewString (Bytes, "Utf-8"); }}

C # Edition

usingSystem;usingSystem.Security.Cryptography;usingSystem.Text;namespacecsharp.util.security{/// <summary>        ///AES Encryption/// </summary>        /// <param name= "str" ></param>        /// <param name= "key" ></param>        /// <returns></returns>         Public Static stringAesencrypt (stringStrstringkey) {            if(string. IsNullOrEmpty (str))return NULL; Byte[] Toencryptarray=Encoding.UTF8.GetBytes (str); System.Security.Cryptography.RijndaelManaged RM=NewSystem.Security.Cryptography.RijndaelManaged {Key=Encoding.UTF8.GetBytes (key), Mode=System.Security.Cryptography.CipherMode.ECB, Padding=System.Security.Cryptography.PaddingMode.PKCS7}; System.Security.Cryptography.ICryptoTransform Ctransform=RM.            CreateEncryptor (); Byte[] Resultarray= Ctransform.transformfinalblock (Toencryptarray,0, toencryptarray.length); returnConvert.tobase64string (Resultarray,0, resultarray.length); }        /// <summary>        ///AES Decryption/// </summary>        /// <param name= "str" ></param>        /// <param name= "key" ></param>        /// <returns></returns>         Public Static stringAesdecrypt (stringStrstringkey) {            if(string. IsNullOrEmpty (str))return NULL; Byte[] Toencryptarray=convert.frombase64string (str); System.Security.Cryptography.RijndaelManaged RM=NewSystem.Security.Cryptography.RijndaelManaged {Key=Encoding.UTF8.GetBytes (key), Mode=System.Security.Cryptography.CipherMode.ECB, Padding=System.Security.Cryptography.PaddingMode.PKCS7}; System.Security.Cryptography.ICryptoTransform Ctransform=RM.            CreateDecryptor (); Byte[] Resultarray= Ctransform.transformfinalblock (Toencryptarray,0, toencryptarray.length); returnEncoding.UTF8.GetString (resultarray); }    }}

Java, C # bilingual version of AES Plus decryption example

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.