日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > java Aes256 加密算法的實現

java Aes256 加密算法的實現

來源:程序員人生   發布時間:2015-01-09 08:12:36 閱讀次數:3310次

如果希望進行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開發 整理發布,轉載須標明出處。

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 中文字幕免费视频 | 躁躁躁日日躁网站 | 国产成人在线一区二区 | 一区二区成人在线 | 一区二区三区精品在线 | 欧美成人精品一区二区男人看 | 天堂视频在线观看 | 日韩精品亚洲一区 | 国产在线精品拍揄自揄免费 | www.嫩草 | 精品久久久久久 | 男女涩涩网站 | 成人伊人 | 日韩1区| 一区二区三区国产免费 | 午夜免费视频 | 男女深夜网站 | 久久久久亚洲 | 国产一区二区三区 在线 | 一级特黄免费 | 国内久久久久久 | 久久久久国产 | 欧美激情在线看 | 国产精品久久久久久亚洲调教 | 日韩精品一区在线 | 亚洲一区免费观看 | 人人九九精 | 国产一区中文字幕 | 精品国产欧美一区二区三区成人 | 一区二区三区中文字幕 | 二区在线观看 | 成人在线 | 欧美高h | 中文字幕国产在线视频 | 久亚洲| 一本到高清 | 亚洲黄色三级 | 久久久久久久综合 | 欧美日韩免费在线视频 | 成人av在线网 | 91激情网|