java實現(xiàn)AES 32位加密解密的方案
1、常用加密32位原因
網(wǎng)上很多解密加密是16位的,用32位密鑰加密會報
java.security.InvalidKeyException: Illegal key size or default parameters
異常錯誤,因為美國的出口限制,Sun通過權限文件(local_policy.jar、US_export_policy.jar)做了相應限制。因此存在以下一些問題:
- 密鑰長度上不能滿足需求(如:java.security.InvalidKeyException: Illegal key size or default parameters);
- 部分算法未能支持,如MD4、SHA-224等算法;
- API使用起來還不是很方便;
- 一些常用的進制轉換輔助工具未能提供,如Base64編碼轉換、十六進制編碼轉換等工具。
2、解決方案
Oracle在其官方網(wǎng)站上提供了無政策限制權限文件(Unlimited Strength Jurisdiction Policy Files),我們只需要將其部署在JRE環(huán)境中,就可以解決限制問題,特別注意:兩個目錄都要替換
jdk8 jar包百度網(wǎng)盤地址:
鏈接: https://pan.baidu.com/s/1wy6If0WBjRjOgRyXYD06UA
提取碼: xcti
%JDK_Home%\jre\lib\security
目錄下,對應覆蓋local_policy.jar和US_export_policy.jar兩個文件
%JRE_Home%\lib\security
目錄下,也需要對應覆蓋這兩個文件。
3、AES工具類
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; /** * AES常用解密加密工具類 * https://github.com/ourlang * @author 小林 */ public class AesUtil { /** * 默認的字符編碼 */ private static final String DEFAULT_CHARSET = "utf-8"; /** * 算法 */ private static String ALGORITHM = "AES"; /** * 算法/模式/填充 **/ private static final String CipherMode = "AES/ECB/PKCS5Padding"; /** * 記錄日志 **/ private final static Logger logger = LoggerFactory.getLogger(AesUtil.class); private AesUtil() { } /** * 解密AES 32位 * * @param sSrc 解密的內容 * @param secretKey 秘鑰 * @return 解密后的明文 數(shù)據(jù) */ public static String decrypt(String sSrc, String secretKey) { if (secretKey == null) { logger.error("需要加密的秘鑰為空"); return null; } try { byte[] raw = secretKey.getBytes(DEFAULT_CHARSET); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(CipherMode); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); // 先用base64解密 byte[] encryptedArr = Base64.getDecoder().decode(sSrc); byte[] original = cipher.doFinal(encryptedArr); return new String(original, DEFAULT_CHARSET); } catch (Exception ex) { logger.error("AES解密失敗", ex); return null; } } /** * 加密32位 * * @param sSrc 需要加密的內容 * @param sKey 秘鑰 * @return 加密的內容 */ public static String encrypt(String sSrc, String sKey) { if (sKey == null) { logger.error("需要加密的秘鑰為空"); return null; } try { byte[] raw = sKey.getBytes(DEFAULT_CHARSET); SecretKeySpec skeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(CipherMode); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(sSrc.getBytes(DEFAULT_CHARSET)); return Base64.getEncoder().encodeToString(encrypted); } catch (Exception ex) { logger.error("AES加密失敗", ex); return null; } } }
到此這篇關于java實現(xiàn)AES 32位加密解密的方案的文章就介紹到這了,更多相關java AES加密解密內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
IDEA下創(chuàng)建SpringBoot+MyBatis+MySql項目實現(xiàn)動態(tài)登錄與注冊功能
這篇文章主要介紹了IDEA下創(chuàng)建SpringBoot+MyBatis+MySql項目實現(xiàn)動態(tài)登錄與注冊功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02Spring Boot集成Druid出現(xiàn)異常報錯的原因及解決
Druid 可以很好的監(jiān)控 DB 池連接和 SQL 的執(zhí)行情況,天生就是針對監(jiān)控而生的 DB 連接池。本文講述了Spring Boot集成Druid項目中discard long time none received connection異常的解決方法,出現(xiàn)此問題的同學可以參考下2021-05-05如何使用Spring AOP的通知類型及創(chuàng)建通知
這篇文章主要給大家介紹了關于如何使用Spring AOP的通知類型及創(chuàng)建通知的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring AOP具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12