用变音符号加密和解密
I have these code for encrypt and decrypt.
It works good for text (for example: "This is a text"), which is withnout diacritics (that means without : ěščřžýáíéúů). But I need encrypt and decrypt text with this special letters (with : ěščřžýáíéúů).
Can somebody help me, please?
Thank so much for every answer and help.
Have a nice day. M.
define ("ENCRYPTION_KEY", "QaY7e4d1c");
$string= "This is a text"; // -> this work alright
//$string= "áýžřčšě"; I NEED THIS TEXT ENCRYPT AND DECRTYPT
echo $encrypted = encrypt($string, ENCRYPTION_KEY);
echo "<br />";
echo $decrypted = decrypt($encrypted, ENCRYPTION_KEY);
function encrypt ($pure_string,$encryption_key)
{
$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH,MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size,MCRYPT_RAND);
$encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH,$encryption_key,utf8_encode($pure_string),MCRYPT_MODE_ECB,$iv);
return $encrypted_string;
}
function decrypt ($encrypted_string,$encryption_key)
{
$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH,MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size,MCRYPT_RAND);
$decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH,$encryption_key,$encrypted_string,MCRYPT_MODE_ECB ,$iv);
return $decrypted_string;
}
我有这些代码用于加密和解密。 p>
它适用于文本(例如:”这是一个文本“),它没有变音符号(这意味着没有:ěščřžýáíéú......)。 But 我需要用这个特殊的字母加密和解密文本(用:ěščřžýáíéúů)。 p>
有人可以帮助我吗? p>
非常感谢每一个人 回答和帮助。 p>
祝你有个愉快的一天。 M。 p>
define(“ENCRYPTION_KEY”,“QaY7e4d1c”);
$ string =“这是一个文本”; // - &gt; 这项工作好吧
// $ string =“áýžřčšě”; 我需要这个文本加密和DECRTYPT
echo $ encrypted = encrypt($ string,ENCRYPTION_KEY);
echo“&lt; br /&gt;”;
echo $ decrypted = decrypt($ encrypted,ENCRYPTION_KEY);
function encrypt($ pure_string ,$ encryption_key)
{
$ iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH,MCRYPT_MODE_ECB);
$ iv = mcrypt_create_iv($ iv_size,MCRYPT_RAND);
$ encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH,$ encryption_key,utf8_encode($ pure_string), MCRYPT_MODE_ECB,$ iv);
return $ encrypted_string;
}
function decrypt($ encrypted_string,$ encryption_key)
{
$ iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH,MCRYPT_MODE_ECB);
$ iv = mcrypt_create_iv($ iv_size,MCRYPT_RAND );
$ decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH,$ encryption_key,$ encrypted_string,MCRYPT_MODE_ECB,$ iv);
return $ decrypted_string;
}
code> pre>
div>
You're calling utf8_encode
in your encryption function, but not calling utf8_decode
when you decrypt, so your functions as they stand don't complement each other.
I'd recommend removing the call to utf8_encode
entirely. mcrypt_encrypt
doesn't care what encoding your string uses, so whatever you pass in will be what you get back out. Your script works fine for me if I remove it:
$encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, $pure_string, MCRYPT_MODE_ECB, $iv);
I'd also suggest reading this: https://paragonie.com/blog/2015/05/if-you-re-typing-word-mcrypt-into-your-code-you-re-doing-it-wrong