Saturday, October 1, 2011

Decrypt and Encrypt in MySQL example

If you need to put encrypted data (eg.credit cards) into MySQL database. Below are examples on how to encrypt and decrypt your data. We don't use the password function because once encrypted it cannot be decrypted.

Make sure the data field accepts special charaters like char() not varchar()

Encrypt:
INSERT INTO tableName (fieldName)
VALUES (AES_ENCRYPT('dataGoesHere','ThePasswordGoesHere'));

Decrypt:
SELECT aes_decrypt(fieldName,'ThePasswordGoesHere') 
FROM tableName;

No comments:

Post a Comment