【问题标题】:Encrypt/Decrypt using mcrypt使用 mcrypt 加密/解密
【发布时间】:2013-12-28 17:06:03
【问题描述】:

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

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";

【问题讨论】:

    标签: php encryption mcrypt


    【解决方案1】:
    1. 不要进行urlencode。不必要的。
    2. 修剪 NULL 字节,而不是空字符串:rtrim($str, chr(0));(相反,您可能还希望将源字符串长度保存在加密结果中,因此您不会 rtrim() 太多。)

    为什么pack('H*', $account) 换成$key?也不需要。

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

    $key = $account . $account
    

    可以,但它显然不完美。 (如果太短,mcrypt 会做类似的事情。)如果每个帐户都有自己的密码,那就太好了。 (结合应用程序机密更是如此,但要详细说明。)

    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);
    

    【讨论】:

    • 感谢 Rudie,我删除了 urlencode 并为不需要的字符添加了 trim 并且它有效,但是,我不知道如何避免使用 $key(pack 等)和 rtrim 太多,我没有'不编写代码,但请您编辑以上两个函数以提供不使用不必要元素的有效解决方案。
    • 只要我取回原始字符串,我就可以完全避免传递 $account,而且生成的令牌每次都应该是随机的/不同的。
    • 我已经扩展了我的答案,但我不会重写加密方法。他们大部分都很好。只有rtrim()urlencode() 注释很重要。
    • 你必须传递 $account (或类似的东西),因为你需要一个加密/解密密钥。它可以是应用程序机密(而不是特定于帐户的机密),但越独特越好。
    • 是的,每个帐户密码都是 32 个字符,每个帐户密码都不同,请继续重写加密和解密函数
    猜你喜欢
    • 2011-01-27
    • 2015-11-10
    • 1970-01-01
    • 2023-03-03
    • 2019-04-13
    • 2013-08-13
    • 2012-07-27
    • 2015-10-09
    • 2011-10-13
    相关资源
    最近更新 更多