使用mcrypt加密/解密

时间:2013-12-10 22:52:32

标签: php encryption mcrypt

尝试使用以下策略实现加密和解密,但最终以随机字符结束。

class Crypt {

public static function encrypt($string, $account) {
    // create a random initialization vector to use with CBC encoding
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

    $key = pack('H*', $account . $account);

    $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $iv);
    $output = $iv . $output;
    $output = base64_encode($output);
    $output = urlencode($output);

    return $output;
}

public static function decrypt($token, $account) {
    $ciphertext_dec = base64_decode($token);

    // retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv_dec = substr($ciphertext_dec, 0, $iv_size);

    // retrieves the cipher text (everything except the $iv_size in the front)
    $ciphertext_dec = substr($ciphertext_dec, $iv_size);

    $key = pack('H*', $account . $account);

    $token = urldecode($token);

    $output = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);
    $output = rtrim($output, "");
    return $output;
}

   }

无法获取准确的值,有时会解密,但我看到一些垃圾值,但大多只是随机字符。

$a = \Crypt::encrypt("MyPassword", "1974246e");
echo \Crypt::decrypt($a, "1974246e");

讨论后编辑

class Crypt {

public static function encrypt($data, $passphrase) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); //create a random initialization vector to use with CBC encoding
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $key = pack('H*', $passphrase . $passphrase);
    return base64_encode($iv . mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $iv));
}

public static function decrypt($data, $passphrase) {
    $data = base64_decode($data);
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); //retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
    $iv = substr($data, 0, $iv_size);
    $data = substr($data, $iv_size); //retrieves the cipher text (everything except the $iv_size in the front)
    $key = pack('H*', $passphrase . $passphrase);
    return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $iv), chr(0));
}
}

用法:

$pass = "MyPassword*&^*&^(*&^(";
$token = \Crypt::encrypt($pass, "1974246e8e8a479bb0233495e8a3ed12");
$answer = \Crypt::decrypt($token, "1974246e8e8a479bb0233495e8a3ed12");
echo $answer == $pass ? "yes" : "no";

1 个答案:

答案 0 :(得分:2)

  1. 不要urlencode。不必要的。
  2. 修剪NULL字节,而不是空字符串:rtrim($str, chr(0));(相反,您可能也希望将源字符串长度保存在加密结果中,因此您不会rtrim()太多。)< / LI>

    pack('H*', $account)为什么$key?也没必要。

    Rijndael 128使用16字节密钥(128位),因此请确保您的密钥至少有那么长:

    $key = $account . $account
    

    会做,但显然不完美。 (如果它太短,mcrypt会做类似的事情。)如果每个帐户都有自己的密码,那就太好了。 (更重要的是与app秘密相结合,但细节。)

    很可能

    rtrim()chr(0)很好,因为你的源字符串不会有尾随的NUL字节。

    I usually use these en/decrypt functions, or alike,但这些都有静态密钥/密钥,所以你的更好。

    将加密的令牌发送到客户端:

    $enc_token = Crypt::encrypt($token, $key);
    // $enc_token might contain `/` and `+` and `=`
    $url = 'page.php?token=' . urlencode($enc_token);