且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP Openssl解密AES Mysql加密

更新时间:2023-02-17 17:23:34

我在这里回答是因为它比明显发表评论要便宜...

I'm answering here because it's cheaper than commenting apparently...

上面的帖子非常确切地告诉您问题所在,但并未真正说明如何解决.

The post above tells you quite exactly what the problem is but doesn't really say how to address it.

openssl_encrypt()和openssl_decrypt()静默将密钥切成最大16个字节的长度(至少对于aes-128-ecb)

openssl_encrypt() and openssl_decrypt() silent cuts the key to max 16 bytes length (at least for aes-128-ecb)

并且无法更改它,因此在MySQL中使用AES_ENCRYPT时,您需要通过创建密钥的子字符串来缩短密钥.

And there is no way to change this, therefore you will need to shorten your key by creating a substring of it when using AES_ENCRYPT in MySQL.

INSERT INTO tablename (dataset) 
VALUES (AES_ENCRYPT('testvalue',SUBSTR( UNHEX(SHA2('mysecretphrase',512)), 1, 16))))

请注意,既然您使用了子字符串,以上答案中的l_16列与["data2"]相同吗? (可能需要在php中执行strtolower())

Notice how the l_16 column in the above answer is the same as ["data2"] now that you use the substring? (may need to do a strtolower() in php)

如果这给您带来了安全隐患,则需要找到一种没有此限制的替代加密算法

If this raises security concerns for you, you will need to find an alternative encryption algorithm that doesnt have this restriction