Simple Encrypt and Decrypt using PHP and aes-256


  

$key ="<SOME_KEY_HERE>";

$iv='<SOME_KEY_HERE>';


function encryptFile($encKey, $encIV, $instr, $outPath) {

    $sourceFile=$instr;

    $key = base64_decode($encKey);

    $iv = base64_decode($encIV);

    $encrypter = 'aes-256-cbc';

    $encryptedString = openssl_encrypt($sourceFile, $encrypter, $key, 0, $iv);

    return $encryptedString;

}


function decryptFile($encKey, $encIV, $instr, $outPath) {

    $encryptedString=$instr;

    $key = base64_decode($encKey);

    $iv = base64_decode($encIV);

    $encrypter = 'aes-256-cbc';

    $decrypted = openssl_decrypt($encryptedString, $encrypter, $key, 0, $iv);

    return $decrypted;

}


$encstr = encryptFile ($key, $iv,"<TEXT TO ENCRYPT>","");

echo $encstr; //encrypted text here


$Dencstr = decryptFile ($key, $iv,$encstr,"");

echo "<br>";

echo $Dencstr;

0 comments :