gpt4 book ai didi

Ruby OpenSSL AES 生成随 secret 钥

转载 作者:数据小太阳 更新时间:2023-10-29 08:13:02 30 4
gpt4 key购买 nike

我有一个我似乎无法弄清楚的基本问题。我正在尝试在 AES-256-CBC 中生成一个可用于加密/解密数据的随 secret 钥。

这是我正在做的:

require 'openssl'
cipher = OpenSSL::Cipher::AES256.new(:CBC)
cipher.encrypt
puts cipher.random_key
>> "\xACOM:\xCF\xB3@o)<&y!\x16A\xA1\xB5m?\xF1 \xC9\x1F>\xDB[Uhz)\v0"

这给了我上面的字符串,它看起来与我过去使用的键完全不同。您可能会说,我对加密非常陌生,但我想了解是否需要进一步准备字符串。我在 rails 中创建了一个快速 View ,这样我就可以转到/generate,它会呈现一个带有随机键的简单 html 页面。它甚至不会呈现页面并提示无效的 uTF8。我能让页面显示的唯一方法是先对 key 进行 Base64 编码。

我知道我错过了一些愚蠢的东西。任何想法都会很棒。

编辑:如果我进行 Base64 编码,这就是它的样子。我应该去掉 = 标志还是什么?

AES-128-CBC
Random Key: 0xdq+IZdmYHHbLC9Uv8jgQ==
Random IV: vp08d/nFGE3R8HsmOzYzOA==

AES-256-CBC
Random Key: BW0wY5fUkcwszV5GIczI+D45eFOz/Ehvw5XdZIavVOQ=
Random IV: D0pXdwQAqu+XSOv8E/dqBw==

感谢您的帮助!

最佳答案

回答你的问题(引用自维基百科):

The '==' sequence indicates that the last group contained only one byte, and '=' indicates that it contained two bytes.

In theory, the padding character is not needed for decoding, since the number of missing bytes can be calculated from the number of Base64 digits. In some implementations, the padding character is mandatory, while for others it is not used.

对于 Ruby 和您的用例,答案是:剥离 =

没问题

关于Ruby OpenSSL AES 生成随 secret 钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13590942/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com