通过 SQLALchemy 在 Postgres 中插入加密数据
我想使用 RSA 算法加密一个字符串,然后使用 python 中的 SQLAlchemy 将该字符串存储到 postgres 数据库中。然后检索加密的字符串并使用相同的密钥对其进行解密。我的问题是存储在数据库中的值与实际的加密字符串不同。存储加密值的列的数据类型是bytea。我正在使用 pycrypto 库。在将数据插入数据库表之前,是否需要更改特定格式的数据?
有什么建议请。
谢谢, 塔拉·辛格
I want to encrypt a string using RSA algorithm and then store that string into postgres database using SQLAlchemy in python. Then Retrieve the encrypted string and decrypt it using the same key. My problem is that the value gets stored in the database is not same as the actual encrypted string. The datatype of column which is storing the encrypted value is bytea. I am using pycrypto library. Do I need to change the data in a particular format before inserting it to database table?
Any suggestions please.
Thanks,
Tara Singh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“同一键”您的意思是“另一个键”,对吗? RSA 给你一个密钥对,如果你用一个密钥对加密,你用另一个密钥对解密......
除此之外,这听起来像是一个编码问题。尝试将数据存储为二进制或使用数据库排序规则对字符串进行编码。
基本上,加密为您提供字节,但您将它们存储为字符串(编码字节)。
By "same key" you mean "the other key", right? RSA gives you a keypair, if you encrypt with one you decrypt with the other ...
Other than that, it sounds like a encoding problem. Try storing the data as binary or encode the string with your databases collation.
Basically encryption gives you bytes but you store them as a string (encoded bytes).
我认为 SQLAlchemy 文档包含一个使用“混合”属性的“配方”示例。我并不声称完全理解它或尝试过它,但您可能会发现它很有用:
http ://www.sqlalchemy.org/trac/wiki/UsageRecipes/SymmetricEncryption
I think the SQLAlchemy documents contain a 'recipe' example which uses the 'hybrid' property. I do not profess to understand it fully or tried it yet, but you may find it useful:
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/SymmetricEncryption