Click here to Skip to main content
15,881,635 members
Articles / Security / Encryption
Tip/Trick

Encrypt and Decrypt password through SQL query

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
15 Apr 2013CPOL 92.7K   8   3
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIt does not work for MS SQL Pin
Member 1022937123-Jun-14 23:21
Member 1022937123-Jun-14 23:21 
AnswerRe: It does not work for MS SQL Pin
Member 1247041319-Apr-16 2:17
Member 1247041319-Apr-16 2:17 
QuestionPassword example? Pin
Bernhard Hiller16-Apr-13 0:20
Bernhard Hiller16-Apr-13 0:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.