3DES PHP加密没有正确解密(3DES PHP Encryption Not Decrypting Properly)

编程入门 行业动态 更新时间:2024-10-25 00:30:22
3DES PHP加密没有正确解密(3DES PHP Encryption Not Decrypting Properly)

我正在使用以下代码来加密将传递到链接URL末尾的外部站点的信息。 现在它可以自己进行加密和解密,但是当我进入在线Decryptors( online-domain-tools.com , tools4noobs.com )时,我看到添加了额外的符号,或者它没有显示正确的内容。 我当然是新手。 我有什么,我从其他问题拼凑而成( php-equivalent-for-java-triple-des-encryption-decryption , php-equivalent-encryption-decryption-tripledes , php-encrypt-decrypt-with-tripledes-pkcs7-和-ecb )。 感谢您的帮助或指导!

我只能在CBC上使用3DES。

PHP代码:

$key = "12AB12AB12AB12AB12AB12AB"; $iv = "12AB12AB"; $cipher = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', ''); // MESSAGE $message = "email=billysmith@afakeemail.com&account=987654321&role=2"; echo 'Message::: ' .$message .'<br />'; // ENCRYPTED $encrypted = Encryptor($message); echo 'Encrypted::: ' .$encrypted .'<br />'; // DECRYPTED $decrypted = Decryptor($encrypted); echo 'Decrypted::: ' .$decrypted .'<br />'; function Encryptor($buffer) { global $key, $iv, $cipher; // get the amount of bytes to pad $extra = 8 - (strlen($buffer) % 8); // add the zero padding if($extra > 0) { for($i = 0; $i < $extra; $i++) { $buffer .= "\0"; } } mcrypt_generic_init($cipher, $key, $iv); $result = bin2hex(mcrypt_generic($cipher, $buffer)); mcrypt_generic_deinit($cipher); return $result; } function Decryptor($buffer) { global $key, $iv, $cipher; mcrypt_generic_init($cipher, $key, $iv); $result = rtrim(mdecrypt_generic($cipher, hex2bin($buffer)), "\0"); mcrypt_generic_deinit($cipher); return $result; } function hex2bin($data) { $len = strlen($data); return pack("H" . $len, $data); }

I'm using the following code to encrypt information that will be passed to an external site on the end of the link URL. Right now it's able to do the encrypting and decrypting itself, but when I go to online Decryptors (online-domain-tools.com, tools4noobs.com) I'm seeing extra symbols added or it's not showing the right content whatsoever. Of course I'm new to this. What I have, I have pieced together from other questions (php-equivalent-for-java-triple-des-encryption-decryption, php-equivalent-encryption-decryption-tripledes, php-encrypt-decrypt-with-tripledes-pkcs7-and-ecb). Thanks for any help or direction!

I can only use 3DES with CBC.

PHP Code:

$key = "12AB12AB12AB12AB12AB12AB"; $iv = "12AB12AB"; $cipher = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', ''); // MESSAGE $message = "email=billysmith@afakeemail.com&account=987654321&role=2"; echo 'Message::: ' .$message .'<br />'; // ENCRYPTED $encrypted = Encryptor($message); echo 'Encrypted::: ' .$encrypted .'<br />'; // DECRYPTED $decrypted = Decryptor($encrypted); echo 'Decrypted::: ' .$decrypted .'<br />'; function Encryptor($buffer) { global $key, $iv, $cipher; // get the amount of bytes to pad $extra = 8 - (strlen($buffer) % 8); // add the zero padding if($extra > 0) { for($i = 0; $i < $extra; $i++) { $buffer .= "\0"; } } mcrypt_generic_init($cipher, $key, $iv); $result = bin2hex(mcrypt_generic($cipher, $buffer)); mcrypt_generic_deinit($cipher); return $result; } function Decryptor($buffer) { global $key, $iv, $cipher; mcrypt_generic_init($cipher, $key, $iv); $result = rtrim(mdecrypt_generic($cipher, hex2bin($buffer)), "\0"); mcrypt_generic_deinit($cipher); return $result; } function hex2bin($data) { $len = strlen($data); return pack("H" . $len, $data); }

最满意答案

简而言之:你的代码是正确的。 您无法使用提供的工具测试加密。

这两个工具都不允许输入您的IV。

IV应该是唯一的,可以公开转让。

通过解码使用错误的IV会在解密数据的开头给出错误的部分。

这是OO版本。 它使用零填充(内置PHP),就像你的代码一样。 如果原始消息已经对齐,它也不会填充**。

<?php $key = "12AB12AB12AB12AB12AB12AB"; $iv = "12AB12AB"; // MESSAGE $message = "email=billysmith@afakeemail.com&account=987654321&role=22"; echo 'Message::: ' . $message . PHP_EOL; $cryptor = new Crypt3Des(); $encryptedMessage = $cryptor->encrypt($message, $key, $iv); echo 'Encrypted::: ' . bin2hex($encryptedMessage) . PHP_EOL; $decryptedMessage = $cryptor->decrypt($encryptedMessage, $key, $iv); echo 'Decrypted::: ' . $decryptedMessage . PHP_EOL; class Crypt3Des { private $cipher; public function __construct() { $this->cipher = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', ''); } public function encrypt($data, $key, $iv) { mcrypt_generic_init($this->cipher, $key, $iv); $result = mcrypt_generic($this->cipher, $data); mcrypt_generic_deinit($this->cipher); return $result; } public function decrypt($encryptedData, $key, $iv) { mcrypt_generic_init($this->cipher, $key, $iv); $result = mdecrypt_generic($this->cipher, $encryptedData); mcrypt_generic_deinit($this->cipher); $result = rtrim($result, "\0"); return $result; } } // Before 5.4.0 if (!function_exists('hex2bin')) { function hex2bin($data) { $len = strlen($data); return pack("H" . $len, $data); } }

To be short: Your code is correct. You can't test your encryption with provided tools.

Both tools do not allow to enter your IV.

The IV should be unique and can be transferred publicly.

Using wrong IV by decoding gives you wrong part at the beginning of decrypted data.

Here is OO version. It uses zero padding (built in PHP), like your code. It also makes no padding **, if the original message is already aligned.

<?php $key = "12AB12AB12AB12AB12AB12AB"; $iv = "12AB12AB"; // MESSAGE $message = "email=billysmith@afakeemail.com&account=987654321&role=22"; echo 'Message::: ' . $message . PHP_EOL; $cryptor = new Crypt3Des(); $encryptedMessage = $cryptor->encrypt($message, $key, $iv); echo 'Encrypted::: ' . bin2hex($encryptedMessage) . PHP_EOL; $decryptedMessage = $cryptor->decrypt($encryptedMessage, $key, $iv); echo 'Decrypted::: ' . $decryptedMessage . PHP_EOL; class Crypt3Des { private $cipher; public function __construct() { $this->cipher = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', ''); } public function encrypt($data, $key, $iv) { mcrypt_generic_init($this->cipher, $key, $iv); $result = mcrypt_generic($this->cipher, $data); mcrypt_generic_deinit($this->cipher); return $result; } public function decrypt($encryptedData, $key, $iv) { mcrypt_generic_init($this->cipher, $key, $iv); $result = mdecrypt_generic($this->cipher, $encryptedData); mcrypt_generic_deinit($this->cipher); $result = rtrim($result, "\0"); return $result; } } // Before 5.4.0 if (!function_exists('hex2bin')) { function hex2bin($data) { $len = strlen($data); return pack("H" . $len, $data); } }

更多推荐

本文发布于:2023-07-26 17:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   PHP   DES   Properly   Decrypting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!