Reputation: 608
Could somebody advice me on subject "Simple encrypt/decrypt of utf-8 strings in php5 with secret key" ? Some advice on independant libraries? (no dependancy on 32/64bit machines).
$encryptedTextUTF8 = simpleEncrypt($originalTextUTF8, $secret);
echo simpleDecrypt($encryptedTextUTF8, $secret); // outputs $originalTextUTF8
Upvotes: 1
Views: 6300
Reputation: 2050
Using the mcrypt extension: http://us.php.net/manual/en/book.mcrypt.php Something like the following could be feasible:
$pass = 'password_string_to_be_encrypted';
$string = 'password_string_to_be_decrypted';
$salt = 'your_salt';
$pepper = 'your_extra_salt';
$encrypted = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $salt . $pass . $pepper ), $string, MCRYPT_MODE_CBC, md5( md5( $salt . $pass . $pepper ))));
$decrypted = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5($salt . $pass . $pepper), base64_decode( $string ), MCRYPT_MODE_CBC, md5( md5( $salt . $pass . $pepper))), "\0");
edit: Proven wrong, I suggest to remove the md5 part of the encryption. As to what is better, I do not know yet, but I'll try to come update my post when I've had sufficient time investigating.. sorry! :)
Upvotes: 2
Reputation: 51411
PHP strings don't know (or care) what their character encoding is. They're just a collection of bytes. This allows you to entirely not worry about UTF-8 here.
Because you're asking for symmetric key encryption, you'll probably want to use the mcrypt extension. It has a wide variety of cyphers available, including AES ("Rijndael"). Mcrypt itself doesn't care whether or not it's on a 32-bit or 64-bit machine.
I'm not going to provide example code, because using encryption requires some non-trivial knowledge that I'm not sure I can properly explain to you in code alone, and copy-paste of encryption code is a bad, bad idea.
Understanding how block cypher modes of operation work will help you pick the proper mode for what you're doing. This understanding will also be necessary if you wish to have programs developed in other languages work with your encrypted data, as they'll use different default cipher modes and different padding schemes.
Upvotes: 1