Java, C # bilingual version of AES Plus decryption example

Source: Internet
Author: User
Tags pkcs7

These days to find a serious can use things that is really difficult, on-line search a big pile, serious can use few, get, finally still have to rely on their own, happened to meet the need to AES plus decryption place, but also Java and C # Mutual decryption operation, here to do a memo

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

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

Package Nb.tmall.util;import Java.security.nosuchalgorithmexception;import Java.security.securerandom;import Javax.crypto.*;import Javax.crypto.spec.secretkeyspec;import sun.misc.*, @SuppressWarnings ("restriction") public Class Encryptutil {public static string Aesencrypt (String str, string key) throws Exception {if (str = = NULL | |        Key = = NULL) return null;        Cipher Cipher = cipher.getinstance ("aes/ecb/pkcs5padding");        Cipher.init (Cipher.encrypt_mode, New Secretkeyspec (Key.getbytes ("Utf-8"), "AES"));        byte[] bytes = cipher.dofinal (str.getbytes ("Utf-8"));    return new Base64encoder (). Encode (bytes); public static string Aesdecrypt (String str, string key) throws Exception {if (str = = NULL | | key = NULL) RET        Urn null;        Cipher Cipher = cipher.getinstance ("aes/ecb/pkcs5padding");        Cipher.init (Cipher.decrypt_mode, New Secretkeyspec (Key.getbytes ("Utf-8"), "AES"));        byte[] bytes = new Base64decoder (). Decodebuffer (str); Bytes = cipher.dofinal (bytes);    return new String (bytes, "Utf-8"); }}

C # Edition

Using system;using system.security.cryptography;using system.text;namespace csharp.util.security{//<summar Y>//AES encryption//</summary>//<param name= "str" ></param>//<para M name= "key" ></param>///<returns></returns> public static string Aesencrypt (String St R, string key) {if (string.            IsNullOrEmpty (str)) return null;            byte[] Toencryptarray = Encoding.UTF8.GetBytes (str);                System.Security.Cryptography.RijndaelManaged rm = new System.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);        Return convert.tobase64string (resultarray, 0, resultarray.length); }//<summary>//AES decryption//</summary>//<param name= "str" ></param&        Gt <param name= "key" ></param>///<returns></returns> public static string Aesdecry PT (String str, string key) {if (string.            IsNullOrEmpty (str)) return null;            byte[] Toencryptarray = convert.frombase64string (str);                System.Security.Cryptography.RijndaelManaged rm = new System.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);        Return Encoding.UTF8.GetString (resultarray); }    }}

Java, C # bilingual version of AES Plus decryption example

Related Article

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.