为什么 mcrypt_encrypt() 将二进制字符放在我的字符串末尾?

Why is mcrypt_encrypt() putting binary characters at the end of my string?(为什么 mcrypt_encrypt() 将二进制字符放在我的字符串末尾?)
本文介绍了为什么 mcrypt_encrypt() 将二进制字符放在我的字符串末尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这是一个加密和解密数据的 PHP 演示脚本:

Here is a PHP demo script that encrypts and decrypts data:

<?

$encryptionkey = 'h8y2p9d1';

$card_nbr = "1234";
echo "original card_nbr: $card_nbr <br>
";

$card_nbr_encrypted=encrypt_data($card_nbr);
echo "card_nbr_encrypted: $card_nbr_encrypted <br>
";

$card_nbr_decrypted=decrypt_data($card_nbr_encrypted);
echo "card_nbr_decrypted: $card_nbr_decrypted <br>
";

$len=strlen($card_nbr_decrypted);
echo "length: $len <br>
";



function encrypt_data($text){
  global $encryptionkey;
  $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  $encrypted_text = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $encryptionkey, $text, MCRYPT_MODE_ECB, $iv);
  return $encrypted_text;
}

function decrypt_data($text){
  global $encryptionkey;
  $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  $decrypted_text = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $encryptionkey, $text, MCRYPT_MODE_ECB, $iv);
  return $decrypted_text;
}

?>


输出为:

original card_nbr: 1234
card_nbr_encrypted: vY¨(Z$<§G3--ù32×rz¨V
card_nbr_decrypted: 1234  (and 28 binary characters)
length: 32 


输出成功解密,但最后添加了 28 个二进制字符.在 Firefox 中查看 HTML 源代码时最容易看到这一点.32 的字符串长度也证明了这一点.有什么想法吗?


The output is successfully decrypted, but 28 binary characters are added to the end. This can most easily be seen in Firefox, when viewing HTML source. The string length of 32 also demonstrates this. Any ideas?

推荐答案

使用空字符 填充返回的字符串以填充 n * blocksize 个字节,因此这就是您看到额外数据的原因.

The returned string is padded out to fill n * blocksize bytes using the null character so that is why you are seeing the extra data.

如果你运行 $card_nbr_decrypted= rtrim($card_nbr_decrypted, ""); 它应该返回实际数据.

If you run $card_nbr_decrypted= rtrim($card_nbr_decrypted, ""); it should return the actual data.

这篇关于为什么 mcrypt_encrypt() 将二进制字符放在我的字符串末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Facebook Login with Strict Mode for Redirect URIs(使用重定向 URI 的严格模式登录 Facebook)
How to retrieve post ID for a post i#39;ve just made via the Facebook API(如何检索我刚刚通过 Facebook API 发布的帖子的帖子 ID)
Facebook PHP SDK logout(Facebook PHP SDK 注销)
Facebook SDK and Graph API Comment Deleting Error(Facebook SDK 和 Graph API 评论删除错误)
PHP amp; Facebook: facebook-debug a URL using CURL and Facebook debugger(PHP amp;Facebook: facebook-debug a URL using CURL and Facebook debugger)
Can#39;t delete photo via Facebook API?(无法通过 Facebook API 删除照片?)