Encrypt and Decrypt Column Value In SQL Server Table

Below is my SQL Server Table in design mode.


Image 1.

Select Records


Image 2.

Now we will insert some records in this table. Here you will notice in Password column I will insert value in Encrypted form.

  1. INSERT INTO EMPLOYEE (Name,Email,[Password],Designation,City,[State],Country, Experience)  
  2. VALUES('Rahul Saxena''[email protected]', EncryptByPassPhrase('RS','india'),'Developer','Noida','Uttar Pradesh','India',5)  
  1. INSERT INTO EMPLOYEE (Name,Email,[Password],Designation,City,[State],Country,Experience)  
  2. VALUES('Shambhu Sharma''[email protected]', EncryptByPassPhrase('RS','Delhi'),'Manager','Delhi','Delhi','India',10)  
  1. INSERT INTO EMPLOYEE (Name,Email,[Password],Designation,City,[State],Country, Experience)  
  2. VALUES('Manu Khanna''[email protected]', EncryptByPassPhrase('RS','Rohini'),'Consultant','Delhi','Delhi','India',12)  
  1. INSERT INTO EMPLOYEE (Name,Email,[Password],Designation,City,[State],Country,Experience)  
  2. VALUES('Abhishek Nigam''[email protected]', EncryptByPassPhrase('RS','Lucknow'),'Technical Consultant','Hawaii','Honolulu','USA',9)  
  1. INSERT INTO EMPLOYEE (Name,Email,[Password],Designation,City,[State],Country, Experience)  
  2. VALUES('Shraddha Gaur''[email protected]', EncryptByPassPhrase('RS','DELHI'),'Lead Tester','Noida','Uttar Pradesh','India',4)  
  1. INSERT INTO EMPLOYEE (Name,Email,[Password],Designation,City,[State],Country, Experience)  
  2. VALUES('Shweta Kashyap''[email protected]', EncryptByPassPhrase('RS','Dehradun'),'Developer','Dehradun','Uttarakhand','India',5)  
  1. INSERT INTO EMPLOYEE (Name,Email,[Password],Designation,City,[State],Country, Experience)  
  2. VALUES('Yogesh Gupta''[email protected]', EncryptByPassPhrase('RS','Khanpur'),'Consultant','Atlanta','Georgia','USA',10)  
  1. INSERT INTO EMPLOYEE (Name,Email,[Password],Designation,City,[State],Country, Experience)  
  2. VALUES('Rakesh Dixit''[email protected]', EncryptByPassPhrase('RS','Jaipur'),'Consultant','Jaipur','Rajasthan','India',10)  

Image 3.

Now see your records in your table.


Image 4.

Now How we can Decrypt Our Column Value while fetching.
  1. SELECT EMP_ID,NAME,EMAIL,CONVERT(VARCHAR(50),DECRYPTBYPASSPHRASE ('RS',PASSWORD))AS DECRYPTEDPASSWORD,  
  2. DESIGNATION,CITY,STATE,COUNTRY,EXPERIENCE FROM EMPLOYEE  

Image 5.
Ebook Download
View all
Learn
View all