SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


EncryptByPassPhrase/DecryptByPassfrase, why don't this work?


EncryptByPassPhrase/DecryptByPassfrase, why don't this work?

Author
Message
Edvard Korsbæk
Edvard Korsbæk
SSC-Addicted
SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)

Group: General Forum Members
Points: 457 Visits: 367
This query returns a lot of results:

select Convert(Char,DecryptByPassPhrase('[PASSPHRASE]',cpr_encrypted)) from dbo.personal

I have changed the actual passphrase to [PASSPHRASE] above.

one of the results is:

'070564-2869 '

Without the quotation marks, but there is a lot of blanks in it.

the field is defined as cpr_encrypted varbinary(256)

Then i use this to find it again:


select COUNT(*) from dbo.personal where CPR_encrypted = Convert(Char,DecryptByPassPhrase('[PASSPHRASE]','070564-2869 '))

Which returns zero (nul NULL, zero)

What am i doing wrong?


Best regards

Edvard Korsbæk
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88407 Visits: 45283
I think you've got that query the wrong way around. You're comparing the encrypted column with the results of the decryption function. You should be comparing the decrypted value with the results of the decryption function applied to the column.

Also, you're not specifying a length for char, you should. do you know what the default is?

Something roughly like this

select COUNT(*) 
from dbo.personal
where Convert(Char[<whatever the string length is>,DecryptByPassPhrase('[PASSPHRASE]',CPR_encrypted)) =
'070564-2869'



Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Edvard Korsbæk
Edvard Korsbæk
SSC-Addicted
SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)SSC-Addicted (457 reputation)

Group: General Forum Members
Points: 457 Visits: 367
Thanks!
did the trick even without lengt for char.
I wonder a bit - 070564-2869 - has 11 characters, but should i use 'With blanks'?

Best regards

Edvard Korsbæk
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88407 Visits: 45283
With blanks?

SQL ignores trailing spaces when comparing strings. Just make sure that the char length is longer than the string you're converting.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search