Search code examples
phpencryptionauth-token

encrypted tokens in php


I'm new to encryption and utilizing this class to get tokens, Can someone help me understand what's going on these two functions with the help of references, video tutorials etc. for a deeper understanding. Purpose is to understand as well as implement in other languages using the same technique.

class Crypt {

public static function encrypt($data, $secret) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $key = pack('H*', $secret);
    return base64_encode($iv . mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv));
}

public static function decrypt($data, $secret) {
    $data = base64_decode($data);
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = substr($data, 0, $iv_size);
    $data = substr($data, $iv_size);
    $key = pack('H*', $secret);
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv), chr(0));
}
}

thanks


Solution

  • as well as implement in other languages using the same technique

    i doubt that ,)

    Not an easy topic. You will have to dig into different cyphers and encryptions, especially RIJNDAEL_128 and CBC, like you posted.

    If you want to learn about rinjndael, then look for AES (Advanced Encryption Standard).

    SPEC You may find the offical specification at: http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf

    Book This is the AES capther out of the crypto-textbook of Christof Paar and Jan Pelzl: http://wiki.crypto.rub.de/Buch/download/Understanding-Cryptography-Chapter4.pdf

    Tutorial

    Videos

    Animation/Presentation

    PHP Manual - Extension mCrypt

    Code