[catatan] Fungsi Sederhana Enkripsi/ Deskripsi Di PHP

Bismillah….

pagi pagi sebelum aktipitas, gw mu catet lagi, ini gw comot dari blog ini:

http://naveensnayak.wordpress.com/2013/03/12/simple-php-encrypt-and-decrypt/
http://earlysandwich.com/programming/simple-php-encrypt-decrypt-functions-81/

Okelah ini dia scriptnya:

function encrypt_decrypt($action, $string) {
    $output = false;

    $encrypt_method = "AES-256-CBC";
    $secret_key = 'This is my secret key';
    $secret_iv = 'This is my secret iv';

    // hash
    $key = hash('sha256', $secret_key);

    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);

    if( $action == 'encrypt' ) {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    }
    else if( $action == 'decrypt' ){
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }

    return $output;
}

$plain_txt = "This is my plain text";
echo "Plain Text = $plain_txt\n";

$encrypted_txt = encrypt_decrypt('encrypt', $plain_txt);
echo "Encrypted Text = $encrypted_txt\n";

$decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt);
echo "Decrypted Text = $decrypted_txt\n";

if( $plain_txt === $decrypted_txt ) echo "SUCCESS";
else echo "FAILED";

echo "\n";

Demo: http://3v4l.org/CArS9

 function simple_encrypt($text, $salt = "earlysandwich.com")
{
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
// This function will be used to decrypt data.
function simple_decrypt($text, $salt = "earlysandwich.com")
{
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}

// Encrypt a tring and echo it
$encoded = simple_encrypt("this text will be encoded and decoded" , "secured");
echo "This is encoded string: $encoded \n";

// Now to decode the encoded text
$decoded = simple_decrypt($encoded , "secured");
echo "This is decoded string: $decoded \n";

demo: http://3v4l.org/jVHrO

Ya rencana gw sih buat modul aplikasi dikantor gw, biasnya disematkan di helper… kalo yang pake framwork codeigniter. ya, kan data di kantor sifatnya -rahasia- yang tahu orang tertentu aja.

Okeh getu aja semoga bermangfaat buat gw sendiri catatannya.

Have A Nide Day!

UPDATE:

https://gen5x4.wordpress.com/2010/10/03/enkripsi-dan-dekripsi-di-php/

Demo: http://3v4l.org/SshCb

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: