0
votes

How do I decrypt triple des encryption the file in c#. Which namespace?

This is the output file:

FirstCol  secondCol  EncryptSecondCol
1         value      00E35587C2CCEF4683B98686ED0483480100000054ABDFFB520111778B477223D1CD853F3CE33B4DD96C0F96

The sql that I used to create the encrypted file below:


--CREATE MASTER KEY
--ENCRYPTION BY PASSWORD = 'jjvc1234%';
--GO

---- Create a test certificate
--CREATE CERTIFICATE Tasc
--   WITH SUBJECT = 'Tasc Encryption', 
--   EXPIRY_DATE = '20301231';
--GO

--CREATE SYMMETRIC KEY TestTableKey
--WITH ALGORITHM = TRIPLE_DES ENCRYPTION
--BY CERTIFICATE Tasc
--GO

Declare @TestTable table(FirstCol varchar(8),secondCol varchar(100),EncryptSecondCol VARBINARY(256))
insert into @TestTable 
values('1','value',null);

--select * from @TestTable 

OPEN SYMMETRIC KEY TestTableKey DECRYPTION
BY CERTIFICATE Tasc
UPDATE @TestTable
SET EncryptSecondCol = ENCRYPTBYKEY(KEY_GUID('TestTableKey'),secondCol)

select * from @TestTable 

--OPEN SYMMETRIC KEY TestTableKey DECRYPTION
--BY CERTIFICATE Tasc
--SELECT FirstCol,SecondCol,CONVERT(VARCHAR(50),DECRYPTBYKEY(EncryptSecondCol)) AS DecryptSecondCol
--FROM @TestTable




--CLOSE SYMMETRIC KEY TestTableKey -- close them drop

--DROP SYMMETRIC KEY TestTableKey
--GO
--DROP CERTIFICATE Tasc
--GO
--DROP MASTER KEY
--GO


-- BACKUP CERTIFICATE Tasc
--TO FILE = 'c:\Temp\TestCert.cer'
2

2 Answers

1
votes
1
votes

You really don't want to do this - you now have your key in two places, which makes it that much harder to secure. If you give one system permission to encrypt using a symmetric key generally the same system should be doing the decryption.

In any case you can't export symmetric encryption keys from SQL, and as you used a certificate to act as a source for the symmetric key, and didn't provide an IDENTITY_VALUE or KEY_SOURCE when creating the symmetric cert SQL used random values for generating the key, so you can't reproduce it in C# either.