Cara encrypt dan decrypt string dengan php mudah cepat

Bismillaahirrohmaanirrohiim…

Berikut ini adalah cara termudah dan tercepat encrypt decrypt teks dengan PHP, untuk melakukan enkripsi dan decrypt disini harus menggunakan kunci / key yang sama.

Caranya hanya dengan menggunakan fungsi bawaah PHP openssl_encrypt dan openssl_decrypt, berikut contoh penggunaannya:

<?php
define('ENC_IV', '1234567890123456');
define('ENC_KEY', 'kunci');
define('ENC_CIPH', 'AES-128-CTR');
define('ENC_OPT', 0);

function encrypt_text($text) {
    //$iv_length = openssl_cipher_iv_length(ENC_CIPH);
    // Using openssl_encrypt() function to encrypt the data
    $text_encrypted = openssl_encrypt($text, ENC_CIPH, ENC_KEY, ENC_OPT, ENC_IV);
    return $text_encrypted;
}

function decrypt_text($text_encrypted) {
    //$iv_length = openssl_cipher_iv_length(ENC_CIPH);
    // Using openssl_encrypt() function to encrypt the data
    $text_decrypted = openssl_decrypt($text_encrypted, ENC_CIPH, ENC_KEY, ENC_OPT, ENC_IV);
    return $text_decrypted;
}

$simple_text = 'Rasupe';

echo 'Teks asal: ' . $simple_text;
echo '<br />';

$text_encrypted = encrypt_text($simple_text);
echo 'Teks dienkripsi: ' . $text_encrypted;
echo '<br />';


$text_decrypted = decrypt_text($text_encrypted);
echo 'Teks decrypt: ' . $text_decrypted;
echo '<br />';

Demikian, semoga bermanfaat…

Bagikan
Baca Juga:   Membuat random string di PHP

You May Also Like

About the Author: rasupe

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *