Question : Encryption\Decryption in SQL SERVER 2005

I have done these steps

1) CREATE MASTER KEY ENCRYPTION BY PASSWORD ='P@SS'

2) CREATE ASYMMETRIC KEY ABC
WITH ALGORITHM=RSA_1024
ENCRYPTION BY PASSWORD = 'P@SSP@SS'

3) CREATE SYMMETRIC KEY XYZ
WITH ALGORITHM=DES
ENCRYPTION BY PASSWORD = 'P@SSP@SSP@SS'

4)CREATE TABLE TEST(A VARCHAR(30),
B VARBINARY(8000));

5)INSERT INTO TEST VALUES('JAHANZAIB',NULL);

6)UPDATE TEST
SET B=ENCRYPTBYKEY(KEY_GUID('XYZ'),A);

NOW COLUMN HAS BEEN ENCRYPTED AND I M GOING TO DECRYPT THE DATA

7)SELECT CONVERT(VARCHAR,(DECRYPTBYKEY(B)) FROM TEST;

IS THERE ANY AUTHENTICATION AND WHERE WE WILL USE THE PASSWORD WHICH ONE I HAVE MENTIONED IN THE
CREATE STATEMENT


THANKS,



Answer : Encryption\Decryption in SQL SERVER 2005

Whenever you use a SYMMETRIC KEY to encrypt and when you created it you used a password, you have to OPEN the SYMMETRIC key in order to use it, and you have to specify the password because there is no way for SQL to open it when it is password based.

The sequence would be:

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'P@SS'
OPEN MASTER KEY DECRYPTION BY PASSWORD = 'P@SS'

CREATE CERTIFICATE CertEncrypt
WITH SUBJECT = 'CertSubject',
EXPIRY_DATE = '1/1/2009'

CREATE SYMMETRIC KEY Key2Encrypt WITH ALGORITHM=AES_256
ENCRYPTION BY CERTIFICATE CertEncrypt

CLOSE MASTER KEY

OPEN SYMMETRIC KEY DECRYPTION BY CERTIFICATE CertEncrypt
INSERT INTO TEST VALUES(EncryptByKey(key_guid('Key2Encrypt'), 'JAHANZAIB')
CLOSE SYMMETRIC KEY Key2Encrypt

SELECT CONVERT(VARCHAR,DecryptByKeyAutoCert(cert_ID('CertEncrypt'), NULL, B)) FROM TEST
Random Solutions  
 
programming4us programming4us