SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to encrypt passwords with asymmetric key?

Execute the following Microsoft SQL Server T-SQL scripts in SSMS Query Editor to create a user login table and populate it with 2 logins and EncryptByAsymKey encrypted passwords. Use DecryptByAsymKey to verify passwords for the logins.

USE [AdventureWorks]

GO

 

-- DROP TABLE [dbo].[UserLogin]

CREATE TABLE [dbo].[UserLogin](

[UserLoginID] [int] IDENTITY(1,1) PRIMARY KEY,

[LoginName] [char](30) NOT NULL,

[PassWord] varbinary(max) NOT NULL,

[IsActive] [bit] NOT NULL

      CONSTRAINT [DF_UserLogin_IsActive] DEFAULT ((1)),

[CreateDate] [smalldatetime] NOT NULL

      CONSTRAINT [DF_UserLogin_CreateDate] DEFAULT (getdate()),

[ModifyDate] [smalldatetime] NOT NULL

      CONSTRAINT [DF_UserLogin_ModifyDate] DEFAULT (getdate()),

[ModifiedBy] [char](6) NOT NULL

      CONSTRAINT [DF_UserLogin_ModifiedBy] DEFAULT ('system')

 

) ON [PRIMARY]

 

GO

 

-- drop ASYMMETRIC KEY Asym_PassWord

CREATE ASYMMETRIC KEY Asym_PassWord

      WITH ALGORITHM = RSA_512

      ENCRYPTION BY PASSWORD = N'secreT007!'

 

 

DECLARE @CipherString varbinary(max);

SELECT @CipherString = EncryptByAsymKey(AsymKey_ID('Asym_PassWord'),

 N'SecretPass!01');

 

INSERT INTO UserLogin

(LoginName, PassWord)

VALUES ('administrator', @CipherString);

GO

 

DECLARE @CipherString varbinary(max);

SELECT @CipherString = EncryptByAsymKey(AsymKey_ID('Asym_PassWord'),

N'OperPass99$');

 

INSERT INTO UserLogin

(LoginName, PassWord)

VALUES ('operator', @CipherString);

GO

SELECT * FROM UserLogin

GO

 

-- Following query can be used to test an entered password:

SELECT  LoginName,

PassWordDecrypted= convert(nvarchar(128),

DecryptByAsymKey(AsymKey_ID('Asym_PassWord'),

[PassWord], N'secreT007!' ))

FROM UserLogin

WHERE LoginName = 'operator'

go

 

SELECT  LoginName,

PassWordDecrypted= convert(nvarchar(128),

DecryptByAsymKey(AsymKey_ID('Asym_PassWord'),

[PassWord], N'secreT007!' ))

FROM UserLogin

WHERE LoginName = 'administrator'

go

 

Related article:

Encrypt Password Field in SQL Server, Registry Information & Query String

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.