Link to home
Start Free TrialLog in
Avatar of jssong2000
jssong2000

asked on

Encrypting and DECRYPTION

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!!!
Avatar of sshah254
sshah254

Avatar of Aaron Shilo
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

ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of jssong2000

ASKER

ENCRYPTBYPASSPHRASE and  DECRYPTBYPASSPHRASE worked pretty well. Thanks.
>>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