Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / security / encryption

Encrypt and Decrypt password through SQL query

3.00/5 (2 votes)
15 Apr 2013CPOL 94.4K  
How to Encrypt And Decrypt a field without writing any frontent code .

Introduction

If we want to store our data in encrypted form, we use code for encryption and for decryption. But here we can store data in a database without writing lengthy code for it, we can do it in just two lines.

Using the code

First we have to create a database and a table.

SQL
create database Encrypt_Decrypt  ;//create database  
use Encrypt_Decrypt  ;// use database 
CREATE TABLE 'encrypt_decrypt'.'test' ('Username' VARCHAR(45) ,'Pswd' VARCHAR(45) ); //creating  table 
//Encryption start  
//Encrypt Pswd data at the time of inserting  
insert into 'encrypt_decrypt'.'test'(Username,Pswd) values('atul',AES_ENCRYPT('tiwari','.a.'));
//For decrypting Data 
SELECT Username,AES_DECRYPT(Pswd,'.a.') FROM test ;  // Here '.a.' is the Encrypt key

This code is tested in MySQL Server.

Points of Interest

While encrypting data we need a key, this should be unique and confidential because it will be further used in decrypting the data. If you forget the key then you can not decrypt your data.

For security purposes you can also put your key in the config file. 

For more help you can post your query.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)