What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 36142 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > SQL Server Encryption

SQL Server Encryption

6 vote(s)
Rating: 4.17 out of 5
Article posted by Madhu.b.rokkam on 4/29/2011 | Views: 4160 | Category: Sql Server | Level: Intermediate | Points: 250 red flag


This article deals with creating Different keys to encrypt data in SQL using the SQL Encryption.

Download


 Download source code for SQL Server Encryption


Introduction

Obfuscating data by the use of a key or password is called Encryption. Using Encryption we can make the data
useless without the corresponding decryption key or password for the Hackers.

Start creating necessary Keys

So Lets start with creating different all the necessary keys and then we will do a sample example.

First We will start with the Service Master Key

The Service Master Key is the root of the SQL Server encryption hierarchy which is generated automatically the first time which we can regenerate, Backup and restore so at to use the same across servers.


Next we will create a Master Key..

The database master key is a symmetric key used to protect the private keys of certificates and asymmetric keys that are present in the database. We can encrypt this master key using a password. We can backup and restore this too.


Before we create other keys Open The master Key

Next we will create a Certificate with a subject and a start date. Here start date is the date on which the certificate becomes valid.We can backup this to a file encrypting it with a password.


Next we will create a Symmetric key with an algorithm, key source and an identity value this will help us to generate the same symmetric key across servers and this is very important since we will use this symmetric key to encrypt or decrypt column values. This doesn't have any Backup and restore commands.

Different algorithms can be used DES | TRIPLE_DES | TRIPLE_DES_3KEY | RC2 | RC4 | RC4_128 | DESX | AES_128 | AES_192 | AES_256

We will use Triple_DES in our case.

Once we are done with these keys we can view them using the below commands.


Lets start using these with an example.

We will create a table called employee with id, name, age and SSN. SSN being a sensitive info we will encrypt while inserting data and decrypt while retrieving.


Here we have used Varbinary to store the encrypted SSN data and length depends on the algorithm used.

Now that we have created the Table we will insert a record into this table.

To Insert data we need to first open the symmetric key and then use the EncryptByKey function to use the GUID that got created for the Symmetric key. And finally close the symmetric key. We can place the above lines in a stored procedure also.

Next we will see how we can retrieve this encrypted info.


 

Conclusion

Hope you all will like this article.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Madhu Rokkam

Experience:0 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, January 13, 2011
Level:Bronze
Status: [Member] [MVP]
Biography:
 Responses
Posted by: Sashys | Posted on: 07 May 2011 07:08:30 AM | Points: 25

Hi Madhu,
I downloaded all your scripts, i was confused a bit initally[With backup and restore queries] but worked fine at last.. Thanks madhu.. I hope the backup and restore is totally optional, but required for future use right.

It was really good madhu..

Posted by: Lakn2 | Posted on: 30 Jun 2011 05:19:45 AM | Points: 25

good one

>> Write Response - Respond to this post and get points
Related Posts

This is part 40 of the series of article on SSIS. In this article we are going to see on how to use the Character Map transformation control in SSIS Packaging.

Use wildcards characters like %, _ in the where clause of an sql statement. Manage Error "xml parsing <line and character details> illegal name character"

This article describes about how you can join different tables in SQL. It describes different join operations.

Backup and Restore is one of the high availability model. In this article, I would like to perform Backup on Remote server and Performing Restore the backup file(.bak) from Remote Server into local SQL Server.

An SQL Server trace can be used to record events that you are interested in. You can create trace file and trace table workloads easily using SQL Server Profiler.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/22/2013 5:28:04 AM