Monday, February 20, 2012

Problem using assymmetric keys for encryption/decryption

Hello,
I'm trying to use assymetric keys for encryption/decryption and have
some problems with it, it turned out that EncryptByAsymKey always
returns null, see the following code as a illustration
CREATE DATABASE encryption1
GO
USE encryption1
-- Create the Master database key
CREATE MASTER KEY ENCRYPTION BY PASSWORD='P@.ssword'
-- Create a assymetric key
CREATE ASYMMETRIC KEY aKey
WITH ALGORITHM = RSA_512
-- Use the keys to encrypt data
-- Because Assymmetric key is protected by Database Master Key must not
be opened
DECLARE @.word VARCHAR(max),
@.aword VARBINARY(max)
SET @.word='Unencrypted data'
-- Encrypt using the symmetric key , adding a authenticator '1000', 1)
SET @.aword=EncryptByAsymKey(AsymKey_ID('aKey
'),@.aword)
SELECT @.word 'Original',
@.aword 'Assymetric Encrypted',
Convert(VARCHAR,DecryptByAsymKey(AsymKey
_ID('aKey'),@.aword))
'Assymmetric Decrypted'
-- Close the SYMMETRIC KEY
CLOSE SYMMETRIC KEY sKey
-- Drop the test database
DROP DATABASE encryption
SELECT AsymKey_ID('aKey')
Any help will be really appreciated.
Thanks a lot in advance
Marc LertensThe line:

> SET @.aword=EncryptByAsymKey(AsymKey_ID('aKey
'),@.aword)
should be:

> SET @.aword=EncryptByAsymKey(AsymKey_ID('aKey
'),@.word)
@.aword is not yet set, so the result of encrypting it is NULL.
Also, note that asymmetric key encryption is not suited for encrypting data,
it is usually used to protect other keys or for signing. You should use
symmetric key encryption for protecting data.
Thanks
Laurentiu Cristofor [MSFT]
Software Design Engineer
SQL Server Engine
http://blogs.msdn.com/lcris/
This posting is provided "AS IS" with no warranties, and confers no rights.
"Marc Mertens" <marc.mertens@.azlan.com> wrote in message
news:ucm05ZTFGHA.1760@.TK2MSFTNGP10.phx.gbl...
> Hello,
> I'm trying to use assymetric keys for encryption/decryption and have
> some problems with it, it turned out that EncryptByAsymKey always returns
> null, see the following code as a illustration
> CREATE DATABASE encryption1
> GO
> USE encryption1
> -- Create the Master database key
> CREATE MASTER KEY ENCRYPTION BY PASSWORD='P@.ssword'
> -- Create a assymetric key
> CREATE ASYMMETRIC KEY aKey
> WITH ALGORITHM = RSA_512
> -- Use the keys to encrypt data
> -- Because Assymmetric key is protected by Database Master Key must not be
> opened
> DECLARE @.word VARCHAR(max),
> @.aword VARBINARY(max)
> SET @.word='Unencrypted data'
> -- Encrypt using the symmetric key , adding a authenticator '1000', 1)
> SET @.aword=EncryptByAsymKey(AsymKey_ID('aKey
'),@.aword)
> SELECT @.word 'Original',
> @.aword 'Assymetric Encrypted',
> Convert(VARCHAR,DecryptByAsymKey(AsymKey
_ID('aKey'),@.aword))
> 'Assymmetric Decrypted'
> -- Close the SYMMETRIC KEY
> CLOSE SYMMETRIC KEY sKey
> -- Drop the test database
> DROP DATABASE encryption
> SELECT AsymKey_ID('aKey')
> Any help will be really appreciated.
> Thanks a lot in advance
> Marc Lertens|||Laurentiu Cristofor [MSFT] wrote:
> The line:
>
>
> should be:
>
>
> @.aword is not yet set, so the result of encrypting it is NULL.
> Also, note that asymmetric key encryption is not suited for encrypting dat
a,
> it is usually used to protect other keys or for signing. You should use
> symmetric key encryption for protecting data.
> Thanks
>
Thank you for replying, it was indeed a silly mistake from me (did not
see my typing error). I'm aware that assymetric encryption is slow and
that you should use symmetric encryption for your data and assymetric
encryption for signing or exchanging a symmetric key, but the code was
just some test code.
Thanks for your help
Marc Mertens

No comments:

Post a Comment