0

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

user2727195
  • 7,122
  • 17
  • 70
  • 118

1 Answers1

0

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

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • curious, why it's difficult to implement in other languages, for instance I'm considering it for node.js, why I like this algorithm because it generates unique tokens each time for the same string – user2727195 Oct 18 '14 at 15:08
  • I think this already exists: https://github.com/chengxianga2008/node-cryptojs-aes .. check for CryptoJS AES implementation.. https://code.google.com/p/crypto-js/source/browse/tags/3.1.2/src/aes.js – Jens A. Koch Oct 18 '14 at 15:15