php - Encyption/Decryption results in replacement and incorrect characters at start of string -
Here are my encrypt and decrypt functions
public functions encrypt ($ text) {$ Key = hash ("md5", key); $ Iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_TWOFISH, MCRYPT_MODE_CBC), MCRYPT_RAND); $ Result = base64_encode (mcrypt_encrypt (MCRYPT_TWOFISH, $ key, $ text, MCRYPT_MODE_CBC, $ iv)); Return result; } Public Function Decrypt ($ text) {$ key = hash ("MD5", key); $ Iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_TWOFISH, MCRYPT_MODE_CBC), MCRYPT_RAND); $ Result = trim (mcrypt_decrypt (MCRYPT_TWOFISH, $ of, base64_decode ($ text), MCRYPT_MODE_CBC, $ iv)); Return result; } The encryption runs on the JSON string to store it as a text file and after that the resulting string is the decrypted replacement and / or false characters in front of the string:
Expected:
Genuine:{"players": [{"label": "...
ΣΈ! G @ $ {WR bel ":" ...
If there is any difference It does not matter if the actual placement / incorrect character I refresh the page on the same file.
I feel that I am not clear, thanks in advance for help!
< Div class = "after" text "itemprop =" text ">
In case anyone comes across this ...
The fourth file needs to be prepended, so before encoding Like:
Public Function Encrypt ($ text) {$ key = hash ("md5", key); $ iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_TWOFISH, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM); $ Result = base 6 4_encode ($ iv.mcrypt_encrypt (MCRYPT_TWOFISH, $ key, $ text, MCRYPT_MODE_CBC, $ iv)); Return result; } Then when decrypting will take the fourth from decode string and its use is to decrypt, so as to:
public function decrypt ($ text) {$ Key = hash ("md5", key); $ Decode = base64_decode ($ text); $ Iv = substr ($ decode, 0, 16); $ Decrypt = substrate ($ decode, 16); $ Result = mcrypt_decrypt (MCRYPT_TWOFISH, $ key, $ decrypt, MCRYPT_MODE_CBC, $ iv); Return result; }
Comments
Post a Comment