Karim's profileMicrosoft Dynamics CRM B...PhotosBlogListsMore ![]() | Help |
|
April 23 Types of Encryption keys in SQL Server 2005 ? Part(II)In part I , we've talked about the Symmetric key; In this article , I'll be talking about Asymmetric key also known as Public-key cryptography, is a system in which the sender and the receiver of a message have a pair of cryptographic keys - a public key and a private key - to encrypt and decrypt the message. In which the sender can only encrypt the message and only the receiver who has the other pair of the key (private key) can decrypt the message and can’t encrypt it.
We can use three encryption algorithms with Asymmetric keys which are: RSA_512, RSA_1024, RSA_2048, for more information about those algorithms.
How to protect your data using Asymmetric key?
First of all we need to create an Asymmetric key, using the following code to create a Asymmetric key:
IF NOT EXISTS (SELECT * FROM sys.asymmetric_keys WHERE name = with algorithm = RSA_2048 encryption by password='P@ssw0rd' END
Then we create the table that we need to encrypt its data by using the following code or any other code you want to:
Id int identity(1,1) primary key, Data VARBINARY(2048))
You need to declare your targeted field for encryption as varbinary data type to accept encryption.
Then we need to insert an encrypted value in our table, to do so we will need to use the EncryptByAsymKey Method which takes the key Guid which is the Guid of the Asymmetric key which we can get using the following select statement:
DECLARE @Key_GUID UNIQUEIDENTIFIER SELECT @Key_GUID = Key_GUID And the second parameter is the data you want to encrypt. So the whole code will be like following: DECLARE @Key_GUID UNIQUEIDENTIFIER SELECT @Key_GUID = Key_GUID ‘using the @Key_Guid to tell the EncryptByAsymKey Method which Asymmetric key it ‘will be using to encrypt the data INSERT INTO Testencryption If you want to view the encrypted data, just select the data from the table using the following code: SELECT Data If you want to decrypt the data, just follow this code: select CONVERT(varchar(max),decryptbyasymkey(AsymKey_ID('Asym_key1'), Data,N'P@ssw0rd')) from Testencryption
For more Information check the following links: Visual Studio® Team System Code Name "Rosario" April 2008 CTP (VPC Image)Download Rosairo April 2008 CTP (VPC Image) from here April 15 Preparation Guide for Exam 70-556 TS: Microsoft Office PerformancePoint Server 2007, ApplicationsPerformance Point Server 2007 Training MaterialsCheck some of the PerformancePoint Server 2007 training Materials here April 08 Types of Encryption keys in SQL Server 2005 ? (Part I)
1. Symmetric key Protected by a Certificate which is protected by database Master key 2. Asymmetric key Protected by a database Master key
N.B: Don’t use Asymmetric keys unless you are in deep need for it as it requires high processing speed and try not to use it when dealing with multiple rows and data.
How to protect your data using Symmetric key?
First of all we need to create a Symmetric key, using the following code to create a symmetric key:
IF NOT EXISTS (SELECT * FROM sys.symmetric_keys WHERE name = ENCRYPTION BY PASSWORD='P@ssw0rd'
Secondly we need to open the created key to use it in encryption, so use the following code to open the key:
OPEN SYMMETRIC KEY PrivateData DECRYPTION
Then we create the table that we need to encrypt its data by using the following code or any other code you want to:
Id int identity(1,1) primary key, Data VARBINARY(255))
You need to declare your targeted field for encryption as varbinary data type to accept encryption.
We will need to open our symmetric key so we can use in encryption by using the following code:
OPEN SYMMETRIC KEY PrivateData DECRYPTION
Then we need to insert an encrypted value in our table, to do so we will need to use the EncryptByKey Method which takes four parameters two of them mandatory which are the Guid of the symmetric key which we can get using the following select statement:
DECLARE @Key_GUID UNIQUEIDENTIFIER SELECT @Key_GUID = Key_GUID And the second parameter is the data you want to encrypt; the other two parameters are optional. For more information about how to create symmetric key check this URL So the whole code will be like following: DECLARE @Key_GUID UNIQUEIDENTIFIER SELECT @Key_GUID = Key_GUID
‘using the @Key_Guid to tell the EncryptByKey Method which symmetric key it ‘will be using to encrypt the data INSERT INTO Testencryption If you want to view the encrypted data, just select the data from the table using the following code: SELECT Data If you want to decrypt the data, just follow this code: SELECT CONVERT (VARCHAR (50), By the way if you are tried to use AES – Advanced Encryption System on any operating system but Windows server 2003 it will generate this error because it’s only supported on the previously mentioned operating system. Msg 15314, Level 16, State 1, Line 1 Either no algorithm has been specified or the bitlength and the algorithm specified for the key are not available in this installation of Windows.
Part II will talk about the encryption with Asymmetric key. |
|
|