php decrypt string with MCRYPT_RIJNDAEL_256 -


<?php function encrypt ($key,$iv,$str) {        $block=mcrypt_get_block_size(mcrypt_rijndael_256, mcrypt_mode_cbc);   $padding=$block-(strlen($str) % $block);   $str.=str_repeat(chr($padding), $padding);     $encryptxt=mcrypt_encrypt(mcrypt_rijndael_256,$key,$str,mcrypt_mode_cbc,$iv);     $encryptxt64=base64_encode($encryptxt);   return $encryptxt64; } function decrypt ($key,$iv,$str) {        $block=mcrypt_get_block_size(mcrypt_rijndael_256, mcrypt_mode_cbc);   $padding=$block-(strlen($str) % $block);   $str.=str_repeat(chr($padding), $padding);     $decryptxt=mcrypt_decrypt(mcrypt_rijndael_256,$key,$str,mcrypt_mode_cbc,$iv);     $decryptxt64=base64_decode($decryptxt);   return $decryptxt64; } echo encrypt("1234567890123456","12345678901234561234567890123456","test")."\n<br/>"; echo decrypt("1234567890123456","12345678901234561234567890123456","xhqkvrq6fxehoggmrkoek04146m2l9bv1scp6c1qcyg=")."\n<br/>"; ?> 

i found way can encrypt string,works fine,but when tried decrypt string,above codes not working. decrypt output likes

� s�'=�ɚ?�

does know how fix decrypt part?thanks in advance!

as stated in comments have

  //encrypt  $encryptxt=mcrypt_encrypt(mcrypt_rijndael_256,$key,$str,mcrypt_mode_cbc,$iv);     $encryptxt64=base64_encode($encryptxt);   //decrypt     $decryptxt=mcrypt_decrypt(mcrypt_rijndael_256,$key,$str,mcrypt_mode_cbc,$iv);     $decryptxt64=base64_decode($decryptxt); 
  1. encrypt
  2. base64 encode
  3. decrypt
  4. base64 decode

it should filo ( first in last out )

  1. encrypt
  2. base64 encode
  3. base64 decode
  4. decrypt

that way decrypting output of encrypt , not base64 encoded output,

like so:

$encryptxt=mcrypt_encrypt(mcrypt_rijndael_256,$key,$str,mcrypt_mode_cbc,$iv);     $encryptxt64=base64_encode($encryptxt);  //decrypt   $decryptxt64=base64_decode($str); $decryptxt=mcrypt_decrypt(mcrypt_rijndael_256,$key,$decryptxt64,mcrypt_mode_cbc,$iv);   

as note mcrypt_rijndael_256 not aes 256, need mcrypt_rijndael_128 32byte key, in regards 128 better, ( 128 being smaller block cypher )

one other thing suggest doing taking md5 hash of input string , preending input string, before encryption. when unecrypt can substr first 32 characters , use check input. need know input string in order see if decrypted. but, hashing , comparing no longer need know check works.

so ( haven't tested should close )

function encrypt ($key,$iv,$str) {        $block=mcrypt_get_block_size(mcrypt_rijndael_256, mcrypt_mode_cbc);   $padding=$block-(strlen($str) % $block);   $str.=str_repeat(chr($padding), $padding);      ///prepend md5 hash of plain text input before encrypting ( can check later )   $str = md5( $str ) . $str;      $encryptxt=mcrypt_encrypt(mcrypt_rijndael_256,$key,$str,mcrypt_mode_cbc,$iv);     $encryptxt64=base64_encode($encryptxt);   return $encryptxt64; } function decrypt ($key,$iv,$str) {        $block=mcrypt_get_block_size(mcrypt_rijndael_256, mcrypt_mode_cbc);   $padding=$block-(strlen($str) % $block);   $str.=str_repeat(chr($padding), $padding);      $decryptxt64=base64_decode($str);   $decryptxt=mcrypt_decrypt(mcrypt_rijndael_256,$key,$decryptxt64,mcrypt_mode_cbc,$iv);       ///separate md5 hash other text, , hash other text , compare hash included when encrypting  if = worked.   /// safe use md5 here because part of encrypt, , not accessible until decrypted.   ///it's sole purpose give 2 things can compare check decryption worked   $hash = substr( $decryptxt, 0, 32); //find first 32 characters (md5 output 32 characters long )   $decryptxt = substr($decryptxt, 33); //find after fist 32   if( $hash != md5($decryptxt) ){         die( 'fail' ); /// or other error   }     return $decryptxt64; } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -