It's the first time I'm using encryption in MariaDB, so I need to assure that I'm getting it right. I need to simply store some identifiers in an encrypted way, and am wondering if I'm doing it correctly. As I'm getting some unexpected behaviours upon data retrieval (sometimes only), I'm suspecting there's something wrong (and I've been checking with the docs etc, it all seems right..?).
- I'm storing the data in
BLOB
andNOT NULL
columns, nothing else specified. - I'm inserting the data using this, whereas
key
is generated viaopenssl rand -base64 32
INSERT INTO data_table ( encr_data ) VALUES( AES_ENCRYPT( "secret_string", "key" ) );
- I'm retrieving the data using this:
SELECT CAST( AES_DECRYPT( encr_data, "key" ) AS CHAR ) as encr_data FROM data_table;
Is that the proper way of doing it in MariaDB? The secret string consists of about 35 - 40 characters.
It's really fundamental for me as the application I'm coding is storing some secret data to be used with another API. In other words, the encryption should not bring any risks concerning the data integrity with itself. Just the thought of needing to regenerate all of the encrypted data due to whatever encryption error (making the encrypted + stored data unuseable)...
So I need to assure that no data is cropped of the encrypted string and that the encryption is done properly, hence the reason for this question.
1 Answer 1
AES_ENCRYPT
returns binary data (BLOB
or BINARY(...)
)
CASTing
a BLOB to a CHAR makes a mess. Do not do that.
You can convert it to hex via HEX(AES_ENCRYPT(...))
. That can be put into a VARCHAR
or TEXT
.
The secret string consists of about 35 - 40 characters.
If that is 40 emoji, that will take a lot of bytes. I suggest declaring the column one of this ways:
VARCHAR(200) COLLATE ascii_bin -- if storing the hex
BINARY(100) -- if storing the binary version
If the use is for password validation, AES is the wrong approach. Instead, use a one-way hash (MD5, SHA%, etc).
NULL vs NOT NULL -- That is up to the business logic. If you need "not set yet" (etc), then NULL
is reasonable.
-
Thx a lot but some questions. You say that
aes_encrypt()
returns a hex, but the [docs] (mariadb.com/kb/en/aes_encrypt) say it returns a binary string?? That's actually also the reason why I felt the need of using blob columns. Otherwise I'd need to encrypt-cast-store and cast-decrypt-select, which seems to be more complicated to me, no?DevelJoe– DevelJoe2021年11月27日 23:44:59 +00:00Commented Nov 27, 2021 at 23:44 -
Then, all I need to do in this case is use the secret string as an identifier which is used to call an external API, and that identifier has like 40 characters. What would u do in this case? Thx again, and sry for the follow-up precisions, but the approach I'll use has to be rock solid, to avoid any trouble with these API call identifiers in the future. So no, hashes would be inappropriate in this context, as I need to reuse the raw unencrypted data :))DevelJoe– DevelJoe2021年11月27日 23:47:41 +00:00Commented Nov 27, 2021 at 23:47
-
Oh and btw, does
aes_encrypt()
actually need akey
of a fix length? Like I can't use a random length encryption key??DevelJoe– DevelJoe2021年11月27日 23:50:16 +00:00Commented Nov 27, 2021 at 23:50 -
Oops,
mysql
returned0x...'
; I mistook that as hex. I'll fix my answer.Rick James– Rick James2021年11月28日 03:46:34 +00:00Commented Nov 28, 2021 at 3:46 -
@DevelJoe - Fixed.Rick James– Rick James2021年11月28日 06:41:29 +00:00Commented Nov 28, 2021 at 6:41