0

What is the best encryption / decryption class / functions that can encrypt an array of data or objects and return it as a hash serialized string?

Then, on decryption, the serialized strings can be decrypted back to its original form of values containing objects or array values.

Thanks

2 Answers 2

3

Preface: You seem to have a notion that hashing some data would be the same thing as encrypting it. Hashing is NOT encryption and cannot be reversed with a password or keyfile, like encryption can.

PHP comes with several hashing protocols like md5 (md5_file), SHA1 (SHA1_file). It all really depends on what you're doing with this hash and what you're hashing in the first place.

Sign up to request clarification or add additional context in comments.

2 Comments

+1. MD5 is bad if you're worried about users deliberately causing collisions, as the algorithm is vulnerable. SHA1 is good for speed. Bcrypt is good if you want to slow things down. All sorts of others.
okay i revised my question, my purpose is not to permanently encrypt the data as non reversible string. Instead, i like to see how we can decrypt an object or arrays.
2

The mcrypt library has a lot of functions to do encryption in as many ways as you can dream. Here's an example using AES:

$secretKey = 'the longer, the better';
$originalString = 'some text here';

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secretKey, $originalString,
                              MCRYPT_MODE_CBC, $iv);
printf( "Original string: %s\n", $originalString );
// Returns "Original string: some text here"

printf( "Encrypted string: %s\n", $crypttext );
// Returns "Encrypted string: <gibberish>"

$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $secretKey, $crypttext,
                                MCRYPT_MODE_CBC, $iv);

// Drop nulls from end of string
$decrypttext = rtrim($decrypttext, "\0");

printf( "Decrypted string: %s\n", $decrypttext );
// Returns "Decrypted string: some text here"

1 Comment

I would encourage you to consider not using MCRYPT_RIJNDAEL_256 if you are planning to be easily interoperable with other system. Most implementations of Rijandael, including AES, use 128-bit block sizes. Working with 256bit block sizes can be a PITA for other systems to talk to PHP services. You can still use 256-bit keys with 128-bit Rijandael blocks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.