aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Encrypt and Decrypt String (Password) in SQL Server 2008

Apr 20, 2015

Here I will explain how to do encryption and decryption of string or text or password in
sql server using encryptbypassphrase and decryptbypassphrase functions in sql server. EncryptByPassPhrase function in sql server will encrypt the data and store it in varbinary format and DecryptByPassphrase function will decrypt varbinary encrypted string and show the result in decrypted format.


This function will use DES algorithm to encrypt the data and store it in varbinary format.

Syntax of EncryptByPassPharse declaration


If you observe above syntax EncryptByPassPhrase has two mandatory arguments: PASSPHRASE (specifies the data string that is used to derive an encryption key) and text (specifies text to be encrypted).


DecryptByPassphrase is used to decrypt the encrypted column.

Syntax of DecryptByPassphrase declaration

DecryptByPassphrase ('PASSPHRASE','text')

If you observe above syntax DecryptByPassphrase has two mandatory arguments: PASSPHRASE (this string is used to derive decryption key this string should be same as encrptbypasspharse 'PASSPHRASE' string) and text (specifies text to be decrypted).

To encrypt and decrypt string with example we need to write the query like as shown below

-- creating temp table and inserting encrypted password using EncryptByPassPhrase
declare @userdetails table(userid int, username varchar(50),password varbinary(100))
insert into @userdetails(userid,username,password) values(1,'suresh',EncryptByPassPhrase('aspdotnetsuresh','dasari'))
insert into @userdetails(userid,username,password) values(2,'rohini',EncryptByPassPhrase('aspdotnetsuresh','alavala'))
insert into @userdetails(userid,username,password) values(3,'madhavsai',EncryptByPassPhrase('aspdotnetsuresh','yemineni'))

select * from @userdetails

-- Converting and Decrypting varbinary column password using DECRYPTBYPASSPHRASE
SELECT userid,username,
CONVERT(varchar(50),DecryptByPassphrase ('aspdotnetsuresh',password))as DecryptedPassword
FROM @userdetails

If you observe above query in EncryptByPassPhrase and DecryptByPassphrase I am using same string name 'aspdotnetsuresh' to generate key for encryption and decryption otherwise it will not convert the string correctly. Now execute this query and check results that will be like as shown below


Encrypt and Decrypt String (Password) in SQL Server 2008

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Unknown said...

code is not working.please upload proper code if you have then upload otherwise not..

Unknown said...

Nice, Thank you Suresh.

Unknown said...

nice. but we

Anonymous said...

Thank you for your valuable solution.

sdsff said...


Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.