php - Remove Special character when decrypt by editing encrypt string -
$secretkey = "mysecretkey"; $plaintext = 'plain text here'; $iv_size = mcrypt_get_iv_size(mcrypt_rijndael_128, mcrypt_mode_cbc); $iv = mcrypt_create_iv($iv_size, mcrypt_rand); $ivdecode = base64_encode(mcrypt_create_iv($iv_size, mcrypt_rand)); $encrypted = trim(mcrypt_encrypt(mcrypt_rijndael_128, substr(sha1($secretkey), 0, 32), $plaintext, mcrypt_mode_cbc, $iv), "\0..\32"); $encrypted = $iv . $encrypted; $ciphertext_base64 = base64_encode($encrypted); #echo $ciphertext_base64 . "\n"; $decrypted = trim(mcrypt_decrypt(mcrypt_rijndael_128, substr(sha1($secretkey), 0, 32), base64_decode($ciphertext_base64), mcrypt_mode_cbc, base64_decode($ivdecode)), "\0..\32"); echo $decrypted;
when run above code got output.
»_w>ø9â„6Åkžplain text here
i can't edit $decrypted
string because can't access it. can edit $encrypted only. how can remove special characters(»_w>ø9â„6Åkž) out put editing $encrypted
string. want send encrypted text using json different server decrypt it.
it not possible split iv , encrypted data prior base64 decoding, first base64 decode , split them.
mcrypt_rijndael_128 aes has block size of 128-bits or 16-bytes. iv must size. instead of including
base64_decode($iv)
parameter create 16-byte iv. base64 decoding iv not work if is not base64 encoded, isn't in case.the key should 128, 192 or 256 bits (16, 24 or 32 bytes), correct size interoperability, not rely on padding encryption algorithms.
similarly, input encrypted , key prepare in separate statement debugging easier.
do not trim output, mcrypt_decrypt correct. padding may add additional block, required.
do not base64 decode result of decryption, plaintext not base64 encoded. – zaph edit
"text ïÕ[pi¤;køv" occurs when attempting print data string, not binary bytes have print representation , many have special characters print representation in 0x80-0xff range.
here concept, not tested, have not used php in 20 years fix errors:
$secretkey = "1234567890123456"; # note length 16-bytes, full key $plaintext = 'xxxxxxxxx'; echo $plaintext . "\n"; # --- encryption --- $key = substr(sha1($secretkey), 0, 32) $iv = mcrypt_create_iv(16, mcrypt_rand); $ciphertext = mcrypt_encrypt(mcrypt_rijndael_128, $key, $plaintext, mcrypt_mode_cbc, $iv); # prepend iv available decryption $ciphertext = $iv . $ciphertext; $ciphertext_base64 = base64_encode($ciphertext); echo $ciphertext_base64 . "\n"; # --- decryption --- $key = substr(sha1($secretkey), 0, 32) $cipher_text_iv = base64_decode($ciphertext_base64) # split iv , encrypted text $iv = substr($cipher_text_iv, 0, 16) $ciphertext = substr($cipher_text_iv, 16) $decrypted = mcrypt_decrypt(mcrypt_rijndael_128, $key, $ciphertext, mcrypt_mode_cbc, $iv); echo $decrypted;
Comments
Post a Comment