In (not too far) the past decision was made (by someone who already works here) to always "encrypt" the database identifiers on something else, on the fly, when it was necessary for external communication.
Now we have moved from PHP 5.x to PHP 7.0 for our main application, and our microservices scattered throughout our infrastructure work with either 7.0 or 7.1. 7.1 servers keep obsolete warnings for mcrypt files. There hasn’t been anything yet. But with PHP 7.2 around the corner, we want to keep updating and updating. Mcrypt is blocking.
Keeping all encrypted values in 60 tables, in 1,400 databases, is a huge task. Is there a way to use OpenSSL, with Blowfish and ECB, to get the same encoded and decoded values to lull us with a false sense of security? Everything so that we can plan the database migration far ahead.
Basically, the currently encrypted value is:
item:13fb7533bf19399ff114468b194ebfaf
This is an identifier 123. The following functions are used to access this line:
$id = 123;
$type = 'item';
$serialized = serialize('' . $id); // To make sure always a string gets put in
$ivSize = mcrypt_create_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB), MCRYPT_RAND);
$iv = mcrypt_create_iv($ivSize);
$passCrypt = mcrypt_encrypt(MCRYPT_BLOWFISH, $type, $serialized, MCRYPT_MODE_ECB, $iv);
$encoded = bin2hex($passCrypt); // `13fb7533bf19399ff114468b194ebfaf`
$encryptedId = $type . ':' . $encoded;
This gives the final result item:13fb7533bf19399ff114468b194ebfaf.
Now, vice versa:
$encryptedId = 'item:13fb7533bf19399ff114468b194ebfaf';
$type = 'item';
$encryptedIdOnly = substr($encryptedId, strlen($type) + 1); // `13fb...`
$decoded = hex2bin($encryptedIdOnly);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB), MCRYPT_RAND);
$decrypted = mcrypt_decrypt(MCRYPT_BLOWFISH, 'item', $decoded, MCRYPT_MODE_ECB, $iv); // This gives ' `s:3:"123";` '
$unserialized = unserialize($decrypted); // '123'
I tried it for several hours, but I am completely stunned by something crypto (but I want to learn!). My current code is:
$cipher = 'BF-ECB';
$isCtypeXDigit = ctype_xdigit($decipher);
$decoded = hex2bin($decipher);
$ivLength = openssl_cipher_iv_length($cipher);
$randomBytes = openssl_random_pseudo_bytes($ivLength);
$decrypted = openssl_decrypt($decoded, $cipher, $prefix, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $randomBytes);
$unserialized = unserialize($decrypted);
Which gives me a thousand things, all similar to IY_Lc d: _ . Can anyone shed light on this - is this possible?