Click here to Skip to main content
13,297,048 members (58,301 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 15 Apr 2013

Encrypt and Decrypt password through SQL query

, 15 Apr 2013
Rate this:
Please Sign up or sign in to vote.
How to Encrypt And Decrypt a field without writing any frontent code .


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.


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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionIt does not work for MS SQL Pin
Member 1022937124-Jun-14 0:21
memberMember 1022937124-Jun-14 0:21 
AnswerRe: It does not work for MS SQL Pin
Member 1247041319-Apr-16 3:17
memberMember 1247041319-Apr-16 3:17 
QuestionPassword example? Pin
Bernhard Hiller16-Apr-13 1:20
memberBernhard Hiller16-Apr-13 1: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 15 Apr 2013
Article Copyright 2013 by atul123321
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid