JAVA AES file plus decryption

Source: Internet
Author: User

AES Plus decryption algorithm, the code is as follows:

/** * Created by Hua on 2017/6/30.*/import Javax.crypto.cipher;import Javax.crypto.spec.ivparameterspec;import javax.crypto.spec.SecretKeySpec; public class AESUtil1 {//initialization vector, AES 16-bitprivate static final String IV ="Abcdefghijk1mnop"; //Binary conversion to 16 binarypublic static String Parsebyte2hexstr (byte[] buf) {StringBuffer SB=new StringBuffer ();  for(inti =0; i < buf.length; i++) {String hex= Integer.tohexstring (Buf[i] &0xFF); if(hex.length () = =1) {hex='0'+Hex;        } sb.append (hex);    } return sb.tostring (); }    //Converting 16 binary to binarypublic staticbyte[] Parsehexstr2byte (String hexstr) {if(Hexstr.length () <1) {returnNULL; }        byte[] result = newbyte[Hexstr.length ()/2];  for(inti =0; I < hexstr.length ()/2; i++) {            intHigh = Integer.parseint (hexstr.substring (i *2I2+1), -); intLow = Integer.parseint (hexstr.substring (i *2+1I2+2), -); Result[i]= (byte) (High * -+Low );    } return result; }    //EncryptPublic static string encrypt (string content, String KeyWord) throws Exception {try {Secretkeys Pec key= New Secretkeyspec (Keyword.getbytes (),"AES"); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding");            Cipher.init (Cipher.encrypt_mode, Key, New Ivparameterspec (Iv.getbytes ())); byte[] EncryptedData = Cipher.dofinal (Content.getbytes ("UTF-8"));        Return Parsebyte2hexstr (EncryptedData); } catch (Exception e) {throw new Exception ("Encryption failed"); }    }    //decryptionPublic static string decrypt (string content, String KeyWord) throws Exception {byte[] Contentbytes =parsehexstr2byte (content); try {secretkeyspec key= New Secretkeyspec (Keyword.getbytes (),"AES"); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding");            Cipher.init (Cipher.decrypt_mode, Key, New Ivparameterspec (Iv.getbytes ())); byte[] result =cipher.dofinal (contentbytes); return new String (result,"UTF-8"); } catch (Exception e) {throw new Exception ("Decryption failed"); }} public static void Main (string[] args) throws Exception {String content="Merson Snow Three points white, snow but lose plum a section of incense. "; String Password="0123456789abcdef";//AES-128-CBC encryption mode is used here, key needs to be 16-bitSystem.out.println ("before encryption:"+content); String Encryptresult=aesutil1.encrypt (content, password); System.out.println ("after encryption:"+Encryptresult); String Decryptresult=Aesutil1.decrypt (Encryptresult,password); System.out.println ("after decryption:"+Decryptresult); }}

Run the output as follows:

Before encryption: Merson Snow three points white, but the snow loses plum a piece of incense. After encryption: a5856355ef87955f0f112970495502ebe61b9bce4998bc777c9b383d608c1ba2da97f377914354d2e0d6500d1dac786aa69e593a67a1703038962 4223C5EE8FD after decryption: Merson Snow three points white, snow but lose plum a section of incense.

JAVA AES file plus decryption

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.