解决此问题的一种简单方法是在加密之后和解密之前将密文转换为十六进制和从十六进制转换密文。
码:
$string = "10101111101";
$encrypted = encryptIt($string);
$decrypted = decryptIt($encrypted);
// Change \n to
for newline in web browser
echo $string."\n";
echo $encrypted."\n";
echo $decrypted."\n";
function encryptIt( $q ) {
$cryptKey = 'qJB0rGtIn5UB1xG03efyCp';
$qEncoded = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $q, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );
$qEncoded = strToHex( $qEncoded );
return( $qEncoded );
}
function decryptIt( $q ) {
$q = hexToStr( $q );
$cryptKey = 'qJB0rGtIn5UB1xG03efyCp';
$qDecoded = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), base64_decode( $q ), MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ), "\0");
return( $qDecoded );
}
function strToHex($string) {
$hex = '';
for ($i=0; $i
$ord = ord($string[$i]);
$hexCode = dechex($ord);
$hex .= substr('0'.$hexCode, -2);
}
return strToUpper($hex);
}
function hexToStr($hex) {
$string='';
for ($i=0; $i < strlen($hex)-1; $i+=2){
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}输出:
10101111101
62554A5951644A4B7552564C52396A69686A5A6F4A453539534850517A533573583331316D5372676C6B493D
10101111101