Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Column Encryption

I have a couple of columns in my application that need to encrypted for security reasons. How is this possible with SQL Server 2008? Also, would I need to always manually decrypt them or will it happen automatically?

Thanks,

Sachin

like image 556
Sachin Kainth Avatar asked Nov 30 '25 16:11

Sachin Kainth


2 Answers

You can create a Certificate and then a Symmetric Key that uses the certificate:

CREATE CERTIFICATE YourCertificate
 WITH SUBJECT = 'Encrypted Content';
GO

CREATE SYMMETRIC KEY SSN_Key
  WITH ALGORITHM = AES_256
   ENCRYPTION BY CERTIFICATE YourCertificate;
GO

To Decrypt: You can use DecryptByKey:

The example from MSDN is:

SELECT CardNumber, CardNumber_Encrypted AS 'Encrypted card number',
   CONVERT(nvarchar,
   DecryptByKey(CardNumber_Encrypted, 1 , 
   HashBytes('SHA1', CONVERT(varbinary, CreditCardID)))) AS 'Decrypted card number'  
FROM Sales.CreditCard;

http://msdn.microsoft.com/en-us/library/ms179331(v=sql.105).aspx

like image 71
Darren Avatar answered Dec 02 '25 04:12

Darren


Take a look MSDN post How to: Encrypt a Column of Data

like image 37
Rahul Avatar answered Dec 02 '25 05:12

Rahul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!