Monday, July 9, 2007

Encrypt Data using Extended Stored Procedure

Extended Store procedures are DLLs that an instance of sql server can load and run dynamically. You can create your own extended stored procedure for external routine using programming language such as C or You can use already created one. In this article I have use xp_md5.dll to explain data encryption using hash5 algorithm. I got this DLL from http://www.cr0.net:8040/code/crypto/md5/ .

1. Extract this DLL to C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn (or appropriate place)
2. Create Extended Stored procedure called xp_md5 in the master data base. Right click on Extended Stored Procedure and select New Extended Store Procedure. Enter xp_md5 for name and select full path to dll.( C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\ xp_md5.dll)

Or
EXEC sp_addextendedproc 'xp_md5', 'xp_md5.dll'
3. Create a user-defined function for each database in which you plan to use the MD5 procedure

CREATE FUNCTION [dbo].[fn_md5] (@data TEXT)
RETURNS CHAR(32) AS
BEGIN
DECLARE @hash CHAR(32)
EXEC master.dbo.xp_md5 @data, -1, @hash OUTPUT
RETURN @hash
END

Use user define function (fn_md5)
SELECT dbo.fn_md5('Lakshman')

Out Put

47a502ffc3c3b43a4e4856fa88290bdd

Or if you want store return value in variable.
DECLARE @hash CHAR(32)
EXEC master.dbo.xp_md5 'Lakshman', -1, @hash OUTPUT
PRINT @hash
Result will same.

1 comment:

Tharindu Weerasinghe said...

It is great! I feel you are more interested in Cryptography. So, I think you can try DES(Data Encryption Standards) in your s/w projects. But it is subjected to Brute Force attack....But it seems not to happen!

The basic structure of DES encryption is as follows: The 64 bit input is first permuted, then subjected to sixteen rounds, each of which takes the 64 bit output of the previous round and a 48 bit per-round key and produces a 64 bit output. The per-round keys are different 48 bit subsets of the 56 bit key. After the round, the 64 bit output is subjected to the inverse initial permutation. DES decryption is essentially done by running this process backwards.

A single DES encryption round involves halving the 64 bit input, mangling the right half and the per-round key together and XOR ing this with the left half, the result is the new right half while the new left half is simply the old right half. A decryption round first mangles the left half of the input and the per-round key together and XOR s this with the right half of the input to produce the next left half, the new right half being the left half of the input.

In cryptography, RSA is an algorithm for public-key cryptography(Asymmetric Key). It was the first algorithm known to be suitable for signing as well as encryption, and one of the first great advances in public key cryptography. So in modern day web-based projects especially in user log-in systems I think it is better to use RSA algorithm to encript data!!!A message encrypted with the public key can be decrypted only with the corresponding private key(which is known by you only).
Unlike symmetric algorithms, asymmetric key algorithms use a different key for encryption than for decryption. I.e., a user knowing the encryption key of an asymmetric algorithm can encrypt messages, but cannot derive the decryption key and cannot decrypt messages encrypted with that key.

I just put my comment...because I am really keen on Cryptography and Network Security!!!