将 RSA 加密数据存储为十六进制
我正在使用 OpenSSL::PKey::RSA
使用私钥加密/解密数据字符串。我将加密数据作为字符串存储在表的列中。我已经使用 Base64.encode64
和 Base64.decode64
使此实现正常工作。但是,我不想将加密数据存储为基数 64,我想将其存储为字符串中的十六进制。
我目前正在使用以下内容来存储加密数据:
encrypted_data = pk.private_encrypt(plain_data).unpack('H*').first
这会导致 encrypted_Data
等于如下所示的字符串,该字符串可以轻松存储在我的数据库中。
d70db8c36d6ccbadd1cca1263ff140df24e0112f636ac9ea92c28f27e443496c
我的问题在于将此十六进制字符串更改回解密数据所需的二进制字符串。我尝试了几种不同的方法,但似乎都不起作用。
解密这个十六进制字符串的最佳/最简单方法是什么?
I am using OpenSSL::PKey::RSA
to encrypt/decrypt a string of data using a private key. I am storing the encrypted data in a column in a table as a string. I have gotten this implementation to work no problem using Base64.encode64
and Base64.decode64
. However, I do not want to store the encrypted data as base 64, I would like to store it as hexadecimal in a string.
I'm currently using the following to store the encrypted data:
encrypted_data = pk.private_encrypt(plain_data).unpack('H*').first
This results in encrypted_Data
equaling a string like that bellow, which easily stores in my database.
d70db8c36d6ccbadd1cca1263ff140df24e0112f636ac9ea92c28f27e443496c
My problem has come in the changing of this hexadecimal string back to the binary string that is needed to decrypt the data. I've tried several different approaches and none seem to work.
What is the best/easiest way to decrypt this hexadecimal string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
unpack 的反面是 pack,这是您要将此十六进制字符串恢复为二进制的东西。像这样:
Pack 是数组上的函数,而不是字符串,因此请确保传递 unpack('H*') 结果相同的数组,否则输出将不同。
The opposite of unpack is pack, which is what you're looking for to get this hex string back to binary. Like so:
Pack is a function on array, not string, so be sure you're passing the same array that unpack('H*') results in or else the output will not be the same.