3

In mySql I do, hex(AES_ENCRYPT('mytext','mykeystring')). This gives me a string with characters, rather than some unreadable things.

How can I do something like this in php? Is there a built in function that'll let me do this? Just like mySql has hex and aes_encrypt with a password/salt.

I'm not looking for exact aes encryption in php. Anything that returns a string of alphabets and numbers and that isnt easy to crack will do (has salt)

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Norman
  • 6,159
  • 23
  • 88
  • 141
  • Try googling for an AES library implemented for PHP: https://www.google.com.au/search?q=php+aes However, you'll have to study MySQL's AES_ENCRYPT to learn 1) what cipher block chaining mode it is using 2) how it converts passwords into keys – Patashu Jun 14 '13 at 05:36

2 Answers2

5

The PHP equivalent is:

// MySQL's AES_ENCRYPT uses Rijndael 128 with ECB mode
$enc_text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_ECB);
// HEX() equivalent
echo bin2hex($enc_text);

Note that ECB block mode ain't great, and it's better to use CFB. My previous answer discusses this in more detail.

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • In your old post, there's a guy who's written twice `You should not use ECB either, for that matter`. If this should not be used, then what should be? – Norman Jun 14 '13 at 12:57
  • @Norman That was a comment on [this answer](http://stackoverflow.com/a/10916445/1338292) and he's right. Instead of ECB you should use CFB, which is also mentioned in this answer ;-) – Ja͢ck Jun 14 '13 at 13:08
0

Use the hash function http://php.net/manual/en/function.hash.php). The hash_algos() function returns an array of available algorithms. (http://www.php.net/manual/en/function.hash-algos.php)

Tom Regner
  • 6,856
  • 4
  • 32
  • 47