encryption - PHP decryption fails on some strings with trim()'s -
i'm having trouble decrypting values end %3d%3d. upon decrypting return value that's illegible. encrypted value being passed via querystring, i've run test looping through values 0 200 rule out problems url encoding.
encryption , decryption functions:
function encryptvalue($encrypt) { $key = variable_get_local("privatekey", $default = ""); $iv = mcrypt_create_iv(mcrypt_get_iv_size(mcrypt_rijndael_256, mcrypt_mode_ecb), mcrypt_rand); $passcrypt = trim(mcrypt_encrypt(mcrypt_rijndael_256, $key, trim($encrypt), mcrypt_mode_ecb, $iv)); $encode = urlencode(base64_encode($passcrypt)); return $encode; } function decryptvalue($decrypt) { $key = variable_get_local("privatekey", $default = ""); $decoded = base64_decode(urldecode($decrypt)); $iv = mcrypt_create_iv(mcrypt_get_iv_size(mcrypt_rijndael_256, mcrypt_mode_ecb), mcrypt_rand); $decrypted = trim(mcrypt_decrypt(mcrypt_rijndael_256, $key, trim($decoded), mcrypt_mode_ecb, $iv)); return $decrypted; }
i've tried keeping iv value identical across encryption , decryption doesn't change output. i've tried removing trim()
around trim($decoded)
didn't change either.
below used identify problem. between 0 , 200 encryption produce value ending on %3d%3d 9 times , result in decryption failing.
for($i=0;$i<200;$i++) { echo encryptvalue($i) . "<br/>"; echo decryptvalue(encryptvalue($i)) . "<br/><hr/>"; }
thanks reading.
your problem trim
function, shouldnt used. dont trim encoded data data removed, vital key have noticed.
it works this:
function encryptvalue($encrypt) { $key = variable_get_local("privatekey", $default = ""); $iv = mcrypt_create_iv(mcrypt_get_iv_size(mcrypt_rijndael_256, mcrypt_mode_ecb), mcrypt_rand); $passcrypt = mcrypt_encrypt(mcrypt_rijndael_256, $key, trim($encrypt), mcrypt_mode_ecb, $iv); $encode = urlencode(base64_encode($passcrypt)); return $encode; } function decryptvalue($decrypt) { $key = variable_get_local("privatekey", $default = ""); $decoded = base64_decode(urldecode($decrypt)); $iv = mcrypt_create_iv(mcrypt_get_iv_size(mcrypt_rijndael_256, mcrypt_mode_ecb), mcrypt_rand); $decrypted = mcrypt_decrypt(mcrypt_rijndael_256, $key, $decoded, mcrypt_mode_ecb, $iv); return $decrypted; }
Comments
Post a Comment