Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 April SQL Server: Encrypt Column data using Symmetric Key Encryption

SQL Server: Encrypt Column data using Symmetric Key Encryption

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

The Database Security is a very common and one of the major aspects for DBA to keep data secure.
If you don’t know about SQL Server TDE, please read below article which uses for data encryption in SQL Server.

SQL Server: Transparent Data Encryption (TDE) to Encrypt a Database

There are different ways to encrypt your data like TDE, data masking, symmetric key.

In this post, I am sharing a demonstration on how to encrypt your table column using Symmetric key encryption.
For example, When you are storing customer’s credit card information, you must encrypt all cards related columns.

Below is a full demonstration of this:

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE dbo.tbl_CustomerCards
(
CustID INT PRIMARY KEY
,CustName VARCHAR(10)
,CustCardNo CHAR(8)
)
GO
INSERT INTO dbo.tbl_CustomerCards VALUES
(1,'Anvesh','25-85-96'),(2,'Neevan','88-26-19')
,(3,'Roy','29-30-32'),(4,'Muskan','36-22-69')
,(5,'Martin','26-88-66'),(6,'Jenny','74-21-31')
GO

Create your master key and give password base on windows policy:

1
2
3
4
5
6
7
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Dbrnd888';
GO
 
 
--If your password does not match windows policy, you will get a below error.
Msg 15118, Level 16, State 1, Line 1
Password validation failed. The password does not meet Windows policy requirements because it is not complex enough.

Create a database certificate which is protected by database master key:

1
2
3
CREATE CERTIFICATE dbrnd_certificate
WITH SUBJECT = 'dbrnd Data';
GO

Create a symmetric key which uses for encryption/decryption:
I used AES_256 algorithm.

1
2
3
4
CREATE SYMMETRIC KEY dbrnd_Symmetric_key
WITH ALGORITHM = AES_256
ENCRYPTION BY CERTIFICATE dbrnd_certificate;
GO

Add one more column in a table:

1
2
3
ALTER TABLE tbl_CustomerCards
ADD CustCardNo_encrypt VARBINARY(MAX)
GO

Open a symmetric key for further use:

1
2
3
OPEN SYMMETRIC KEY dbrnd_Symmetric_key
DECRYPTION BY CERTIFICATE dbrnd_certificate;
GO

Update newly created column by generating encrypted data with use of old column:

1
2
3
4
UPDATE tbl_CustomerCards
SET CustCardNo_encrypt = EncryptByKey (Key_GUID('dbrnd_Symmetric_key'),CustCardNo)
FROM tbl_CustomerCards
GO

Now, You can remove your old column:

1
2
3
ALTER TABLE tbl_CustomerCards
DROP COLUMN CustCardNo;
GO

SELECT encrypted/decrypted card numbers:

1
2
3
SELECT CustID, CustCardNo_encrypt AS 'Encrypted_CardNumber',
CONVERT(varchar, DecryptByKey(CustCardNo_encrypt)) AS 'Decrypted_CardNumber'
FROM dbo.tbl_CustomerCards;

Try to Insert few records with encrypted card number values:

1
2
3
4
INSERT INTO dbo.tbl_CustomerCards VALUES
(7,'Meera',EncryptByKey( Key_GUID('dbrnd_Symmetric_key'), CONVERT(varchar,'66-52-36')))
,(8,'Laria',EncryptByKey( Key_GUID('dbrnd_Symmetric_key'), CONVERT(varchar,'99-36-23')))
GO

Once you complete with the use symmetric key, CLOSE it:

1
2
CLOSE SYMMETRIC KEY dbrnd_Symmetric_key;
GO

The result:

SQL Server Column Encrypted Result

Apr 18, 2017Anvesh Patel
PostgreSQL 9.5: Multiple columns or keys in ON CONFLICT clauseSQL Server 2016: Introduced AT TIME ZONE Expression to select different TIME ZONEs

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

April 18, 2017 SQL ServerAES, Anvesh Patel, Column encryption, Data Security, database, database research and development, dbrnd, Decryption, encryption, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, SYMMETRIC KEY, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....