我正在使用以下代码来加密将传递到链接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); } }更多推荐
发布评论