码迷,mamicode.com
首页 > 编程语言 > 详细

java Aes256 加密算法的实现

时间:2015-01-01 10:08:34      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:java

如果希望进行AES256位的加密解密,需要事先从java官网下载 local_policy.jar与US_export_policy.jar替换%JAVA_HOME%/jre/lib/security的两个policy文件,local_policy.jar与US_export_policy .jar。 主要是为了突破AES算法只能支持到128位的限制。如果未替换,可能会得到如下错误: *

 

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 加密
 
*
 
* @author jlins
 
*
 
*/
 
public class Aes256Encryptor {
 
// 说明 key 需要大家自己去设定加密解密的key,key牵涉到安全信息,所以这里无法公布
 
    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", "01234567890123456", "123", "123", "0123456789012345678",
 
                            "012345678901234567890123456789", "b", "0123456789012345", "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.getInstance(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.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;
 
}
 
/**
 
* 补齐的16位的整数倍
 
*
 
* @param pData
 
* @return
 
*/
 
private static byte[] paddingData(String pData) {
 
       byte[] 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;
 
        }
 
/**
 
* 初始化向量到16位
 
* */
 
       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;
 
}
 
}

 

原文地址:http://www.itmmd.com/201411/98.html
该文章由 android开发 整理发布,转载须标明出处。

java Aes256 加密算法的实现

标签:java

原文地址:http://blog.csdn.net/androidmylove/article/details/42317573

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!