Java Aes256 Encryption Algorithm Implementation, javaaes256
If you want to encrypt and decrypt the AES256 bits, download local_policy.jar and US_export_policy.jar from the java official website to replace the two policy files: % JAVA_HOME %/jre/lib/security, local_policy.jar and US_export_policy. The main purpose is to break through the AES algorithm's limit that only 128 bits are supported. If not replaced, the following error may be returned :*
Java. security. invalidKeyException: Illegal key package com. july; import java. io. unsupportedEncodingException; import javax. crypto. cipher; import javax. crypto. spec. ivParameterSpec; import javax. crypto. spec. secretKeySpec; import com. july. util. hex;/*** java Aes256 encryption ** @ author jlins **/public class Aes256Encryptor {// you need to set the key for encryption and decryption by yourself, the key involves security information, so private static final byte [] ke cannot be published here. Y = {}; 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", "01234567890123456", "123 ", "123", "0123456789012345678", "012345678901234567890123456789", "B", "012 3456789012345 "," 01234567890123456 "," 012345678901234567 "}; String ivss [] = {" test "," test0123456789012 "," test01234567890123 ", "test", "test", "a", "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. getInstance (transform); byte [] encryptedBytes = Hex. decode (pHexText); byte [] iv = createIV (pIv); cipher. init (Cipher. DECRYPT_MODE, keySpec, new IvParameterSp Ec (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. getInstance (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;}/*** fill in the 16-bit integer multiple ** @ param pData * @ return */private static byte [] paddingData (String pData) {B Yte [] bytes = pData. getBytes (); int length = bytes. length/16; if (length * 16 <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 the vector to 16 bits **/private static byte [] createIV (String pIv) throws UnsupportedEncodingException {byte [] Bytes = pIv. getBytes ("US-ASCII"); int length = bytes. length/16; if (length * 16 <bytes. length) {length ++;} byte [] result = new byte [16]; System. arraycopy (bytes, 0, result, 0, bytes. length> 16? 16: bytes. length); for (int I = bytes. length; I <result. length; I ++) {result [I] = 0x00;} return result ;}}
Address: http://www.itmmd.com/201411/98.html
This article is organized and published by android Developers. The source must be indicated ..