In symmetric key encryption technique both the sender and receiver have same key to encrypt the data in which both sender and receiver can encrypt the data. It is easy to implement.
Asymmetric key encryption: Asymmetric key technique is different from the symmetric key technique. It 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. This is a relatively complex system where the sender can use his key to encrypt the message but he cannot decrypt it. The receiver, on the other hand, can use his key to decrypt the message but he cannot encrypt it.
Symmetric encryption algorithm types are the following:
TripleDES: It is the improved version of des algorithm. DES uses 56 bits of a 64-bit key to encrypt messages in fixed-sized blocks of data and Triple DES use 3 DES.
ADVANTAGE: Key size of 3DES is larger than DES.
DISADVANTAGE: The process of 3DES is very slow.
RC2:
It is the replacement of the DES. RC2 encrypts data in 64-bit blocks and has a variable key size of 8 to 128 bits in 8-bit increments. Encryption algorithms strength lies in the length of its keys. If size of key is big then encryption algorithm is more strong.
Elaborate Triple Des with 192 bit in more details. This algorithm is considered more secure and faster.
Steps to create symmetric key and use in database encryption: Before creating the symmetric key we need to create master key and a certificate, which act as protectors of our symmetric key store.
Create a Database Master Key
- CREATE MASTER KEY ENCRYPTION BY PASSWORD = ‘myStrongPassword’
Create a certificate
- CREATE CERTIFICATE MyCertificateName
- WITH SUBJECT =’Label for Certificate’
After created master key and certificate now we add symmetric key to certificate.
Create a Symmetric Key
- CREATE SYMMETRIC KEY MySymmetrickeyName
- WITH ALGORITHM = TRIPLE_DES ENCRYPTION
- BY CERTIFICATE MyCertificateName
After creating the symmetric key we use that key for encrypt/decrypt the data. Firstly, we need to open symmetric key to encrypt/decrypt the data.
Query to open Symmetric key
- OPEN SYMMETRIC KEY MySymmetricKeyName
- DECRYPTION BY CERTIFICATE MyCertificateName
Now, we can use encrypt function to encrypt the data using the following query.
Encrypting Data Query
- DECLARE @Result varbinary(256)
- SET @Result = ENcryptByKey(Key_GUID(‘MySymmetricKeyName’)),
- @ValueToEncrypt)
- Decrypt Data Query
- DECLARE @Result varbinary(max)
- SET @Result = ENcryptByKey(Key_GUID(‘MySymmetricKeyName’)),
- @ValueToEncrypt)
Steps to implement the Encryption feature in database with Screenshots,
Step 1: Query to create user table for implementing encryption process.
Step 2: Query to insert data in user table.
Step 3: Create a master key in database using below query.
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'EncryptionUser@123'
Step 4: Now create certificate in same database using below query.
CREATE CERTIFICATE UserTestCert WITH SUBJECT = 'EncryptionUserCert'
Step 5: Create symmetric key.
CREATE SYMMETRIC KEY EncryptTableKey WITH ALGORITHM = TRIPLE_DES ENCRYPTION
BY CERTIFICATE UserTestCert
Step 6: After creating symmetric key in database use that key to encrypt and decrypt the data.
Step 7: After executing the update query data is in encrypted mode.
Step 8: Query to decrypt the data.
Queries to drop the key and certificate from database,
- CLOSE SYMMETRIC KEY EncryptTableKey
- GO
- DROP SYMMETRIC KEY EncryptTableKey
- GO
- DROP CERTIFICATE EncryptionUserCert
- GO
- DROP MASTER KEY
- GO
Advantages of using Encryption technology,