We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Encrypting and DECRYPTION

Medium Priority
739 Views
Last Modified: 2012-05-11
I have a complicated column in a sql server 2005 table. It's composed of very complicated string like "something  D:\pathe\myapp.exe" or/and
"antprd Not Found[/usr/local/qt/examples/network/httpd]".....

How to encript it and I could pull and descrpt in the asp.net/c# program.

Appreciated!!!
Comment
Watch Question

Aaron ShiloChief Database Architect
CERTIFIED EXPERT

Commented:
hi

checkout this simple demo for using encryption on columns


-- Create Database   
USE master   
GO   
CREATE DATABASE EncryptTest ON  PRIMARY  
 ( NAME = N'EncryptTest', FILENAME = N'C:\db\EncryptTest.mdf')    
 LOG ON  
 ( NAME = N'EncryptTest_log', FILENAME = N'C:\db\EncryptTest_log.ldf')    
GO  


-- 2 --
-- Create table and insert data in the table   
USE EncryptTest   
GO   
CREATE TABLE TestTable (FirstCol INT, SecondCol VARCHAR(50))    
GO   
INSERT INTO TestTable (FirstCol, SecondCol)    
SELECT 1,'First'  
UNION ALL  
SELECT 2,'Second'  
UNION ALL  
SELECT 3,'Third'  
UNION ALL  
SELECT 4,'Fourth'  
UNION ALL  
SELECT 5,'Fifth'  
GO   
-- Check the content of the TestTable   
USE EncryptTest   
GO   
SELECT *   
FROM TestTable   
GO  

-- 3 --

-- Create Database Master Key   
USE EncryptTest   
GO   
CREATE MASTER KEY ENCRYPTION BY  
PASSWORD = 'SQLAuthority'  
GO  


-- 4 --

-- Create Encryption Certificate   
USE EncryptTest   
GO   
CREATE CERTIFICATE EncryptTestCert   
    WITH SUBJECT = 'SQLAuthority'  
GO  

-- 5 --

The symmetric key can be encrypted by using any of the certificate, 
password, and symmetric key, asymmetric key options. 
We can use many different algorithms for encrypting key. 
Supported algorithms are DES, TRIPLE_DES, RC2, RC4, RC4_128, DESX, AES_128, AES_192, 
and AES_256. 

-- Create Symmetric Key   
USE EncryptTest   
GO   
CREATE SYMMETRIC KEY TestTableKey   
    WITH ALGORITHM = TRIPLE_DES   
    ENCRYPTION BY CERTIFICATE EncryptTestCert   
GO  

-- 6 --

-- Encrypt Data using Key and Certificate   
-- Add Columns which will hold the encrypted data in binary   
USE EncryptTest   
GO   
ALTER TABLE TestTable   
ADD EncryptSecondCol VARBINARY(256)    
GO  
Select * from TestTable
GO
-- 7 --

-- Update binary column with encrypted data created by certificate and key   
USE EncryptTest   
GO   
OPEN SYMMETRIC KEY TestTableKey   
DECRYPTION BY CERTIFICATE EncryptTestCert   
UPDATE TestTable   
SET EncryptSecondCol = ENCRYPTBYKEY(KEY_GUID('TestTableKey'),SecondCol)    
GO  
Select * from TestTable
GO

-- 8 --

-- DROP original column which was encrypted for protect the data   
USE EncryptTest   
GO   
ALTER TABLE TestTable   
DROP COLUMN SecondCol   
GO  



-- 9 --

-- Check the content of the TestTable   
USE EncryptTest   
GO   
SELECT *   
FROM TestTable   
GO  

-- 10 --

-- Decrypt the data of the SecondCol   
USE EncryptTest   
GO   
OPEN SYMMETRIC KEY TestTableKey   
DECRYPTION BY CERTIFICATE EncryptTestCert   
SELECT CONVERT(VARCHAR(50),DECRYPTBYKEY(EncryptSecondCol)) AS DecryptSecondCol   
FROM TestTable   
GO  


-- 11 --

-- Clean up database   
USE EncryptTestGO   
CLOSE SYMMETRIC KEY TestTableKey   
GO   
DROP SYMMETRIC KEY TestTableKey   
GO   
DROP CERTIFICATE EncryptTestCert   
GO   
DROP MASTER KEY  
GO   
USE [master]    
GO   
DROP DATABASE [EncryptTest]    
GO  

Open in new window

CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Here are samples I use in one of my applications. The data is encrypted in the column but also allows me to decrypt the data if necessary. I do all of the encrypt/descript within stored procedures that are secured so someone can't get the database and figure out how to decrypt the column. Default_PWD and Contact_ID are other fields in this particular database.

ENCRYPTBYPASSPHRASE(Default_PWD, 'tempPassword1', 1, Contact_ID)
CONVERT(VARCHAR, DECRYPTBYPASSPHRASE(Default_PWD, Enc_User_PWD, 1, Contact_ID))

Open in new window

Author

Commented:
ENCRYPTBYPASSPHRASE and  DECRYPTBYPASSPHRASE worked pretty well. Thanks.
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>ENCRYPTBYPASSPHRASE and  DECRYPTBYPASSPHRASE worked pretty well. Thanks.<<
Than may I suggest you award the points to the correct solution.  Here's how:
I accepted the wrong solution. What do I do?
https://www.experts-exchange.com/help.jsp#hs=29&hi=409
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.