Skip to content

Instantly share code, notes, and snippets.

@vietdien2005
Created April 9, 2017 09:54
Show Gist options
  • Save vietdien2005/3ed77fc817a25a65e0bcec70fa182988 to your computer and use it in GitHub Desktop.
Save vietdien2005/3ed77fc817a25a65e0bcec70fa182988 to your computer and use it in GitHub Desktop.
encrypt decrypt id number
function encryptor($action, $string) {
    $output = false;

    $encrypt_method = "AES-256-CBC";
    //pls set your unique hashing key
    $secret_key = 'dj7oiop1mkdp251EnCrIt4QKq4988w6a';
    $secret_iv = 'oeuGJW0cBI4ye998Z7435sj9EkGnDD34';

    // 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);

    //do the encyption given text/string/number
    if( $action == 'encrypt' ) {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    }
    else if( $action == 'decrypt' ){
        //decrypt the given text/string/number
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }

    return $output;
}

for ($i=0; $i < 100; $i++) { 
    $encrypt = encryptor('encrypt', $i);
    var_dump($encrypt);
    $decrypt = encryptor('decrypt', $encrypt);
    var_dump($decrypt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment