为什么这些奇怪的字符出现在mcrypt中?

时间:2021-07-10 00:26:05

I encrypt and decrypt successfully, but when I decrypt the value, there appears strange characters at the end of the string, "���". The initial $_POST['value'] do not have any blank space or any strange character.

我成功加密和解密,但是当我解密该值时,字符串末尾会出现奇怪的字符“ ”。最初的$ _POST ['value']没有任何空格或任何奇怪的字符。

How can I solve this?

我怎么解决这个问题?

I encrypt with this:

我用这个加密:

$key = 'my key';
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);        
$id = mcrypt_generic($td, $_POST['value']);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

I decrypt with this:

我用这个解密:

$key = 'my key';
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$id = mdecrypt_generic($td, $_COOKIE['value']);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

5 个解决方案

#1


It is just padding the result based on the block size used. If you use rtrim(), you will get rid of them.

它只是根据使用的块大小填充结果。如果你使用rtrim(),你将摆脱它们。

#2


These are unicode entities. Try utf8_decode() on the output.

这些是unicode实体。在输出上尝试utf8_decode()。

There is also a related closed PHP Bug

还有一个相关的已关闭的PHP Bug

mcrypt produces binary output which is neither iso-8859-1 nor utf-8 therefore you should tell your database that that data is binary stuff, not text data.

mcrypt生成的二进制输出既不是iso-8859-1也不是utf-8,因此你应该告诉你的数据库该数据是二进制文件,而不是文本数据。

I also found this info on the mcrypt example page.

我也在mcrypt示例页面上找到了这个信息。

I could En/Decrypt within VB and PHP just fine But when I tried to encrypt one in VB and decrypt in PHP I got the wrong values with the mcrypt function alone

我可以在VB和PHP中使用En / Decrypt就好了当我尝试在VB中加密一个并在PHP中解密时,我单独使用mcrypt函数得到了错误的值

I found that at least with VB9 that the stream encryption uses a UTF char that is the value for how many missing bytes left in the 8 bit stream.

我发现至少在VB9中,流加密使用的是UTF字符,它是8位流中剩余字节数的值。

So if you encrypt 1234 it will add chr(4) four times (the amount of missing bytes) In php use chr otherwise most browsers/client cant read it. Im not good at explaining things but the php code I figured out is below.

因此,如果您加密1234,它将添加chr(4)四次(丢失的字节数)在php中使用chr否则大多数浏览器/客户端无法读取它。我不擅长解释事情,但我想出的PHP代码如下。

#3


Try to switch to cfb instead of ecb mode, then rewrite the functions to use the same IV for both encryption and decryption. An easy way to do that is passing IV along with the encrypted data (I assume you've got something like "return $encrypted_data" at the end of your function, you may return $iv.$encrypted_data instead of $encrypted_data itself, and then get the IV back with substr() ). Worked for me.

尝试切换到cfb而不是ecb模式,然后重写函数以使用相同的IV进行加密和解密。一个简单的方法是将IV与加密数据一起传递(我假设你在函数末尾有类似“return $ encrypted_data”的东西,你可以返回$ iv。$ encrypted_data而不是$ encrypted_data本身,以及然后使用substr()获取IV。为我工作。

#4


Use the following function for decrypted text.

对解密文本使用以下函数。

function pkcs5_unpad($text)
{
    $pad = ord($text{strlen($text)-1});
    if ($pad > strlen($text))
        return false;
    if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
        return false;
    return substr($text, 0, -1 * $pad);
}

#5


Not is from VB is from PHP(encrypt) and PHP(decrypt) and the page is UTF-8 and the database is UTF-8 and the connection is UTF-8.

不是来自VB是来自PHP(加密)和PHP(解密),页面是UTF-8,数据库是UTF-8,连接是UTF-8。

Not is from all. I encrypt two passphrases. The first have strange characters and the last doesn't have. All values are POST from the same <form>.

不是所有人。我加密了两个密码。第一个有奇怪的字符,最后一个没有。所有值都来自同一

的POST。

#1


It is just padding the result based on the block size used. If you use rtrim(), you will get rid of them.

它只是根据使用的块大小填充结果。如果你使用rtrim(),你将摆脱它们。

#2


These are unicode entities. Try utf8_decode() on the output.

这些是unicode实体。在输出上尝试utf8_decode()。

There is also a related closed PHP Bug

还有一个相关的已关闭的PHP Bug

mcrypt produces binary output which is neither iso-8859-1 nor utf-8 therefore you should tell your database that that data is binary stuff, not text data.

mcrypt生成的二进制输出既不是iso-8859-1也不是utf-8,因此你应该告诉你的数据库该数据是二进制文件,而不是文本数据。

I also found this info on the mcrypt example page.

我也在mcrypt示例页面上找到了这个信息。

I could En/Decrypt within VB and PHP just fine But when I tried to encrypt one in VB and decrypt in PHP I got the wrong values with the mcrypt function alone

我可以在VB和PHP中使用En / Decrypt就好了当我尝试在VB中加密一个并在PHP中解密时,我单独使用mcrypt函数得到了错误的值

I found that at least with VB9 that the stream encryption uses a UTF char that is the value for how many missing bytes left in the 8 bit stream.

我发现至少在VB9中,流加密使用的是UTF字符,它是8位流中剩余字节数的值。

So if you encrypt 1234 it will add chr(4) four times (the amount of missing bytes) In php use chr otherwise most browsers/client cant read it. Im not good at explaining things but the php code I figured out is below.

因此,如果您加密1234,它将添加chr(4)四次(丢失的字节数)在php中使用chr否则大多数浏览器/客户端无法读取它。我不擅长解释事情,但我想出的PHP代码如下。

#3


Try to switch to cfb instead of ecb mode, then rewrite the functions to use the same IV for both encryption and decryption. An easy way to do that is passing IV along with the encrypted data (I assume you've got something like "return $encrypted_data" at the end of your function, you may return $iv.$encrypted_data instead of $encrypted_data itself, and then get the IV back with substr() ). Worked for me.

尝试切换到cfb而不是ecb模式,然后重写函数以使用相同的IV进行加密和解密。一个简单的方法是将IV与加密数据一起传递(我假设你在函数末尾有类似“return $ encrypted_data”的东西,你可以返回$ iv。$ encrypted_data而不是$ encrypted_data本身,以及然后使用substr()获取IV。为我工作。

#4


Use the following function for decrypted text.

对解密文本使用以下函数。

function pkcs5_unpad($text)
{
    $pad = ord($text{strlen($text)-1});
    if ($pad > strlen($text))
        return false;
    if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
        return false;
    return substr($text, 0, -1 * $pad);
}

#5


Not is from VB is from PHP(encrypt) and PHP(decrypt) and the page is UTF-8 and the database is UTF-8 and the connection is UTF-8.

不是来自VB是来自PHP(加密)和PHP(解密),页面是UTF-8,数据库是UTF-8,连接是UTF-8。

Not is from all. I encrypt two passphrases. The first have strange characters and the last doesn't have. All values are POST from the same <form>.

不是所有人。我加密了两个密码。第一个有奇怪的字符,最后一个没有。所有值都来自同一

的POST。