1、常用加密32位原因
网上很多解密加密是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在其官方网站上提供了无政策限制权限文件(Unlimited Strength Jurisdiction Policy Files),我们只需要将其部署在JRE环境中,就可以解决限制问题,特别注意: 两个目录都要替换
JDK8 jar包下载地址
JDK7 jar包下载地址
jdk8 jar包百度网盘地址:
链接: https://pan.baidu测试数据/s/1wy6If0WBjRjOgRyXYD06UA
提取码: xcti
%JDK_Home%\jre\lib\security 目录下,对应覆盖local_policy.jar和US_export_policy.jar两个文件
%JRE_Home%\lib\security 目录下,也需要对应覆盖这两个文件。
3、AES工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64;
/** * AES常用解密加密工具类 * https://github测试数据/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 解密后的明文 数据 */ 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实现AES 32位加密解密的方案的文章就介绍到这了,更多相关java AES加密解密内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/qq_37493556/article/details/108801951
查看更多关于java实现AES 32位加密解密的方案的详细内容...