Hello All,I am trying to implement the column encryption on one of the tables, have used the below link as the reference and got stuck at the last step.http://benjii.me/2010/05/how-to-use-sql-server-encryption-with-symmetric-keys/I have completed the following steps so far.- CREATE MASTER KEY ENCRYPTION BY PASSWORD = ‘myStrongPassword’- CREATE CERTIFICATE MyCertificateNameWITH SUBJECT = 'A label for this certificate'- CREATE SYMMETRIC KEY MySymmetricKeyName WITHIDENTITY_VALUE = 'a fairly secure name',ALGORITHM = AES_256,KEY_SOURCE = 'a very secure strong password or phrase'ENCRYPTION BY CERTIFICATE MyCertificateName;- CREATE PROCEDURE OpenKeysASBEGIN SET NOCCOUNT ON; BEGIN TRY OPEN SYMMETRIC KEY MySymmetricKeyName DECRYPTION BY CERTIFICATE MyCertificateName END TRY BEGIN CATCH -- Handle non-existant key here END CATCHEND-CREATE FUNCTION Encrypt( @ValueToEncrypt varchar(max))RETURNS varbinary(256)ASBEGIN -- Declare the return variable here DECLARE @Result varbinary(256) SET @Result = EncryptByKey(Key_GUID('MySymmetricKeyName'), @ValueToEncrypt) -- Return the result of the function RETURN @ResultEND- CREATE FUNCTION Decrypt( @ValueToDecrypt varbinary(256))RETURNS varchar(max)ASBEGIN -- Declare the return variable here DECLARE @Result varchar(max) SET @Result = DecryptByKey(@ValueToDecrypt) -- Return the result of the function RETURN @ResultENDexample by using the functionEXEC OpenKeys-- EncryptingSELECT Encrypt(myColumn) FROM myTable-- DecryptingSELECT Decrypt(myColumn) FROM myTablewhen I ran the last command :-- DecryptingSELECT Decrypt(myColumn) FROM myTableI get the following error :Msg 257, Level 16, State 3, Line 2Implicit conversion from data type nvarchar to varbinary is not allowed. Use the CONVERT function to run this query.Edit - where will I use the convert function, in decrypt function or in select statement?Can some one please help me in this regard?
↧