Replacement of mcrypt in PHP

PHP 7 မွာ mcrypt ကို သံုးလို႕ မရေတာ့ပါဘူး။ mcrypt အစား openssl_encrypt, openssl_decrypt ကို ေျပာင္းသံုးဖုိ႕ လိုပါတယ္။ သို႕ေပမယ့္ mcrypt နဲ႕ encrypt လုပ္ထားတာေတြကို openssl_decrypt နဲ႕ ျပန္ျဖည္ရင္ အဆင္မေျပ သလို openssl_encrypt နဲ႕ လုပ္ထားရင္ mcrypt_decrypt နဲ႕ ျပန္ျဖည္ရင္ လြဲေနတာေတြ ရိွပါတယ္။ အဓိကကေတာ့ padding ပံုစံ ကြာသြားလို႕ပါ။ ဒါေၾကာင့္ system အေဟာင္းေတြ မွာ အဆင္ေျပေအာင္ မူရင္း စာ ကို padding ထပ္ျဖည့္ေပးဖို႕လိုပါတယ္။

 

<?php

function aes_128_encrypt($message,$key,$iv) {
    $message_padded = $message;

    if (strlen($message_padded) % 8) {
        $message_padded = str_pad($message_padded,
            strlen($message_padded) + 8 - strlen($message_padded) % 8, "\0");
    }
    return openssl_encrypt( utf8_encode($message_padded),'AES-128-CBC', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING , $iv);
}

function aes_128_decrypt($encrypted,$key,$iv) {
    $raw = openssl_decrypt($encrypted,'AES-128-CBC', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING , $iv);
    return rtrim($raw,"\0");
}

$key = "0123456789ABCDEF";
$iv = "0123456789ABCDEF";

//$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, "hello world", MCRYPT_MODE_CBC, $iv);
$encrypted = aes_128_encrypt("123456789",$key,$iv);
var_dump($encrypted);

//$raw = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $iv);
$raw = aes_128_decrypt($encrypted,$key,$iv);

var_dump($raw);

 


 
3 Kudos
Don't
move!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.