Implementation of Java AES256 encryption algorithm

Source: Internet
Author: User

If you want to AES256 encryption and decryption, you need to download Local_policy.jar and Us_export_policy.jar to replace the%java_home%/jre/lib/security two policy files in advance from the JAVA official website , Local_policy.jar and Us_export_policy. jar. The main purpose is to break the AES algorithm to only support the 128-bit limit. If not replaced, you may get the following error: *

Java.security.InvalidKeyException:Illegal Key Package com.jlins; Import java.io.UnsupportedEncodingException; Import Javax.crypto.Cipher; Import Javax.crypto.spec.IvParameterSpec; Import Javax.crypto.spec.SecretKeySpec; Import Com.jlins.util.Hex;      /** * java Aes256 encryption * * @author jlins * */public class Aes256encryptor {//Description key requires you to set the encryption and decryption of the Key,key involves security information, so there is no way to publish     private static final byte[] key = {};     Private static final String transform = "aes/cbc/nopadding";     Private static final String algorithm = "AES";     private static final Secretkeyspec KeySpec = new Secretkeyspec (key, algorithm); public static void Main (string[] args) throws Exception {String pwds[] = {"123", "0123456789012345", "012345678 90123456 "," 123 "," 123 "," 0123456789012345678 "," 012345678901234567890123456789 "," B "," 012345         6789012345 "," 01234567890123456 "," 012345678901234567 "}; String ivss[] = {"Test", "Test", "Test", "test0123456789012", "test01234567890123 "," Test "," Test "," a "," Test "," Test "," test "};          String rr[] = new String[ivss.length];             for (int i = 0; i < ivss.length; i++) {String en = Encrypt (pwds[i], ivss[i]);             String decy = Decrypt (en, ivss[i]);             Rr[i] = "[" + ivss[i] + "],[" + decy + "]-->[" + en + "]";            System.out.println (Rr[i]);            } System.out.println ("---------");             for (int i = 0; i < rr.length; i++) {System.out.println (rr[i]); }}/** */public static string decrypt (string phextext, String pIv) throws Exception {Cipher Cipher = Cipher.get         Instance (transform);         byte[] encryptedbytes = Hex.decode (Phextext);         Byte[] IV = CREATEIV (PIV);         Cipher.init (Cipher.decrypt_mode, KeySpec, new Ivparameterspec (iv));         byte[] decryptedbytes = cipher.dofinal (encryptedbytes); System.arraycopy (decryptedbytes, 0, encryptedbytes, 0, EncrypTedbytes.length);         string result = new String (encryptedbytes); return Result.trim (); }/** */public static string Encrypt (string pData, String pIv) throws Exception {Cipher Cipher = Cipher.getinst          ance (transform);          Byte[] IV = CREATEIV (PIV);          Cipher.init (Cipher.encrypt_mode, KeySpec, new Ivparameterspec (iv));          byte[] Output = cipher.dofinal (Paddingdata (pData));          byte[] encryptedcontent = new Byte[output.length];          System.arraycopy (output, 0, encryptedcontent, 0, encryptedcontent.length);          string result = new String (Hex.encode (encryptedcontent)). toUpperCase (); return result; }/** * Padded 16-bit integer times * * @param pData * @return * * * private static byte[] Paddingdata (String pData) {byte[] bytes = P        Data.getbytes ();        int length = BYTES.LENGTH/16;         if (length * < bytes.length) {length++;        } byte[] result = new byte[length * 16]; System.arraycopy (bytes, 0, result, 0, Bytes.length);         for (int i = bytes.length; i < result.length; i++) {result[i] = 0x00;         } return result; }/** * Initialize vector to 16 bit * */private static byte[] Createiv (String pIv) throws Unsupportedencodingexception {byte        [] bytes = Piv.getbytes ("Us-ascii");        int length = BYTES.LENGTH/16;        if (length * < bytes.length) {length++;        } byte[] result = new BYTE[16];        System.arraycopy (bytes, 0, result, 0, bytes.length > 16:bytes.length);       for (int i = bytes.length; i < result.length; i++) {result[i] = 0x00; } return result; } }

Original address: http://www.itmmd.com/201411/98.html
The article is published by the Android Development and distribution, reproduced must be marked by the source. .

Implementation of Java AES256 encryption algorithm

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.