-
Notifications
You must be signed in to change notification settings - Fork 96
/
HowToEncryptData.sql
251 lines (191 loc) · 5.51 KB
/
HowToEncryptData.sql
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*=======================================================================================
-----------------------------HOW TO ENCRYPT SENSISTIVE DATA------------------------------
Written By: Eric Rouach, Madeira Data Solutions
Date of Creation: May 2021
This series of scripts is based on the AdventureWorks2014 - [Sales].[CreditCard] Table
The process describes the encryption of the CardNumber column:
We will demonstrate two ways of encrypting data:
*encrypt and make data decryptable
**encrypt and make data undecryptable
=======================================================================================*/
--
USE
AdventureWorks2014;
GO
--Check the [Sales].[CreditCard] table content:
SELECT * FROM [Sales].[CreditCard]
/*
Since it is unsafe to keep the CardNumber as clear-text, let's encrypt it:
We will demonstrate two ways of encrypting data:
*encrypt and make data decryptable
**encrypt and make data undecryptable
For the first case, we need to take the following actions first:
-Create and backup Master Key
-Create and backup Certificate
-Create a Symmetric Key
*/
--1) Create Master key:
CREATE MASTER KEY
ENCRYPTION BY PASSWORD = '$trongPa$$word'; --choose a strong password and keep it in a safe place!
GO
--Check the master key has been created:
SELECT * FROM sys.symmetric_keys
GO
--2) Backup Master Key:
BACKUP MASTER KEY TO FILE = 'C:\EncryptionBackups\AW2014MasterKeyBackup'
ENCRYPTION BY PASSWORD = '$trongPa$$word';
GO
--3) Create certificate
CREATE CERTIFICATE AW2014Certificate
WITH SUBJECT = 'CreditCard_Encryption',
EXPIRY_DATE = '20991231';
GO
--4) Backup certificate
BACKUP CERTIFICATE AW2014Certificate TO FILE = 'C:\EncryptionBackups\AW2014CertificateBackup'
GO
--5) Create symmetric key
CREATE SYMMETRIC KEY AW2014SymKey
WITH ALGORITHM = AES_256
ENCRYPTION BY CERTIFICATE AW2014Certificate;
GO
--==================================
--*encrypt and make data decryptable
--==================================
--Add a varbinary datatype encrypted column
ALTER TABLE [Sales].[CreditCard]
ADD CardNumberEnc VARBINARY(250) NULL
GO
--Check the table
SELECT * FROM [Sales].[CreditCard]
GO
--Open the symmetric key
OPEN SYMMETRIC KEY AW2014SymKey
DECRYPTION BY CERTIFICATE AW2014Certificate;
--Encrypt existing data
UPDATE [Sales].[CreditCard]
SET
CardNumberEnc =
EncryptByKey(Key_GUID('AW2014SymKey'), CardNumber, 1, CONVERT(VARBINARY, CreditCardID))
--Check the table
SELECT * FROM [Sales].[CreditCard]
--Make the new column Non-Nullable
ALTER TABLE [Sales].[CreditCard]
ALTER COLUMN CardNumberEnc VARBINARY(250) NOT NULL
GO
--Check the table
SELECT * FROM [Sales].[CreditCard]
--Drop old column
DROP INDEX [AK_CreditCard_CardNumber] ON [Sales].[CreditCard]
GO
ALTER TABLE [Sales].[CreditCard]
DROP COLUMN CardNumber
EXEC sp_rename
'Sales.CreditCard.CardNumberEnc', 'CardNumber', 'COLUMN';
GO
--Close the symmetric key
CLOSE SYMMETRIC KEY AW2014SymKey;
--Create a NonClustered index on the new column
CREATE NONCLUSTERED INDEX [AK_CreditCard_CardNumber] ON [Sales].[CreditCard]
(
[CardNumber]
)
GO
--Check the table
SELECT
CreditCardID,
CardType,
CardNumber,
ExpMonth,
ExpYear,
ModifiedDate
FROM
[Sales].[CreditCard]
--Select and decrypt the CardNumber
OPEN SYMMETRIC KEY AW2014SymKey
DECRYPTION BY CERTIFICATE AW2014Certificate;
SELECT
CreditCardID,
CardType,
CONVERT(NVARCHAR(25), DecryptByKey(CreditCard.[CardNumber], 1, CONVERT(varbinary, CreditCardID))) AS CardNumber,
ExpMonth,
ExpYear,
ModifiedDate
FROM
[Sales].[CreditCard]
CLOSE SYMMETRIC KEY AW2014SymKey;
GO
--=========================================================
--=====================================
--**encrypt and make data undecryptable
--=====================================
USE
AdventureWorks2014;
GO
--Add a varbinary datatype encrypted column
ALTER TABLE [Sales].[CreditCard]
ADD CardNumberEnc VARBINARY(250) NULL
GO
--Check the table
SELECT * FROM [Sales].[CreditCard]
--Encrypt existing data
UPDATE [Sales].[CreditCard]
SET
CardNumberEnc =
HASHBYTES('SHA2_256', CardNumber) --SHA2_256 is the encryption algorithm
--Encrypt existing data with a salt as an extra security layer:
UPDATE [Sales].[CreditCard]
SET
CardNumberEnc =
HASHBYTES('SHA2_256', CardNumber+CAST([CreditCardID] as NVARCHAR(250)))
--Check the table
SELECT * FROM [Sales].[CreditCard]
--Make the new column Non-Nullable
ALTER TABLE [Sales].[CreditCard]
ALTER COLUMN CardNumberEnc VARBINARY(250) NOT NULL
GO
--Check the table
SELECT * FROM [Sales].[CreditCard]
--Drop old column
DROP INDEX [AK_CreditCard_CardNumber] ON [Sales].[CreditCard]
GO
ALTER TABLE [Sales].[CreditCard]
DROP COLUMN CardNumber
EXEC sp_rename
'Sales.CreditCard.CardNumberEnc', 'CardNumber', 'COLUMN';
GO
--Create a NonClustered index on the new column
CREATE NONCLUSTERED INDEX [AK_CreditCard_CardNumber] ON [Sales].[CreditCard]
(
[CardNumber]
)
GO
--Check the table
SELECT
CreditCardID,
CardType,
CardNumber,
ExpMonth,
ExpYear,
ModifiedDate
FROM
[Sales].[CreditCard]
GO
--============CleanUp============
DROP SYMMETRIC KEY AW2014SymKey
GO
DROP CERTIFICATE AW2014Certificate
GO
DROP MASTER KEY
GO
USE master;
GO
DROP DATABASE [AdventureWorks2014]
GO
RESTORE DATABASE AdventureWorks2014
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\AdventureWorks2014.bak'
WITH
MOVE
'AdventureWorks2014_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\AdventureWorks2014.mdf',
MOVE
'AdventureWorks2014_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\AdventureWorks2014.ldf'
GO