有没有办法将AES密钥从base64转换为UTF-8字符串?

发布于 2025-02-05 14:15:22 字数 494 浏览 3 评论 0原文

我正在使用以下方式生成AES-256 SecretKey:

            KeyGenerator aesKeyGen = KeyGenerator.getInstance("AES");
            aesKeyGen.init(256);
            SecretKey secretKey = aesKeyGen.generateKey();
            clientAesKey = secretKey.getEncoded();

有没有一种方法可以将字节数字视为字符串?对我有用的唯一方法是:

Base64.getEncoder().encodeToString(clientAesKey);

如果我打印并复制字符串,并尝试使用在线base64解码器进行解码,它会抱怨字符串不是UTF-8。

我知道它破坏了加密的目的,但是有什么方法可以将静态32个字符的长字符串分配为秘密键?

I am generating an AES-256 SecretKey using:

            KeyGenerator aesKeyGen = KeyGenerator.getInstance("AES");
            aesKeyGen.init(256);
            SecretKey secretKey = aesKeyGen.generateKey();
            clientAesKey = secretKey.getEncoded();

Is there a way that I can see the array of bytes as a string? Only way that works for me is this:

Base64.getEncoder().encodeToString(clientAesKey);

If I print it and copy the string, and try to decode it using an online Base64 decoder, it complains that the string is not UTF-8.

I know it defeats the purpose of encryptions, but is there a way that I can assign a static 32-characters long string as a secret key?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笔一画续写前缘 2025-02-12 14:15:22

这是“可视化”一个键的简单方法:

System.out.println(new java.math.BigInteger(1, keyBytes).toString(16));

This is a simple way to 'visualize' a key:

System.out.println(new java.math.BigInteger(1, keyBytes).toString(16));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文