本文介绍了在mysql和php中加密AES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  SELECT AES_encrypt(Hello World,password)AS encrypted_value 

这给出了结果: 9438eb79863e7009722fc3f0ad4b7198



但是当我使用php中的代码执行AES_encrypt时,它会给我一个不同的值。



我得到的PHP代码来自stackoverflow -


$ b $ (
mcrypt_encrypt(
MCRYPT_RIJNDAEL_256
$ sSecretKey,$ sValue,
MCRYPT_MODE_ECB(

$ ,
mcrypt_create_iv(
mcrypt_get_iv_size(
MCRYPT_RIJNDAEL_256,
MCRYPT_MODE_ECB
),
MCRYPT_RAND)

),\ 0
?>

PHP代码的结果是 ytip2sEkD87gmRk3IVI09qE7T + RoLr20YK4rJp16NkY =



php或codeigniter中是否有方法返回相同的值。
- 谢谢。

解决方案

您正在使用的代码有三个问题:


  1. 正如其他人所说,您的PHP代码目前正在使用 MCRYPT_RIJNDAEL_256 ,而如下所述:


  2. 正在应用 base64_encode()将PHP的二进制结果转换为文本,而MySQL结果似乎只是其二进制结果的十六进制表示。您可以使用在v5.6.1之后,或者在PHP中。


  3. 如:

    而MySQL使用。


因此,要获得相同的结果在PHP中您正在为MySQL显示:

 <?php 

class MySQL_Function {
const PKCS7 = 1;

private static function pad($ string,$ mode,$ blocksize = 16){
$ len = $ blocksize - (strlen($ string)%$ blocksize);
switch($ mode){
case self :: PKCS7:
$ padding = str_repeat(chr($ len),$ len);打破;

默认值:
throw new Exception();
}
return $ string。$ padding;
}

public static function AES_ENCRYPT($ str,$ key_str){
return mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
$ key_str,self :: pad $ str,self :: PKCS7),
MCRYPT_MODE_ECB
);
}
}

echo bin2hex(MySQL_Function :: AES_encrypt(Hello World,password));

?>


There is a function in Mysql AES_encrypt.

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

This gives the result: 9438eb79863e7009722fc3f0ad4b7198

But when I use the code in php to do AES_encrypt it gives me a different value.

The PHP code I got from stackoverflow -- PHP AES encrypt / decrypt

<?php
base64_encode(
        mcrypt_encrypt(
            MCRYPT_RIJNDAEL_256,
            $sSecretKey, $sValue,
            MCRYPT_MODE_ECB,
            mcrypt_create_iv(
                mcrypt_get_iv_size(
                    MCRYPT_RIJNDAEL_256,
                    MCRYPT_MODE_ECB
                ),
                MCRYPT_RAND)
            )
        ), "\0"
?>

The result from PHP code is ytip2sEkD87gmRk3IVI09qE7T+RoLr20YK4rJp16NkY=

Is there a method in php or codeigniter so that it returns the same value.?--Thank you.

解决方案

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():

  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():

    Whereas MySQL uses PKCS7 padding.

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

<?php

class MySQL_Function {
  const PKCS7 = 1;

  private static function pad($string, $mode, $blocksize = 16) {
    $len = $blocksize - (strlen($string) % $blocksize);
    switch ($mode) {
      case self::PKCS7:
        $padding = str_repeat(chr($len), $len); break;

      default:
        throw new Exception();
    }
    return $string.$padding;
  }

  public static function AES_ENCRYPT($str, $key_str) {
    return mcrypt_encrypt(
      MCRYPT_RIJNDAEL_128,
      $key_str, self::pad($str, self::PKCS7),
      MCRYPT_MODE_ECB
    );
  }
}

echo bin2hex(MySQL_Function::AES_encrypt( "Hello World", "password" ));

?>

这篇关于在mysql和php中加密AES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 15:34