65.9K
CodeProject is changing. Read more.
Home

Encrypt and Decrypt password through SQL query

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (2 votes)

Apr 15, 2013

CPOL
viewsIcon

94364

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.

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.