Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: AES解密的返回值乱码 #97

Open
KissMyLady opened this issue Jan 3, 2024 · 0 comments
Open

BUG: AES解密的返回值乱码 #97

KissMyLady opened this issue Jan 3, 2024 · 0 comments

Comments

@KissMyLady
Copy link

🐛🐛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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant