MYSQL | AES encrypt()/ decrypt() [英] MYSQL | AES encrypt() / decrypt()

查看:282
本文介绍了MYSQL | AES encrypt()/ decrypt()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以可解密的方式加密字符串。最好的方法是通过给定的密码获得一点保护。安全isnt在这里很重要。

I need to encrypt a string in a decryptable way. Best way would be by a given password to get a little bit of protection in. Security isnt that important in here.

我选择了AES_ENCRYPT(),但不能解密。

I picked out AES_ENCRYPT() so far but cant decrypt it.

加密: strong> SELECT AES_ENCRYPT('test','test')

输出 87bd903885943be48a4e68ab63b0ec6a

解密: SELECT AES_DECRYPT('87bd903885943be48a4e68ab63b0ec6a','test')

输出: NULL

简单的问题:为什么地狱不能解密呢?

Simple question: Why the hell can't I decrypt it? Couldnt find anything about it online.

如果解决方案变得太大(我喜欢简单),我也可以使用另一种加密方法。

If the solutions becomes too big (I like it simple) I would also be fine with another method of encryption.

非常感谢!

MySQL-Client-Version:5.5 .41

MySQL-Client-Version: 5.5.41

推荐答案

您需要先将十六进制字符串转换为二进制数据:

You need to convert the hexadecimal string into binary data first:

SELECT AES_DECRYPT(UNHEX('87bd903885943be48a4e68ab63b0ec6a'), 'test') FROM DUAL;

其实,我很惊讶你原来的 SELECT 语句首先返回一个十六进制字符串。这是我得到的:

Actually, I'm surprised your original SELECT statement returned a hex string in the first place. This is what I get:

mysql> SELECT AES_ENCRYPT('test','test') FROM DUAL;
+----------------------------+
| AES_ENCRYPT('test','test') |
+----------------------------+
| ???8??;?Nh?c??j                     |
+----------------------------+
1 row in set (0.02 sec)

我只能得到一个十六进制字符串,如果我调用 HEX()显式:

I can only get a hex string if I call HEX() explicitly:

mysql> SELECT HEX(AES_ENCRYPT('test','test')) FROM DUAL;
+----------------------------------+
| HEX(AES_ENCRYPT('test','test'))  |
+----------------------------------+
| 87BD903885943BE48A4E68AB63B0EC6A |
+----------------------------------+
1 row in set (0.00 sec)

(这是在MySQL版本5.6.22中)

(This is in MySQL version 5.6.22)

这篇关于MYSQL | AES encrypt()/ decrypt()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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