我一直在尝试在服务器端、PHP 和客户端上实现 mcrypt 加密/解密技术.我正在尝试使用 mcrypt.js
库作为:
I have been trying to implement mcrypt encryption/ decryption technique on both server end, PHP and client end. I am trying to use mcrypt.js
library at the moment as:
<?php
$key = 'testtesttesttesttesttesttesttest';
function string_encrypt($string, $key) {
$crypted_text = mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
$key,
$string,
MCRYPT_MODE_ECB
);
return base64_encode($crypted_text);
}
function string_decrypt($encrypted_string, $key) {
$decrypted_text = mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$key,
base64_decode($encrypted_string),
MCRYPT_MODE_ECB
);
return trim($decrypted_text);
}
echo 'Provided Text: '.$test_str = 'This is test message.';
echo '<br />';
echo 'Encyrpted Value: '.$enc_str = string_encrypt($test_str, $key);
echo '<br />';
echo 'Decrypted Value: '.string_decrypt($enc_str, $key);
echo '<br />';
?>
<script src='rijndael.js'></script>
<script src='mcrypt.js'></script>
<script src='base64v1_0.js'></script>
<script lang='javascript'>
var enc_str = mcrypt.Encrypt('<?php echo $test_str ?>','');
enc_str = B64.encode(enc_str);
alert(enc_str);
// I don't get this same as encypted PHP text. i.e. $enc_str
var dec_str = B64.decode('<?php echo $enc_str ?>');
alert(mcrypt.Decrypt(dec_str,''));
// I don't get this same as decypted PHP text.
// i.e. string_decrypt($enc_str)
</script>
我在 mcrypt.js 库中使用了以下这些私有变量.
I have used these following private vars at the mcrypt.js library.
var cMode='ecb';
var cCipher='rijndael-256';
var cKey='testtesttesttesttesttesttesttest';
//I am providing the same key
正如我上面评论的,为什么 enc_str
不等于 $enc_str
以及为什么它是 mcrypt.Decrypt('<?php echo $enc_str?>', '')
不等于 string_decrypt($enc_str, $key)
?
As I commented above, why is it enc_str
not equal as $enc_str
and why is it mcrypt.Decrypt('<?php echo $enc_str ?>', '')
not equal as string_decrypt($enc_str, $key)
?
更新的问题:
我尝试了 base64 编码/解码甚至 hex2bin/bin2hex 来解析这些字符串,但这两个产生了以下结果:
I tried both base64 encode/ decode and even hex2bin/ bin2hex to parse those strings but these two produced following results:
使用 Hex2bin/Bin2hex
PHP 结果:
Provided Text: This is test message.
Encyrpted Value: a51e970427ec8f666a5684cc1712ad03b29889cc10f4ccbf55733564d11c0386
Decrypted Value: This is test message.
JS 结果:
Provided Text:This is test message.
Mcrypted value:¥'ìfjV̲ÌôÌ¿Us5dÑ
Encyrpted Value:a51e970427ec8f666a5684cc1712ad03b29889cc10f4ccbf55733564d11c0386
After Hex to Bin Text:¥'ìfjV̲ÌôÌ¿Us5dÑ
Decrypted Value:This is test message.�����������
/*These diamond with question mark is produced while decypting the value.*/
<小时>
使用 Base64 编码/解码:
PHP 结果:
Provided Text: This is test message.
Mcrypt encrypted value : ¥—'ìfjV„̲˜‰ÌôÌ¿Us5dц
/*
Here mcrypted value provided by JS and PHP is different
That is causing to produce different value at two ends
*/
Encyrpted Value: pR6XBCfsj2ZqVoTMFxKtA7KYicwQ9My/VXM1ZNEcA4Y=
Decrypted Value: This is test message.
JS 结果:
Provided Text:This is test message.
Mcrypted value:¥'ìfjV̲ÌôÌ¿Us5dÑ
Encyrpted Value:wqUewpcEJ8Oswo9malbChMOMFxLCrQPCssKYwonDjBDDtMOMwr9VczVkw5EcA8KG
After Base64 Decode:¥'ìfjV̲ÌôÌ¿Us5dÑ���
Decrypted Value:This is test message.�����������bFaêF«+JéÓ!ÆÖ
并且在这两种情况下,UTf-8 内容都无法在 JS 端解密.
And on both cases, UTf-8 content can not be decrypted on JS end.
*链接:*
Mcrypt JS 库
Base64 JS 库
主要问题似乎是您的 string_encrypt
和 string_decrypt
PHP 函数无法访问$key
变量,因此对于加密密钥 mcrypt_encrypt
使用