Friday, May 27, 2011

ENCRYPTION DECRYPTION

CREATE TABLE
THEN
%-- INSERTING THE VALUE--%
insert into testTable (firstcol,secondcol)values(1,'first')
 insert into testTable (firstcol,secondcol)values(2,'second')
 insert into testTable (firstcol,secondcol)values(3,'third')
 insert into testTable (firstcol,secondcol)values(4,'fourth')
 insert into testTable (firstcol,secondcol)values(5,'fifth')
 insert into testTable (firstcol,secondcol)values(6,'sixth')
 
select* from testtable
CREATING MASTER KEY AND CERTIFICATE
create master key encryption by password='SqlAuthority'
create CERTIFICATE encryptTestcert with subject='sqlAuthority'
create symmetric key testTableKey with algorithm =TRIPLE_DES
encryption by certificate encryptTestcert

ALTER TABLE TestTable add encryptsecondcol varbinary(256)
DECRYPTING KEY
open symmetric key testtablekey decryption by certificate encryptTestcert
update testtable set encryptsecondcol=encryptbykey(key_guid('testtablekey'),secondcol)

select * from testtable

OPEN symmetric key testtablekey decryption by certificate encryptTestcert
select convert(varchar(50),decryptbykey(encryptsecondcol)) as decryptseconcol from testtable

No comments:

Post a Comment