AES encryption in mysql and php

后端 未结 4 1241
夕颜
夕颜 2020-12-22 12:02

There is a function in Mysql AES_encrypt.

SELECT AES_encrypt( \"Hello World\", \"password\" ) AS encrypted_value 

This gives the result:

4条回答
  •  [愿得一人]
    2020-12-22 12:15

    There are three problems with the code you are using:

    1. As others have mentioned, your PHP code is currently using MCRYPT_RIJNDAEL_256 whereas, as documented under AES_ENCRYPT():

      Encoding with a 128-bit key length is used, but you can extend it up to 256 bits by modifying the source. We chose 128 bits because it is much faster and it is secure enough for most purposes.

    2. As others have mentioned, you are applying base64_encode() to convert PHP's binary result to text, whereas the MySQL result appears merely to be a hexadecimal representation of its binary result. You can either use TO_BASE64() in MySQL since v5.6.1 or else bin2hex() in PHP.

    3. As documented under mcrypt_encrypt():

      If the size of the data is not n * blocksize, the data will be padded with '\0'.

      Whereas MySQL uses PKCS7 padding.

    Therefore, to obtain the same results in PHP as you currently show for MySQL:

    
    

提交回复
热议问题