【发布时间】:2015-11-06 12:57:07
【问题描述】:
我正在尝试使用 mcrypt 编写一个简单的 2 路加密类,以了解有关加密的更多信息,并且一切似乎都运行良好。我遇到的问题是我收到一个 PHP 错误,指出“IV 参数必须与块大小一样长”但是,加密和解密似乎都在工作。我对 IV 大小的理解不正确吗?任何朝着正确方向的推动将不胜感激。谢谢。
编辑:我实际上是错的,加密/解密不起作用,我不确定为什么。
Edit2:好的,我知道为什么加密不起作用(感谢 Robert),但原来的问题仍然存在。
<?php
ini_set("display_errors", 1);
class IDBCrypt {
private $iv_size, $hash_type, $hash_size, $hash_key, $encryption_key;
const SECRET_KEY = "Ep8+NFPfybsJn26ZFyPn213WTI";
const HASH_KEY = "mU2YjBiZDVmYjBiOWUyNmE";
const HASH_TYPE = "sha256";
const HASH_SIZE = 64;
/**
* For SHA256 hashing algorithm
* each digit requires 4 bits of
* memory. This means that you need
* 64 digits to represent a 256 bit
* hash, thereby making the size of
* a hash generated with this algorithm
* 256 bits, with a length of 64
*/
function __construct() {
/* Constructor */
}
function encrypt( $data ) {
// Generate an IV to encrypt with
$iv = mcrypt_create_iv( self::HASH_SIZE, MCRYPT_RAND );
$hashed_iv = hash_hmac( self::HASH_TYPE, $iv, self::HASH_KEY );
// echo $iv ."<br><br>";
// Encrypt plain text
$cipher_text = mcrypt_encrypt( MCRYPT_RIJNDAEL_128, self::SECRET_KEY, $data, MCRYPT_MODE_CBC, $hashed_iv );
// Base64 encode and salt the data
$cipher_text_64 = base64_encode( $hashed_iv . $cipher_text );
return $cipher_text_64;
}
function decrypt( $data ) {
// Base64 decode the cipher text
$ciphertext_dec = base64_decode( $data );
// retrieves the IV/salt from the cipher text
$iv_dec = substr( $ciphertext_dec, 0, self::HASH_SIZE );
// retrieves the cipher text (everything except the $iv_size in the front)
$ciphertext_dec = substr( $ciphertext_dec, self::HASH_SIZE );
$plaintext = mcrypt_decrypt( MCRYPT_RIJNDAEL_128, self::SECRET_KEY, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec );
return $plaintext;
}
}
$crypt = new IDBCrypt();
$string = $crypt->encrypt("Greetings from encryption and beyond");
echo $string . "<br>";
echo $crypt->decrypt($string);
?>
【问题讨论】:
-
你应该明确检查
mcrypt_enc_get_iv_size()function -
你在哪里设置 'iv_size' 我找不到?
-
感谢您的发现,我正疯狂地试图找出加密失败的原因。现在加密工作正常,我仍然收到原始错误“IV 参数必须与块大小一样长”。我已经编辑了代码以反映该修复。
标签: php encryption mcrypt