Call now: (800) 766-1884  



 Home


 SQL Server Tips
 SQL Server Training

 SQL Server Consulting
 SQL Server Support
 SQL Server Remote DBA



 Articles
 Services
 SQL Server Scripts
 Scripts Menu



 

 

 

   
  SQL Server Tips by Gama and Naughter


TEA encryption with TSQL

 

This first example will use two UDF's to encrypt and decrypt data: UDFencTEA and UDFdecTEA. They were written in TSQL and the code is not very legible because TSQL does not have unsigned integers and bit shift operators that the TEA algorithm uses. The workaround was to use a bigint to store the values, multiplication instead of shifting and AND masks to remove the overflow bits during the calculations.

The table will have one field, where to store the encrypted data:

 

CREATE TABLE tblCrypt(secret varchar(8000))

 

The insert trigger will encrypt data added to the table:

 

CREATE TRIGGER "tblCrypt_ITrig" ON tblCrypt

INSTEAD OF INSERT

AS

SET NOCOUNT ON

INSERT tblCrypt(secret)

SELECT dbo.UDFencTEA(secret, 'abc') FROM INSERTED

 

The update trigger will encrypt data changed in the table:

 

CREATE TRIGGER "tblCrypt_UTrig" ON tblCrypt

INSTEAD OF UPDATE

AS

SET NOCOUNT ON

UPDATE tblCrypt SET secret= dbo.UDFencTEA(INSERTED.secret, 'abc') FROM INSERTED

 

A view will return the decrypted data:

 

CREATE VIEW dbo.VIEW_tblCrypt

AS

SELECT dbo.UDFdecTEA(secret, 'abc') as secret

FROM  dbo.tblCrypt

 

This code will insert 10,000 records in a second table used for testing purposes:

 

CREATE TABLE tbl10k(secret varchar(8000))

DECLARE @counter int, @data varchar(8000)

SET @data=REPLICATE('a',8000)

SET @counter=1

WHILE @counter <=10000

  BEGIN

  INSERT tbl10k(secret) VALUES(@data)

  SET @counter=@counter + 1

  END

 

Each record will be 8000 characters long. The contents of this table will be copied to the first one and fire the trigger:

 

DECLARE @start datetime

SET @start=GETDATE()

INSERT tblcrypt(secret)

SELECT secret from tbl10k

SELECT 'time=',datediff(ms,@start, GETDATE())

 

The overhead of the UDF call plus the overhead of the Crypto API cause a small performance loss. There will be several rounds of encrypting blocks of data, which are extremely fast but the overhead mentioned before is high enough as to dwarf this gain. The insert trigger will encrypt the records, one at a time, running the slow TSQL calculations repeatedly.


The above book excerpt is from:

Super SQL Server Systems
Turbocharge Database Performance with C++ External Procedures

ISBN: 0-9761573-2-2
Joseph Gama, P. J. Naughter

 http://www.rampant-books.com/book_2005_2_sql_server_external_procedures.htm
 

 

Burleson Consulting Remote DB Administration


 

 


 

 

 

 

 
Burleson is the America's Team

Note: The pages on this site were created as a support and training reference for use by our staff of DBA consultants.  If you find it confusing, please exit this page.

Errata?  SQL Server technology is changing and we strive to update our SQL Server support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:and include the URL for the page.
 


Burleson Consulting
SQL Server database support

 

Copyright 1996 -  2013 by Vaaltech Web Services. All rights reserved.

Hit Counter