记AES加密在linux系统每次都不一样的问题 蔚落 2023-07-15 13:04 23阅读 0赞 记AES加密在linux系统每次都不一样的问题 在项目中通常会用到AES的加密方法,具体代码如下 package com.mt.demo.client.utils; import lombok.extern.slf4j.Slf4j; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.springframework.stereotype.Component; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.security.*; import java.util.Base64; /** * AESUtils * * @author mt.luo * @description: AES加密 */ @Slf4j @Component public class AESUtils { private final String aesSeed = "ada46ab5da824b96a18409c49dc91dc2"; private final String algorithm = "SHA-256"; private final String AES = "AES"; private final String cipher = "AES/ECB/PKCS7Padding"; private final String provider = "BC"; /** * convert to byte[] * * @param key key * @return byte[] * @throws NoSuchAlgorithmException */ private byte[] toHash256(String key) throws NoSuchAlgorithmException { MessageDigest messageDigest = MessageDigest.getInstance(this.algorithm); messageDigest.update(key.getBytes()); return messageDigest.digest(); } /** * parseHexStrToByte * * @param hexStr hexStr * @return byte */ private byte[] parseHexStrToByte(String hexStr) { if (hexStr.length() < 1) { return null; } byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i < hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } /** * parseByteToHexStr * * @param bytes bytes * @return String */ private String parseByteToHexStr(byte[] bytes) { StringBuilder stringBuilder = new StringBuilder(); for (byte b : bytes) { String hex = Integer.toHexString(b & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } stringBuilder.append(hex.toUpperCase()); } return stringBuilder.toString(); } /** * 获取密钥 * * @param seed seed * @return SecretKeySpec * @throws NoSuchAlgorithmException NoSuchAlgorithmException */ private SecretKeySpec getSecretKeySpec(String seed) throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance(this.AES); SecureRandom secureRandom = new SecureRandom(this.toHash256(seed)); keyGenerator.init(256, secureRandom); SecretKey secretKey = keyGenerator.generateKey(); byte[] encode = secretKey.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(encode, this.AES); Security.addProvider(new BouncyCastleProvider()); return secretKeySpec; } /** * 加密 * * @param data data * @return String * @throws NoSuchAlgorithmException * @throws NoSuchProviderException * @throws NoSuchPaddingException * @throws UnsupportedEncodingException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public String encrypt(String data) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException { SecretKeySpec secretKeySpec = this.getSecretKeySpec(this.aesSeed); Cipher cipher = Cipher.getInstance(this.cipher, this.provider); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); return this.parseByteToHexStr(Base64.getEncoder().encode(cipher.doFinal(data.getBytes(StandardCharsets.UTF_8.name())))); } } 然后这样使用在windows系统的时候没有问题,而将程序部署到Linux则发现每次加密之后获取的加密字符串都不同,也无法解密,重写获取密钥部分的代码 /** * 获取密钥 * * @param seed seed * @return SecretKeySpec * @throws NoSuchAlgorithmException NoSuchAlgorithmException */ private SecretKeySpec getSecretKeySpec(String seed) throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance(this.AES); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(this.toHash256(seed)); keyGenerator.init(256, secureRandom); SecretKey secretKey = keyGenerator.generateKey(); byte[] encode = secretKey.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(encode, this.AES); Security.addProvider(new BouncyCastleProvider()); return secretKeySpec; } #### 主要原因是SecureRandom 实现完全隨操作系统本身的内部状态,除非调用方在调用 getInstance 方法之后又调用了 setSeed 方法;该实现在 windows 上每次生成的 key 都相同,但是在 linux 系统上则不同。 #### 欢迎关注微信交流 ![在这里插入图片描述][70] [70]: /images/20230528/c63173053c414105b0b06e40512a94d5.png
相关 AES加密 一、AES是什么 AES高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府 我就是我/ 2023年10月03日 17:58/ 0 赞/ 76 阅读
相关 记AES加密在linux系统每次都不一样的问题 记AES加密在linux系统每次都不一样的问题 在项目中通常会用到AES的加密方法,具体代码如下 package com.mt.demo.client.utils; 蔚落/ 2023年07月15日 13:04/ 0 赞/ 24 阅读
相关 AES 加密 util.encryption = function (params) \{ let \{ data, param, key \} = params const res 悠悠/ 2023年02月18日 03:05/ 0 赞/ 73 阅读
相关 AES 加密 import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.c 秒速五厘米/ 2022年12月29日 15:28/ 0 赞/ 223 阅读
相关 AES加密 package com.test.util.encrypt; import org.apache.commons.lang3.StringUtil 小咪咪/ 2022年07月15日 20:43/ 0 赞/ 290 阅读
相关 AES加密 AES加密是一种对称加密,即加密秘钥与解密秘钥相同 示例如下: public class Aes { //算法 private st 偏执的太偏执、/ 2022年04月14日 05:14/ 0 赞/ 310 阅读
相关 AES加密 import java.io.UnsupportedEncodingException; import java.security.InvalidKeyExce 小灰灰/ 2022年03月09日 14:46/ 0 赞/ 339 阅读
相关 AES加密 AES技术是一种对称的分组加密技术,使用128位分组加密数据,提供比WEP/TKIPS的RC4算法更高的加密强度。AES的加密码表和解密码表是分开的,并且支持子密钥加密,这种 ゞ 浴缸里的玫瑰/ 2022年02月15日 00:09/ 0 赞/ 338 阅读
相关 Python AES加密 与 JS AES加密 import execjs from Crypto.Cipher import AES from binascii import b2a_hex, a2 「爱情、让人受尽委屈。」/ 2021年12月20日 11:55/ 0 赞/ 429 阅读
相关 AES加密 介绍 AES是一种对称加密,使用同一个密钥来加密和解密一段密文 安装 pip install pycryptodome 基础语法 aes 今天药忘吃喽~/ 2021年12月09日 04:49/ 0 赞/ 390 阅读
还没有评论,来说两句吧...