1、Java后端工具类代码如下AesUtil.java:
package com.jiguiquan.www.utils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Hex; import org.apache.commons.lang3.StringUtils; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; /** * @author jiguiquan * @email jiguiquan@haier.com * @date 2021-06-05 14:38:19 */ @Slf4j public class AesUtil { // AES key要求是16位的 private static final String KEY = "12345678abcdefgh"; private static final String KEY_ALGORITHM = "AES"; private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默认的加密算法 /** * AES 加密操作 * * @param content 待加密内容 * @return 返回Base64转码后的加密数据 */ public static String encrypt(String content) { try { if (StringUtils.isBlank(content)) { return content; } // 创建密码器 Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); byte[] byteContent = content.getBytes("utf-8"); // 初始化为加密模式的密码器 cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(KEY)); // 加密 byte[] result = cipher.doFinal(byteContent); //转为16进制 return Hex.encodeHexString(result); } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * AES 解密操作 * * @param content * @return */ public static String decrypt(String content) { log.info("需要解密的字符串为---" + content); if (StringUtils.isBlank(content)) { return content; } try { //实例化 Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); //使用密钥初始化,设置为解密模式 cipher.init(Cipher.DECRYPT_MODE, getSecretKey(KEY)); byte[] result = cipher.doFinal(Hex.decodeHex(content.toCharArray())); return new String(result, "utf-8"); } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * 生成加密秘钥 * * @return */ private static SecretKeySpec getSecretKey(final String password) { return new SecretKeySpec(password.getBytes(), KEY_ALGORITHM); } }
2、编写一个简单的测试方法:
@SpringBootTest class RareBackendApplicationTests { @Test void contextLoads() { } @Test public void testAES(){ String origin = "0x96fD87D68eC259035604230D0c4dFE9a3d66066A"; String afterEncrypt = AesUtil.encrypt(origin); System.out.println("afterEncrypt:" + afterEncrypt); String afterDecrypt = AesUtil.decrypt(afterEncrypt); System.out.println("afterDecrypt:" + afterDecrypt); } }
3、运行测试代码,结果如下:
afterEncrypt: 3f58ef0b24095a2460a1dc5fb432ad82f32515e1c2ee0c05f53301aa4b5267f7c28cd914c7b430039c05791d795dba3f afterDecrypt: 0x96fD87D68eC259035604230D0c4dFE9a3d66066A
4、前端生成签名的代码如下:
const CryptoJS = require('crypto-js'); const origin = "0x96fD87D68eC259035604230D0c4dFE9a3d66066A"; let key = CryptoJS.enc.Utf8.parse('12345678abcdefgh') let encryptResult = CryptoJS.AES.encrypt(origin, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) let afterEncrypt = encryptResult.ciphertext.toString() console.log("afterEncrypt:", afterEncrypt) let bytes = CryptoJS.AES.decrypt(afterEncrypt, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7, format: CryptoJS.format.Hex }) let decryptResult = bytes.toString(CryptoJS.enc.Utf8) let afterDecrypt = decryptResult.toString() console.log("afterDecrypt:", afterDecrypt)
5、运行前端代码,结果如下:
afterEncrypt: 3f58ef0b24095a2460a1dc5fb432ad82f32515e1c2ee0c05f53301aa4b5267f7c28cd914c7b430039c05791d795dba3f afterDecrypt: 0x96fD87D68eC259035604230D0c4dFE9a3d66066A
可以看到,前后加解密前后的字符串是完全一样的,这样就可以用来作为前后端敏感字符串的传输方式了!