-3

Can you please give me a full example of how to create an encryption and decryption in PHP language? I use hexa for the data and the key. I search through google and find that there is one website that match my expectation which is here.

Take this for example:

Data: 225551100012FFFF

Key: DC1C1F2B180F85D8D522A75D2354ED149A5B81F198387B51

When I decrypt, I got 389da227862957c4

Thank you in advance!

Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
  • 1
    What have you tried so far? You're expected to make some effort towards solving your own problem before asking here as Stack Overflow is not a free coding service. – Royal Wares Dec 06 '18 at 09:17
  • Have try many solution that others told me like from this link below: https://stackoverflow.com/questions/22592919/triple-des-encryption-decryption-using-php and now i currently code in Go to see if it can be solve by that language. – Sintya Oktaviani Dec 07 '18 at 03:37

1 Answers1

0

Have found my answer from this website http://www.isapp.it/en/menu-en/31-tips-a-tricks/php/118-php-how-to-encrypt-text-in-triple-des-ecb.html

But because i want to encrypt and decrypt it using hexa, i modify the code a bit to this

function cryptECB($crypt, $key) {
    //Omit hex2bin and bin2hex if plain text is used
    $crypt = hex2bin($crypt);
    $key = hex2bin($key);

    $iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

    $cryptText = mcrypt_encrypt(MCRYPT_3DES, $key, $crypt, MCRYPT_MODE_ECB, $iv);

    return bin2hex($cryptText);
}

function decryptECB($encrypted, $key) {
    //Omit hex2bin and bin2hex if plain text is used
    $encrypted = hex2bin($encrypted);
    $key = hex2bin($key);
    $iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

    $stringText = mcrypt_decrypt(MCRYPT_3DES, $key, $encrypted, MCRYPT_MODE_ECB, $iv);

    return bin2hex($stringText);
}
  • ECB mode is insecure, 3DES is an old cipher, the mcrypt library is not maintained and based on bad practices. There is no such thing as copy / paste security; you will have to learn about cryptography first. – Maarten Bodewes Dec 11 '18 at 11:54