我需要帮助来加密字符串并以二进制形式返回加密结果(例如10101111101)。并且还需要帮助来解密二进制结果。请指导我。
我使用了这段代码.. 它对字符串的加密/解密工作完美。但问题是加密结果包含“特殊字符,运算符等” 我不想要这个。我需要加密的结果只有“字母数字,数字,二进制,字母”function encryptIt( $q ) {
$cryptKey = 'qJB0rGtIn5UB1xG03efyCp';
$qEncoded = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $q, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );
return( $qEncoded );}
function decryptIt( $q ) {
$cryptKey = 'qJB0rGtIn5UB1xG03efyCp';
$qDecoded = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), base64_decode( $q ), MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ), "\0");
return( $qDecoded );}
答案 0 :(得分:1)
解决此问题的一种简单方法是在加密后和解密之前将密文转换为十六进制和从十六进制转换。
代码:
$string = "10101111101";
$encrypted = encryptIt($string);
$decrypted = decryptIt($encrypted);
// Change \n to <br> 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<strlen($string); $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