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
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
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
First off: Create your assym Key using this code:
USE [yourDB]
GO
CREATE ASYMMETRIC KEY ClaveAsym
WITH ALGORITHM = RSA_2048 ---->Take a look in this type of algorithms
ENCRYPTION BY PASSWORD = 'yourKey'
GO
Remember this, you must to declare a variable always that you want to decrypt or encrypt:
DECLARE @KEYID INT SET @KEYID = AsymKey_ID('ClaveAsym')
Decrypting data
SELECT columnA, CAST(DecryptByAsymKey(@keyid, columnUser, N'yourKey') AS VARCHAR(100)), CONVERT(VARCHAR(MAX), DECRYPTBYASYMKEY(@keyId, columnPass, N'yourKey')) FROM yourTable
Encrypting Data
DECLARE @User VARCHAR(MAX), @pass VARCHAR(MAX)
SET @User = 'yourUser'
sET @pass = 'yourPass'
DECLARE @KEYID INT SET @KEYID = AsymKey_ID('ClaveAsym')
INSERT INTO yourTable( User, passw) VALUES EncryptByAsymKey (@keyid, @User ), EncryptByAsymKey (@keyid, @pass))