[英]mcrypt_encrypt fails to initialise
I am trying to encrypt some data in PHP using the Rijndael cipher in CBC mode with a 256bit key but for some reason I get the following error message: 我正在尝试使用Rijndael密码在CBC模式下使用256位密钥对PHP中的某些数据进行加密,但是由于某些原因,我收到以下错误消息:
mcrypt_encrypt() Module initialization failed mcrypt_encrypt()模块初始化失败
My code: 我的代码:
$hashKey = hash('sha256',$key);
$iv = hash('sha256',$hashKey);
// ------Cipher-------------key-------------Data-------------Mode---------IV--
$encryptedQuestion = base64_encode(mcrypt_encrypt('MCRYPT_RIJNDAEL_256', $hashKey , $_POST['question'], MCRYPT_MODE_CBC, $iv));
Can anyone see whats wrong with this? 谁能看到这有什么问题吗?
There are a few issues with the code that I can spot: 我可以发现一些代码问题:
Your $iv
should not be dependent on $hashKey
; 您的
$iv
不应依赖于$hashKey
; rather, you should create it separately using mcrypt_create_iv()
. 相反,您应该使用
mcrypt_create_iv()
单独创建它。
Your $hashKey
should be binary rather than textual. 您的
$hashKey
应该是二进制而不是文本。
MCRYPT_RIJNDAEL_256
is a constant, it should not be passed as a string. MCRYPT_RIJNDAEL_256
是一个常数,不应将其作为字符串传递。
The following code is more verbose than yours, but it should give you an insight in the steps required to encrypt something: 以下代码比您的代码更冗长,但是它应该使您了解加密某些内容所需的步骤:
$crypto = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($crypto), MCRYPT_DEV_URANDOM);
$hkey = hash('sha256', $key, true);
mcrypt_generic_init($handle, $hkey, $iv);
$enc_question = mcrypt_generic($handle, $_POST['question']);
mcrypt_generic_deinit($handle);
mcrypt_module_close($handle);
I've also left out any error checks. 我也没有进行任何错误检查。
I got a similar error. 我也遇到类似的错误。 I assigned the constants to a variable and passed the variable and that was the cause of the error.
我将常量分配给变量,然后将其传递给变量,这就是错误的原因。
This failed for me > return new encKey($cipher_name, $mode, $value); 这对我来说失败了>返回新的encKey($ cipher_name,$ mode,$ value);
Now this works > return new encKey(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB, $value); 现在可以正常工作>返回新的encKey(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB,$ value);
In php 5.6 try mcrypt_ecb to overcome the invalid key issue 在php 5.6中尝试使用mcrypt_ecb来解决无效密钥问题
$choice ="2";
$key = "1234";
$key = hash("sha512", $key, TRUE);
for ($x = 0; $x < 8; $x++) {
$key = $key . substr($key, $x, 1);
}
$msg = "pato";
echo("key is".$key." \n");
if ($msg == ''){
die("Please enter a text to encrypt! ");
}
if ($key == ''){
die("Please enter a key! ");
}
function pkcs5_pad($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function pkcs_pad($text, $blocksize) {
//$pad = $blocksize - (strlen($text) % $blocksize);
$tes=substr($text,0,$blocksize) ;
return $tes;
}
function encryptnow( $thekey, $themsg)
{
$padded = pkcs5_pad($themsg, mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
echo("padded".$padded."\n");
$keypad = pkcs_pad($thekey, mcrypt_get_key_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
$i;
//$iv=pkcs5_pad($i, mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
//$iv = mcrypt_create_iv(8, MCRYPT_DEV_RANDOM);
// echo("padded".$keypad."\n");
$encrypted = base64_encode(mcrypt_ecb(MCRYPT_3DES, $keypad, $padded, MCRYPT_MODE_CBC));
echo "<html><hr size='2' ></html>";
echo "<P><P><b>Plain Text : </b>";
echo($themsg);
echo "<p><b>Cipher Text : </b> ";
echo "$encrypted";
die();
}
if ($choice == '2'){
encryptnow($key, $msg);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.