【AES】 前后端解决方案

1、前言

前端引用crypto-js-4.2.0 js,后端采用springboot的默认加密库

2、前端

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8"/>
    <title>AES</title>
</head>
<h1>AES</h1>

<body></body>
<script src="../script/jq/jquery-2.1.1.min.js"></script>
<script src="./crypto-js-4.2.0/crypto-js.js"></script>
<script>
    $(function () {
        let obj = {name: "张三"};
        let str1 = JSON.stringify(obj);
        str1 = "张三123";
        let cc = aesEncrypt(str1);
        console.log("===aesEncrypt ", cc);

        let cc2 = aesDecrypt(cc);
        console.log("===aesDecrypt ", cc2);
    });

    function aesEncrypt(plaintext) {
        try {
            const ciphertext = 'qZief4Y7nr5O2yK5cQxf9z1C5S7g16AP9fNHrHCeNBM='; // 要解密的密文
            const key = 'FffrwWL=2=f!yV5Z'; // 密钥
            const keyBytes = CryptoJS.enc.Utf8.parse(key);
            const encrypted = CryptoJS.AES.encrypt(
                CryptoJS.enc.Utf8.parse(plaintext), keyBytes, {
                    mode: CryptoJS.mode.ECB,  //aes加密模式EcB 这个参数要前后端至使用统一的加密模式
                    padding: CryptoJS.pad.Pkcs7  //使用Pkcs7的方式填充
                }
            );
            const hexStr = CryptoJS.enc.Hex.parse(encrypted.ciphertext.toString());
            return CryptoJS.enc.Base64.stringify(hexStr);
        } catch (e) {
            console.log("===aesEncrypt ", e);
        }
        return plaintext;
    }

    function aesDecrypt(secrecyStr) {
        try {
            const key = 'FffrwWL=2=f!yV5Z'; // 密钥
            const keyBytes = CryptoJS.enc.Utf8.parse(key);
            const decryptedBytes = CryptoJS.AES.decrypt(secrecyStr, keyBytes, {
                mode: CryptoJS.mode.ECB,// 设置成与后端一致的加解密模式
                padding: CryptoJS.pad.Pkcs7// 设置成与后端一致的填充方式
            });
            const decryptedText = decryptedBytes.toString(CryptoJS.enc.Utf8);
            return decryptedText;
        } catch (e) {
            return secrecyStr;
        }
    }
</script>

</html>

3、java

package org.jeecg.common.util.security;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Base64;

public class AesUtils {
    private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
    private static final String key = "FffrwWL=2=f!yV5Z";

    public static void main(String[] args) throws Exception {
        String aaa = "张三123";
        System.out.printf("====== 加密 === 明文 [%s] 密文 [%s] \n", aaa, encrypt(aaa));
        String bbb = "aRCbHShtrpNWJnNg25W69g==";
        System.out.printf("====== 解密 === 密文 [%s] 明文 [%s] \n", bbb, decrypt(bbb));
    }

    public static String encrypt(String data) throws Exception {
        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);
        byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decrypt(String encryptedData) throws Exception {
        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        byte[] decoded = Base64.getDecoder().decode(encryptedData);
        String result = new String(cipher.doFinal(decoded), StandardCharsets.UTF_8);
        return result;
    }
}

posted @ 2024-04-26 10:09  一只桔子2233  阅读(3)  评论(0编辑  收藏  举报