[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Encrypting and DECRYPTION

Posted on 2011-04-29
6
Medium Priority
?
695 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!!!
0
Comment
Question by:jssong2000
6 Comments
 
LVL 9

Expert Comment

by:sshah254
ID: 35492510
0
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 35492716
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
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 35493244
If you use SQL Server's encryption you cannot "descrpt in the asp.net/c# program."
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 4

Expert Comment

by:jimtpowers
ID: 35494836
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
 

Author Closing Comment

by:jssong2000
ID: 35508653
ENCRYPTBYPASSPHRASE and  DECRYPTBYPASSPHRASE worked pretty well. Thanks.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35509687
>>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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question