Monday, September 27, 2010

Encrypt And Decrypt String in SQL

In Sql Server Provided some function to Encrypt and Decrypt the Strings Ex :

For testing purpose i have created one table called login_details you need to be set the password column is varbinay because the password string stored in binary format and length should be min 60

First Create the table :-

create table login_details(uid integer,username varchar(10),password varbinary(100))

Insert Some records on the EncryptByPassPhrase function first parameter used to generate the key for encryption/decryption and second parameter show the password string---

insert into login_details(uid,username,password) values(1,'murli',EncryptByPassPhrase('12','XXX'))

insert into login_details(uid,username,password) values(2,'deepak',EncryptByPassPhrase('12','YYY'))

insert into login_details(uid,username,password) values(3,'sanjay',EncryptByPassPhrase('12','ZZZ'))


For Decryption we need to write code same here:-

select uid,username, DECRYPTBYPASSPHRASE ('12',password) as Password from login_details

select uid,username,convert(varchar(10), DECRYPTBYPASSPHRASE ('12',password)) from login_details

For More Details

No comments:

Post a Comment