当前位置: 首页> 游戏> 游戏 > 国外免费空间网站申请_厦门网站建设方案书_今日头条搜索引擎_百度收录量

国外免费空间网站申请_厦门网站建设方案书_今日头条搜索引擎_百度收录量

时间:2025/7/14 14:14:33来源:https://blog.csdn.net/qq_31452013/article/details/144895118 浏览次数:0次
国外免费空间网站申请_厦门网站建设方案书_今日头条搜索引擎_百度收录量

前端直接默认参数加密的,没有传iv salt等

const cryptoJS = require("crypto-js");
const encryptedValue = cryptoJS.AES.encrypt(value, secret)

后端代码

前端默认参数加密,后端解密

 public static byte[][] GenerateKeyAndIV(int keyLength, int ivLength, int iterations, byte[] salt, byte[] password, MessageDigest md) {int digestLength = md.getDigestLength();int requiredLength = (keyLength + ivLength + digestLength - 1) / digestLength * digestLength;byte[] generatedData = new byte[requiredLength];int generatedLength = 0;try {md.reset();// Repeat process until sufficient data has been generatedwhile (generatedLength < keyLength + ivLength) {// Digest data (last digest if available, password data, salt if available)if (generatedLength > 0) {md.update(generatedData, generatedLength - digestLength, digestLength);}md.update(password);if (salt != null) {md.update(salt, 0, 8);}md.digest(generatedData, generatedLength, digestLength);// additional roundsfor (int i = 1; i < iterations; i++) {md.update(generatedData, generatedLength, digestLength);md.digest(generatedData, generatedLength, digestLength);}generatedLength += digestLength;}// Copy key and IV into separate byte arraysbyte[][] result = new byte[2][];result[0] = Arrays.copyOfRange(generatedData, 0, keyLength);if (ivLength > 0) {result[1] = Arrays.copyOfRange(generatedData, keyLength, keyLength + ivLength);}return result;} catch (DigestException e) {throw new RuntimeException(e);} finally {// Clean out temporary dataArrays.fill(generatedData, (byte) 0);}}//解密方法public static byte[] aesDecrypt(String encryptedText, String secret) {try {byte[] cipherData = cn.hutool.core.codec.Base64.decode(encryptedText);byte[] saltData = Arrays.copyOfRange(cipherData, 8, 16);MessageDigest md5 = MessageDigest.getInstance("MD5");final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, secret.getBytes(StandardCharsets.UTF_8), md5);SecretKeySpec key = new SecretKeySpec(keyAndIV[0], "AES");IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);byte[] encrypted = Arrays.copyOfRange(cipherData, 16, cipherData.length);Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding");aesCBC.init(Cipher.DECRYPT_MODE, key, iv);return aesCBC.doFinal(encrypted);} catch (Exception e) {log.error("aesDecrypt errorencryptedText" + e.getMessage(), e);return null;}}

后端加密

 public static byte[] aesEncrypt(String datastr, String secret) {try {byte[] input = datastr.getBytes();// 以下两个变量不可修改,否则java加密的,node无法解密byte[] saltData = "DFބf$t:".getBytes();byte[] preData = "Salted__".getBytes();MessageDigest md5 = MessageDigest.getInstance("MD5");final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, secret.getBytes(StandardCharsets.UTF_8), md5);SecretKeySpec key = new SecretKeySpec(keyAndIV[0], "AES");IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding");aesCBC.init(Cipher.ENCRYPT_MODE, key, iv);byte[] encrypt = aesCBC.doFinal(input);byte[] encryptedData = addBytes(saltData, encrypt);return Base64.getEncoder().encode((addBytes(preData, encryptedData)));} catch (Exception e) {log.error("aesEncrypt error.param={}", datastr, e);return null;}}public static byte[] addBytes(byte[] data1, byte[] data2) {byte[] data3 = new byte[data1.length + data2.length];System.arraycopy(data1, 0, data3, 0, data1.length);System.arraycopy(data2, 0, data3, data1.length, data2.length);return data3;}

关键字:国外免费空间网站申请_厦门网站建设方案书_今日头条搜索引擎_百度收录量

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: