Thursday, January 17, 2013

Encrypted Field

The steps to encrypt a field is easy, full details are here



CREATE MASTER KEY ENCRYPTION BY
PASSWORD = 'your password here'
GO

CREATE CERTIFICATE EncryptTestCert
    WITH SUBJECT = 'name'
GO


CREATE SYMMETRIC KEY cryptoKey
    WITH ALGORITHM = TRIPLE_DES
    ENCRYPTION BY CERTIFICATE EncryptTestCert
GO


To get encrypted content

OPEN SYMMETRIC KEY cryptoKey
DECRYPTION BY CERTIFICATE EncryptTestCert



ENCRYPTBYKEY(KEY_GUID('cryptoKey'),@input) 



To get decrypted content

OPEN SYMMETRIC KEY cryptoKey
DECRYPTION BY CERTIFICATE EncryptTestCert




CONVERT(nvarchar(Max), DecryptByKey(fieldName))