试试这个:
CREATE Procedure [dbo].[EncryptPasswd]
@user_id int,
@username varchar(255),
@passwd varchar(255)
As
Begin
Declare @res varbinary(2000)
IF NOT EXISTS(select * from sys.symmetric_keys where name='##MS_DatabaseMasterKey##')
CREATE MASTER KEY ENCRYPTION
BY PASSWORD = 'xyz@123'
IF NOT EXISTS(select * from sys.certificates where name='EncryptTestCert')
CREATE CERTIFICATE EncryptTestCert
WITH SUBJECT = 'xyz@123'
IF NOT EXISTS(select * from sys.symmetric_keys where name='TestTableKey')
CREATE SYMMETRIC KEY TestTableKey
WITH ALGORITHM = TRIPLE_DES ENCRYPTION
BY CERTIFICATE EncryptTestCert
OPEN SYMMETRIC KEY TestTableKey DECRYPTION
BY CERTIFICATE EncryptTestCert
INSERT INTO Users(user_id,username,passwd)
SELECT @user_id,@username,ENCRYPTBYKEY(KEY_GUID('TestTableKey'),@passwd)
END
每当您将新用户插入用户表时使用此存储过程。因此这将加密密码并插入。