如何在MySQL数据库中存储AES加密信息 [英] How to store AES encrypted information in MySQL database

查看:533
本文介绍了如何在MySQL数据库中存储AES加密信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条信息是使用aes-256-cbc加密编码的。我应该如何将其存储在数据库中?目前,我正在使用VARCHAR(255)utf8_bin。这样可以吗?还是应该使用其他字段类型,例如VARBINARY(255)?在这种情况下,是否有可能使用VARCHAR丢失某些数据?谢谢。

I have a piece of information which is encoded using aes-256-cbc encryption. How should I store it in the database? Currently I'm using VARCHAR(255) utf8_bin. Is this OK or should I use other field type like VARBINARY(255)? Is there a possibility of losing some data using VARCHAR in this case? Thanks.

推荐答案

尽管有AES密文,但在数据库中存储加密(而不是散列)密码的适当性(可能)。是二进制数据,因此应该这样存储,即存储在 BINARY / VARBINARY 列或 BLOB

The possible (in)appropriateness of storing encrypted (as opposed to hashed) passwords in a database notwithstanding, AES ciphertext is binary data, and therefore should be stored as such, i.e. in a BINARY / VARBINARY column or a BLOB.

也可以对密文进行编码,例如为 base64 ,然后将其存储为文本(即 CHAR / VARCHAR / TEXT )列。这样虽然节省空间,但有时 会更方便,例如在视觉上检查数据或在可能难以处理包含任意二进制数据的字段的程序之间传递数据时。

It's also possible to encode the ciphertext e.g. as base64, and then store it in a text (i.e. CHAR / VARCHAR / TEXT) column. This is less space-efficient, but it may sometimes be more convenient, e.g. when inspecting the data visually or passing it between programs that may have trouble dealing with fields containing arbitrary binary data.

这篇关于如何在MySQL数据库中存储AES加密信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆