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!!!
jssong2000Asked:
Who is Participating?
 
Anthony PerkinsConnect With a Mentor Commented:
If you use SQL Server's encryption you cannot "descrpt in the asp.net/c# program."
0
 
sshah254Commented:
0
 
Aaron ShiloChief Database ArchitectCommented:
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

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
jimtpowersCommented:
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

0
 
jssong2000Author Commented:
ENCRYPTBYPASSPHRASE and  DECRYPTBYPASSPHRASE worked pretty well. Thanks.
0
 
Anthony PerkinsCommented:
>>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?
http://www.experts-exchange.com/help.jsp#hs=29&hi=409
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.