-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn_encypt.sql
More file actions
45 lines (37 loc) · 1.01 KB
/
column_encypt.sql
File metadata and controls
45 lines (37 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- create master key
USE [DatabaseName];
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '[Pwd]';
SELECT name KeyName,
symmetric_key_id KeyID,
key_length KeyLength,
algorithm_desc KeyAlgorithm
FROM sys.symmetric_keys;
-- create certificate
USE [DatabaseName];
GO
CREATE CERTIFICATE [CertificateName] WITH SUBJECT = 'Protect my data';
GO
SELECT name CertName,
certificate_id CertID,
pvt_key_encryption_type_desc EncryptType,
issuer_name Issuer
FROM sys.certificates;
-- create symmertic key
CREATE SYMMETRIC KEY [KeyName] WITH ALGORITHM = AES_256 ENCRYPTION BY CERTIFICATE [CertificateName];
SELECT name KeyName,
symmetric_key_id KeyID,
key_length KeyLength,
algorithm_desc KeyAlgorithm
FROM sys.symmetric_keys;
-- exec
-- open key
OPEN SYMMETRIC KEY [KeyName]
DECRYPTION BY CERTIFICATE [CertificateName];
UPDATE [Database].dbo.[Table]
SET [EncryptColumn] = EncryptByKey (Key_GUID('[KeyName]'), [Column])
FROM [Database].dbo.[Table];
GO
-- close key
CLOSE SYMMETRIC KEY [KeyName];
GO