PHP Mcrypt Encrypt Decrypt Sample

HYEONG HWAN, MUN/ 10월 18, 2014/ 미분류/ 0 comments

https://blog.lael.be/post/227

Replace your-own $key value.


function encrypt($text) {
  $key = '6wMwDU8rvMms6ZrY';// 24 bit Key
  $iv = "fYfhHeDm";// 8 bit IV
  $bit_check=8;// bit amount for diff algor.

  $text_num =str_split($text,$bit_check);
  $text_num = $bit_check-strlen($text_num[count($text_num)-1]);
  for ($i=0;$i<$text_num; $i++) {$text = $text . chr($text_num);}
  $cipher = mcrypt_module_open(MCRYPT_TRIPLEDES,'','cbc','');
  mcrypt_generic_init($cipher, $key, $iv);
  $decrypted = mcrypt_generic($cipher,$text);
  mcrypt_generic_deinit($cipher);
  return base64_encode($decrypted);
}

function decrypt($encrypted_text){
  $key = '6wMwDU8rvMms6ZrY';// 24 bit Key
  $iv = "fYfhHeDm";// 8 bit IV
  $bit_check=8;// bit amount for diff algor.

  $cipher = mcrypt_module_open(MCRYPT_TRIPLEDES,'','cbc','');
  mcrypt_generic_init($cipher, $key, $iv);
  $decrypted = mdecrypt_generic($cipher,base64_decode($encrypted_text));
  mcrypt_generic_deinit($cipher);
  $last_char=substr($decrypted,-1);
  for($i=0;$i<$bit_check-1; $i++){
    if(chr($i)==$last_char){
      $decrypted=substr($decrypted,0,strlen($decrypted)-$i);
      break;
    }
  }
  return $decrypted;
}

 


$userid_dec = str_replace("\x0", '', $userid_dec); //패딩제거
$userid_dec = str_replace("\x7", '', $userid_dec); //패딩제거

$key = ‘12345678901234567890123456789012’;
$iv = ‘1234567890123456’;
$input = “student”;

$td = mcrypt_module_open(‘rijndael-128’, ”, ‘cbc’, ”);

mcrypt_generic_init($td, $key, $iv);
$encrypted_data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

echo base64_encode($encrypted_data);

Leave a Comment

작성하신 댓글은 관리자의 수동 승인 후 게시됩니다.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*