AES encryption and decryption Java application

Source: Internet
Author: User

AES Full name Advanced encryptionstandard, high encryption algorithm, more secure, can replace DES.

Aes:

 PackageCom.blog.d201706.encrypt;ImportJavax.crypto.Cipher;ImportJavax.crypto.spec.SecretKeySpec;ImportJava.security.Key; Public classAes {/*** Add decryption key*/    Private FinalKey KeySpec; /*** Constructor Function *@paramKey*/     PublicAes (String key) {KeySpec=NewSecretkeyspec (Key.getbytes (), "AES"); }    /*** Encryption *@paramSTR *@return     */     Publicstring Encryt (String str) {//initializes the Cipher object according to the key, Encrypt_mode represents the encryption mode        Try{Cipher C= Cipher.getinstance ("AES");            C.init (Cipher.encrypt_mode, KeySpec); byte[] src =str.getbytes (); //encryption, results saved into Cipherbyte            byte[] Cipherbyte =c.dofinal (SRC); returnparsebyte2hexstr (Cipherbyte); } Catch(Exception e) {e.printstacktrace (); }        return NULL; }    /*** Decryption *@paramDecodestr *@return     */     Publicstring Decrypt (string decodestr) {//initializes the Cipher object according to the key, Decrypt_mode represents the encryption mode        Try {            if(NULL= = Decodestr)return NULL; byte[] Buff =Parsehexstr2byte (DECODESTR);            Cipher C; C= Cipher.getinstance ("AES");            C.init (Cipher.decrypt_mode, KeySpec); byte[] Cipherbyte =c.dofinal (Buff); return NewString (Cipherbyte); } Catch(Exception e) {e.printstacktrace (); }        return NULL; }    /*** Convert binary to 16 binary * *@paramBUF *@return     */     Public StaticString Parsebyte2hexstr (bytebuf[]) {StringBuffer SB=NewStringBuffer ();  for(inti = 0; i < buf.length; i++) {String hex= Integer.tohexstring (Buf[i] & 0xFF); if(hex.length () = = 1) {hex= ' 0 ' +Hex;        } sb.append (Hex.touppercase ()); }        returnsb.tostring (); }    /*** Convert 16 binary to binary * *@paramHexstr *@return     */     Public Static byte[] Parsehexstr2byte (String hexstr) {if(Hexstr.length () < 1)return NULL; byte[] result =New byte[Hexstr.length ()/2];  for(inti = 0; I < Hexstr.length ()/2; i++) {            intHigh = Integer.parseint (Hexstr.substring (i * 2, I * 2 + 1), 16); intLow = Integer.parseint (Hexstr.substring (i * 2 + 1, I * 2 + 2), 16); Result[i]= (byte) (High * 16 +Low ); }        returnresult; }}
View Code

Mainaes:

 PackageCom.blog.d201706.encrypt; Public classMainaes { Public Static voidMain (string[] args)throwsException {AES AES=NewAes ("1234567890123456"); String msg= "Test text: Today Week 5" +System.currenttimemillis (); String encontent=Aes.encryt (msg); String decontent=Aes.decrypt (encontent); System.out.println ("Clear Text is:" +msg); System.out.println ("After encryption:" +encontent); System.out.println ("After decryption:" +decontent); }}
View Code

AES encryption and decryption Java application

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.