AES encryption, decryption (Linux, window encryption and decryption effect consistent, support Chinese)

Source: Internet
Author: User

Transferred from: http://sunfish.iteye.com/blog/2169158

Import Java.io.unsupportedencodingexception;import Java.security.invalidkeyexception;import Java.security.nosuchalgorithmexception;import Java.security.securerandom;import javax.crypto.BadPaddingException ; Import Javax.crypto.cipher;import Javax.crypto.illegalblocksizeexception;import Javax.crypto.keygenerator;import Javax.crypto.nosuchpaddingexception;import Javax.crypto.secretkey;import Javax.crypto.spec.secretkeyspec;import            Org.apache.axis.encoding.base64;public class AES {private static int length=128;/** * Encrypt * * @param content * What you need to encrypt * @param password * Encryption password * @return * @throws nosuchalgorithmexception * @throws nosuchpaddingexception * @throws unsupportedencodingexception * @throws invalidkeyexception * @throws badpaddingexception * @throws illegalblock Sizeexception */private Static byte[] Encrypt (string content, string password) throws Exception {Keygenerator KGen = KeyGen                Erator.getinstance ("AES"); SecureRandom securerandom = SecureRandom.getinstance ("sha1prng"); Securerandom.setseed (Password.getbytes ()); Kgen.init (length, securerandom); Secretkey Secretkey = Kgen.generatekey (); byte[] Encodeformat = secretkey.getencoded (); Secretkeyspec key = new Secretkeyspec (Encodeformat, "AES"); Cipher Cipher = cipher.getinstance ("AES");//Create cipher byte[] bytecontent = content.getbytes ("Utf-8"); Cipher.init ( Cipher.encrypt_mode, key);//Initialize byte[] result = cipher.dofinal (bytecontent); return result; Encrypt}/** * Decrypt * * @param content * to decrypt contents * @param password * Decryption key * @return */private static byte[                 ] Decrypt (byte[] content, String password) throws Exception {Keygenerator KGen = keygenerator.getinstance ("AES");                   SecureRandom securerandom = securerandom.getinstance ("sha1prng"); Securerandom.setseed (Password.getbytes ()); Kgen.init (length, securerandom); Secretkey Secretkey = Kgen.generatekey (); byte[] Encodeformat = secretkey.getencoded (); Secretkeyspec key = new Secretkeyspec (EnCOdeformat, "AES"); Cipher Cipher = cipher.getinstance ("AES");//Create cipher Cipher.init (Cipher.decrypt_mode, key);//Initialize byte[] result = Cipher.dofinal (content); return result; Encrypt}///**//* Convert binary to 16 binary//*//* @param buf//* @return//*///public static String Parseb YTE2HEXSTR (Byte buf[]) {//stringbuffer sb = new StringBuffer (),//for (int i = 0; i < buf.length; i++) {//string hex = i Nteger.tohexstring (Buf[i] & 0xFF);//if (hex.length () = = 1) {//hex = ' 0 ' + hex;//}//sb.append (hex.touppercase ());//}/ /return sb.tostring ();//}/////**//* Convert 16 binary to binary//*//* @param hexstr//* @return//*///public static byte[] Parsehexstr 2Byte (String hexstr) {//if (Hexstr.length () < 1)//return null;//byte[] result = new Byte[hexstr.length ()/2];//for (in t i = 0; I < Hexstr.length ()/2; i++) {//int high = Integer.parseint (Hexstr.substring (i * 2, I * 2 + 1), and//int low = Integer.parseint (Hexstr.substring ( I * 2 + 1, I * 2 + 2),//16);//result[i] = (byte) (high * + low);//}//return result;//}/** * Encryption * * @param content * requires encrypted contents * @param password * encrypted password * @return */p Ublic Static byte[] Encrypt2 (string content, string password) {try {secretkeyspec key = new Secretkeyspec (password.getbyte S (), "AES"); Cipher Cipher = cipher.getinstance ("aes/ecb/nopadding"); byte[] bytecontent = content.getbytes ("Utf-8"); Cipher.init ( Cipher.encrypt_mode, key);//Initialize byte[] result = cipher.dofinal (bytecontent); return result; Encryption} catch (NoSuchAlgorithmException e) {e.printstacktrace ();} catch (Nosuchpaddingexception e) {e.printstacktrace ();} catch (InvalidKeyException e) {e.printstacktrace ();} catch (Unsupportedencodingexception e) {e.printstacktrace ();} catch (Illegalblocksizeexception e) {e.printstacktrace ();} catch (Badpaddingexception e) {e.printstacktrace ();} return null;} public static string Encrypt2str (string content, string password) throws Exception {byte[] Encryptresult = Encrypt (content , password); return Base64.encode (Encryptresult);} Publicstatic string Decrypt2str (string content, string password) throws Exception {byte[] Decryptresult = Decrypt (base64.decode (content), password); return new String (Decryptresult, "UTF-8");} public static void Main (string[] args) throws Exception {String content = "T sun est"; String password = "12345678";//Encryption System.out.println ("Before encryption:" + content); String tt4 = encrypt2str (content, password); System.out.println (new String (TT4));//Decrypt string d = decrypt2str (tt4, password); System.out.println ("After decryption:" + D);//Before encryption: T Sun est//bpf0jyjdj/pvharf66+oma==//after decryption: T Sun est}}

AES encryption, decryption (Linux, window encryption and decryption effect consistent, support Chinese)

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.