ssssssss-team/magic-api

BUG: AES解密的返回值乱码

KissMyLady opened this issue · 0 comments

🐛🐛BUG: AES解密的返回值乱码

问题描述

1, 在magic-api中使用AES加密功能, 代码如下
这是magic自带执行 , 本地java环境执行aes加密没问题

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.KeyUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.lang.String;


Random random = new Random();
var randomNumber = random.nextInt(100);
long startTime = System.currentTimeMillis();


try {
    //执行
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    String content = "Hello World !";
    String secretKey = "2023_10_21_23_00";

    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(secretKey.getBytes(), "AES"));

    byte encryptStr = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
    var s = Base64.getEncoder().encodeToString(encryptStr);

    //执行解密
    byte encryptByte = Base64.getDecoder().decode(s);
    byte decryptBytes = cipher.doFinal(encryptByte);

    String s1 = new String(decryptBytes, "utf-8");
    // string s1 = decryptBytes::string
    return s + " s1: " +s1
}
catch (e){
    return ""+e
}

return 'Hello: ' + s

2, 运行上述代码, api返回值如下

{
    "code": 1,
    "message": "success",
    "data":"KyPPZortd3pLh9Cnkz+xtw== s1: �����>/D�T�q\\{˷W��l��uE���p��m",
    "timestamp": 1704275711438,
    "executeTime": 3
}

二, 截图描述

97db9c268e48b8b611c61df9e1c1b53