Thursday, February 21, 2008

Storing the password in secure form and reading it back

  • Create the table which have fields UserName and Passwords and set Password field Varbinary

CREATE TABLE [dbo].[User] ([ID] [int] IDENTITY (1, 1) NOT NULL ,[UserName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,[Password] [varbinary] (255) NULL ,) ON [PRIMARY]GO

  • Now store UserName and Password by using new method pwdencrypt().

INSERT INTO UserEncrycptTable(UserName,[Password]) VALUES ( 'Firoz',pwdencrypt('PasswordFiroz'));

  • When we execute the above query and open the table you will notice that password is not in plain text.
  • Now we will retrive this password usin method using the method pwdencrypt().

DECLARE @varPassword varbinary(255)SELECT @varPassword = [Password] FROM UserEncrycptTable where UserName = 'Firoz'

DECLARE @chkPassword varchar(255)

SELECT @chkPassword = 'PasswordFiroz'

PRINT pwdcompare(@chkPassword, @varPassword, 0);

No comments: