Blowfish加密在php [英] Blowfish encryption in php
问题描述
函数decrypt_blowfish($ data,$ key){
$ iv = (H *,substr($ data,0,16));
$ key = pack(H *,$ key);
$ x = pack(H *,substr($ data,16));
$ res = mcrypt_decrypt(MCRYPT_BLOWFISH,$ key,$ x,MCRYPT_MODE_CBC,$ iv);
return $ res;
}
我尝试过简单:
$ pre>
function encrypt_blowfish($ data,$ key){
$ iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH,MCRYPT_MODE_CBC);
$ iv = mcrypt_create_iv($ iv_size,MCRYPT_RAND);
$ crypttext = mcrypt_encrypt(MCRYPT_BLOWFISH,$ key,$ data,MCRYPT_MODE_CBC,$ iv);
return $ crypttext;
}
但它返回strang ASCI字符而不是正确的blowfish代码。有人可以解释一下为什么,我做错了什么?
提前感谢
CH
$ code函数decrypt_blowfish($ data,$ key){
$ iv = pack(H *,substr($ data,0,16));
$ x = pack(H *,substr($ data,16));
$ res = mcrypt_decrypt(MCRYPT_BLOWFISH,$ key,$ x,MCRYPT_MODE_CBC,$ iv);
return $ res;
}
函数encrypt_blowfish($ data,$ key){
$ iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH,MCRYPT_MODE_CBC);
$ iv = mcrypt_create_iv($ iv_size,MCRYPT_RAND);
$ crypttext = mcrypt_encrypt(MCRYPT_BLOWFISH,$ key,$ data,MCRYPT_MODE_CBC,$ iv);
return bin2hex($ iv。$ crypttext);
}
$ string = encrypt_blowfish('hello world','abc123');
echo'ENCRYPTED:'。 $ string。 \\\
;
echo'DECRYPTED:'。 decrypt_blowfish($ string,'abc123');
尝试一下。在解密函数中,您将从十六进制转换为二进制,因此期望传递十六进制值。您的加密功能正在输出二进制文件,因此需要将其转换为hex,并进行上述更改。
I'm writing an encryption to my application and website, but I don't know how to correctly encrypt the string in php. Decryption is already done by this code:
function decrypt_blowfish($data,$key){
$iv=pack("H*" , substr($data,0,16));
$key=pack("H*" , $key);
$x =pack("H*" , substr($data,16));
$res = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $x , MCRYPT_MODE_CBC, $iv);
return $res;
}
I tried with simple:
function encrypt_blowfish($data,$key){
$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $data, MCRYPT_MODE_CBC, $iv);
return $crypttext;
}
But it returns strang ASCI chars instead of correct blowfish code. Could somebody explain me why, and what am I doing wrong? Thanks in advance
C.H.
function decrypt_blowfish($data,$key){
$iv=pack("H*" , substr($data,0,16));
$x =pack("H*" , substr($data,16));
$res = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $x , MCRYPT_MODE_CBC, $iv);
return $res;
}
function encrypt_blowfish($data,$key){
$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $data, MCRYPT_MODE_CBC, $iv);
return bin2hex($iv . $crypttext);
}
$string = encrypt_blowfish('hello world', 'abc123');
echo 'ENCRYPTED: ' . $string . "\n";
echo 'DECRYPTED: ' . decrypt_blowfish($string, 'abc123');
Try that. In the decryption function you are converting from hex to binary, so it is expecting a hex value to be passed. Your encryption function is outputting binary, so you need to convert it to hex with the above change.
这篇关于Blowfish加密在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!