views:

63

answers:

1

Hi, I have inserted some data in table using encryption (encrypted by creating my own certificate.).

INSERT
INTO Person2(ContactID, eFirstName, eMiddleName, eLastName)
Values (1, EncryptByCert(Cert_ID('TestCertificate'), 'FirstName'),
   EncryptByCert(Cert_ID('TestCertificate'), 'Middle Name'), 
   EncryptByCert(Cert_ID('TestCertificate'), 'Last Name'))

Then updated other columns of the table like this

UPDATE Person2
SET FirstName = DecryptByCert(Cert_ID('TestCertificate'), eFirstName),
MiddleName = DecryptByCert(Cert_ID('TestCertificate'), eMiddleName),
LastName = DecryptByCert(Cert_ID('TestCertificate'), eLastName);

The end result is like this

1   楆獲乴浡e 楍摤敬丠浡e 慌瑳丠浡e 0x42712BB8DE86BE3E7BDAADF973476730D345EE5B4B3A3CFA2BDAF7128FEF6E85928AB69C8C866DFD66F65F0E8588C8463AE00984A81E8AAB712C1120DA20DD31BCB13C39971D4E48711AAF87A665F1B0809A06E69057861E828C4F82B7F6745722CE32C63F826FB3ECC26F59C525C2D6DE5D4B974B05F557963440E153A9483D 0x94003DE781AC56AC691DC883E2E3AB78975E36CEB378F7FD1F7844102DA07D6233F9B8D022C38A629960D7D5C7FEA6603B955F2DF82317B826F4472D5638F26EBFE46488BE35445144776CC4697A5852E0C6F68E302CEF0C9D32DFD4495E16EC80FF730F571A9499E463443E24FB66ED10BD57E6AC3D628038C859B4C8EEC049 0x2A9041F6BEBE9A42B76D7A1817A351073A59DD852D4C067614E5B6773BD0FCEC44A8E94E87B2F0B2D3AC7F8E5D8A831B159A675C81AED07AA4F9982FBF5689C713D80BA97DE19FA116A507268C0098AEB5F56C95FF83B2F2448ED6EB387444CF53A2666B34E0478BD4337CCFF5C007D960D96FEEF6FD77C8300805593569E352

The first tree coloums which are like chines language style should contain values 'FirstName' 'MiddleName' and 'LastName' CAn any one help whats the problem?

+1  A: 

Try this:

INSERT
INTO Person2(ContactID, eFirstName, eMiddleName, eLastName)
Values (1, EncryptByCert(Cert_ID('TestCertificate'), N'FirstName'),
    EncryptByCert(Cert_ID('TestCertificate'), N'Middle Name'),
    EncryptByCert(Cert_ID('TestCertificate'), N'Last Name'))

I think the problem might be that you are encrypting ANSI strings (varchar) but decrypting as unicode (nvarchar).

Matt Howells
Yes it works. Got the point. Thanks.
Waheed