我正在使用以下代码对将在链接 URL 末尾传递到外部站点的信息进行加密。现在它能够自己进行加密和解密,但是当我去在线解密器(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)。感谢您的任何帮助或指导!
我只能将 3DES 与 CBC 一起使用。
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);
}