Friday, June 18, 2010

Symmetric Key Encryption

Symmetric Key Encryption
In a symmetric key algorithm, there is but one key. That same key is used to encrypt the data and decrypt, the data. If someone were to get possession of the key, that person could take anything you've encrypted, and decrypt it immediately.

Advantages
Symmetric key encryption is known to be faster and stronger than their asymmetric counterpart. It uses less overhead on system resources. For simple encryption this is the best way.
When a symmetric key is created, the symmetric key must be encrypted by using at least one of the following: certificate, password, symmetric key, asymmetric key, or PROVIDER. The key can have more than one encryption of each type. In other words, a single symmetric key can be encrypted by using multiple certificates, passwords, symmetric keys, and asymmetric keys at the same time
Here I am going to explain the simplest way of encrypting data in a database, i.e. Symmetric Key Encryption with Password
Symmetric Key Encryption with Password
Using this method is very simple and can be used if we want to encrypt data in whole column.
The simple example below will help you understand.
USE master
GO
CREATE DATABASE RKN_Test
ON PRIMARY ( NAME = N'RKN_Test', FILENAME = N'D:\RKN_Test.mdf')—Any Path you prefer
LOG ON ( NAME = N'RKN_Test_log', FILENAME = N'D:\RKN_Test_log.ldf')
GO

Master key is used to encrypt the Certificates and Keys in a database.
Passwords should meet windows password policy if you have set any.

USE RKN_Test
GO
CREATE MASTER KEY ENCRYPTION
BY PASSWORD = 'RKN@123'
GO

We can use many types of algorithm while creating Symmetric keys like DES, TRIPLE_DES, TRIPLE_DES_3KEY, AES_128, AES_192, AES_256 etc.Please be careful while choosing the encryption algorithm.

USE RKN_Test
GO
CREATE SYMMETRIC KEY RKN_TableKey
WITH ALGORITHM = TRIPLE_DES
ENCRYPTION BY PASSWORD='Passw0rd1'

USE RKN_Test
GO
CREATE TABLE TestTable (FirstCol INT, SecondCol VARBINARY(256))
GO

SELECT * FROM TestTable

USE RKN_Test
GO
OPEN SYMMETRIC KEY RKN_TableKey DECRYPTION
BY PASSWORD='Passw0rd1'
INSERT INTO TestTable VALUES (1,
ENCRYPTBYKEY(KEY_GUID('RKN_TableKey'),'Testing'))
GO
CLOSE SYMMETRIC KEY RKN_TableKey

USE RKN_Test
GO
OPEN SYMMETRIC KEY RKN_TableKey DECRYPTION
BY PASSWORD='Passw0rd1'
SELECT FirstCol,CONVERT(VARCHAR(50),DECRYPTBYKEY(SecondCol)) AS DecryptedValue
FROM TestTable
GO

1 comment:

  1. Is symmetric key encryption more powerful that asymmetric form of encryption ? I am curious to know the major points of difference between both these encryption forms and which is the most powerful solution.
    digital signature certificate

    ReplyDelete