AES symmetric encryption algorithm encryption \ Decryption Instance

Source: Internet
Author: User
Tags decrypt

Package Com.soufun.com;import Java.io.unsupportedencodingexception;import java.security.InvalidKeyException; Import Java.security.nosuchalgorithmexception;import Java.security.securerandom;import Java.util.Date;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;/** * @author WHD Data August 16, 2015 */public class Aesutil {private static final String AES = "AES";p rivate static final String utf8= "UTF-8"; static Keygenerator KGen =null;static{try {kgen= Keygenerator.getinstance (AES);} catch (NoSuchAlgorithmException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} /* * @param content: * @param password: */private static byte[] Encrypt (string content, string password) {try {//Use static code block to Generate Keygenerator object//keygenerator KGen = Keygenerator.getinstance (AES);//Use 128-bit kgen.init (+, new SEcurerandom (Password.getbytes ())); Secretkey Secretkey = Kgen.generatekey (); byte[] Encodeformat = secretkey.getencoded (); Secretkeyspec key = new Secretkeyspec (Encodeformat, AES);//Cipher The object actually completes the cryptographic operation cipher cipher = Cipher.getinstance (AES);// Encrypt content to encode byte[] bytecontent = content.getbytes (UTF8);//use Key to initialize Cipher object Cipher.init (Cipher.encrypt_mode, key);// Formally perform cryptographic operations byte[] result = cipher.dofinal (bytecontent); return result;} 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;} /* * @param content: * @param password: */private static byte[] Decrypt (byte[] content, String password) {try {//Use static code block to Generate Keygenerator object//keygenerator KGen = Keygenerator.getinstance (AES);//Use 128-bit kgen.init (+, new SecurerandoM (Password.getbytes ())); Secretkey Secretkey = Kgen.generatekey (); byte[] Encodeformat = secretkey.getencoded (); Secretkeyspec key = new Secretkeyspec (Encodeformat, AES);//Cipher The object actually completes the cryptographic operation cipher cipher = Cipher.getinstance (AES);// Initialize the Cipher object with a key Cipher.init (Cipher.decrypt_mode, key);//formally perform decryption operation byte[] result = cipher.dofinal (content); return result ;} catch (NoSuchAlgorithmException e) {e.printstacktrace ();} catch (Nosuchpaddingexception e) {e.printstacktrace ();} catch (InvalidKeyException e) {e.printstacktrace ();} catch (Illegalblocksizeexception e) {e.printstacktrace ();} catch ( Badpaddingexception e) {e.printstacktrace ();} return null;} /** * binary-"hex conversion * @param buf * @return */private static String parsebyte2hexstr (byte buf[]) {stringbuffer sb = new Stri Ngbuffer (); for (int i = 0; i < buf.length; i++) {String hex = integer.tohexstring (Buf[i] & 0xFF); if (hex.length () = = 1) {hex = ' 0 ' + hex;} Sb.append (Hex.touppercase ());} return sb.tostring ();} /** * hex-Binary conversion * @param hexstr * @retuRN */private static byte[] Parsehexstr2byte (String hexstr) {if (Hexstr.length () < 1) {return null;} Byte[] result = new Byte[hexstr.length ()/2];for (int i = 0; i < hexstr.length ()/2; i++) {int high = Integer.parsein T (hexstr.substring (i * 2, I * 2 + 1), int low = Integer.parseint (Hexstr.substring (i * 2 + 1, I * 2 + 2), +); Result[i] = (byte) (high * + low);} return result;} public static void Main (string[] args) throws Unsupportedencodingexception {long begin=new Date (). GetTime (); String content = "aaades encryption test"; String password = "12345678DD";//Encryption System.out.println ("Before encryption:" + content); byte[] Encryptresult = Encrypt (content, password); String encryptresultstr = Parsebyte2hexstr (Encryptresult); SYSTEM.OUT.PRINTLN ("After encryption:" + ENCRYPTRESULTSTR);//decryption byte[] Decryptfrom = Parsehexstr2byte (ENCRYPTRESULTSTR); byte[] Decryptresult = Decrypt (decryptfrom, password);//Decrypt the contents to decode string result = new String (Decryptresult, UTF8); System.out.println ("After decryption:" + result); Long end= new Date (). GetTime (); SysteM.out.println (End-begin);}} 

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

AES symmetric encryption algorithm encryption \ Decryption Instance

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.