当前位置:网站首页>Sqlserver encrypts and decrypts data

Sqlserver encrypts and decrypts data

2022-07-04 22:13:00 CodingSir

– sqlserver Encrypt data 、 Decrypt

– Create test table

CREATE TABLE TBLUser 
(    
    Name             nvarchar(30),     
    Password         varbinary(1000)
)
  • 1
  • 2
  • 3
  • 4
  • 5

– Create database master **

CREATE master key  ENCRYPTION BY PASSWORD ='[email protected]'
  • 1

– Create certificate

CREATE CERTIFICATE TestCert with SUBJECT = 'Test Certificate'
  • 1

– Create symmetry **

CREATE SYMMETRIC KEY TestSymmetric WITH ALGORITHM = AES_256 ENCRYPTION BY CERTIFICATE TestCert 
  • 1

– Use symmetric secret keys to encrypt data

OPEN SYMMETRIC KEY TestSymmetric DECRYPTION BY CERTIFICATE TestCert;
INSERT INTO TBLUser values(' Zhang San ', ENCRYPTBYKEY(Key_Guid(N'TestSymmetric'), '123456'));
CLOSE SYMMETRIC KEY TestSymmetric;
  • 1
  • 2
  • 3

– View encrypted data

SELECT * FROM TBLUser
  • 1

– Decrypt data

OPEN SYMMETRIC KEY TestSymmetric DECRYPTION BY CERTIFICATE TestCert;
SELECT Name, CAST(DecryptByKey(password) as varchar(100)) Password FROM TBLUser;
CLOSE SYMMETRIC KEY TestSymmetric;
原网站

版权声明
本文为[CodingSir]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042137273098.html