首页 SQLServer 数据库加密

数据库加密

作者:胡同里的砖头 围观群众:221 更新于:2024-04-29

1,创建测试表

CREATE TABLE TBLUser 
(
Name nvarchar(30),
Password varbinary(1000)
)

2, 创建数据库主密码
CREATE master key  ENCRYPTION BY PASSWORD ='passW@ord'

3.创建证书
CREATE CERTIFICATE TestCert with SUBJECT = 'Test Certificate'

4.创建对称**
CREATE SYMMETRIC KEY TestSymmetric WITH ALGORITHM = AES_256 ENCRYPTION BY CERTIFICATE TestCert 

5.使用对称秘钥加密数据
OPEN SYMMETRIC KEY TestSymmetric DECRYPTION BY CERTIFICATE TestCert;
INSERT INTO TBLUser values('张三', ENCRYPTBYKEY(Key_Guid(N'TestSymmetric'), '123456'));
CLOSE SYMMETRIC KEY TestSymmetric;

6.查看加密后的数据
SELECT * FROM TBLUser
7.解密数据
OPEN SYMMETRIC KEY TestSymmetric DECRYPTION BY CERTIFICATE TestCert;
SELECT Name, CAST(DecryptByKey(password) as varchar(100)) Password FROM TBLUser;
CLOSE SYMMETRIC KEY TestSymmetric;

  • 本文标题: 数据库加密
  • 文章分类:【SQLServer】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8