How to encrypt some sensitive columns in my mysql table?

Hello, i need to encrypt some sensitive columns in my mysql table

in php i use this to encrypt:

function encrypt($key, $string) {
  $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
  $encrypted = openssl_encrypt($string, 'aes-256-cbc', $key, 0, $iv);
  return base64_encode($encrypted . '::' . $iv);
}

and this to decript:

function decrypt($key, $garble) {
    list($encrypted_data, $iv) = explode('::', base64_decode($garble), 2);
    return openssl_decrypt($encrypted_data, 'aes-256-cbc', $key, 0, $iv);
}

So i need to encrypt data in database without PHP but the same way my function does.

Is it possible from phpMyAdmin?

No. since MySQL’s AES_DECRYPT() does not use IVs.

That is bad for me!

Why is that bad?

i need encrypt data same way in php and mysql so this means i need different encrypt mode

The point is that encryption using PHP and MySQL can (and most likely do) differ. Even if they were both named, say MD5, it would be at best a fragile relationship if it ever worked to begin with.

Encryption is usually not a problem as it is not meant to be decrypted. So I question your need to do so. That said, if you want to encrypt with PHP use PHP to decrypt. If you are hoping to use PHP to decrypt what was encrypted in MySQL, phpMyAdmin or otherwise, I’m afraid you’re out of luck.

So what you want to say is that there are no methods to encrypt my data made with PHP and mysql even if i use same key?

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.