Monday, June 14, 2010

Encrypt column data in SQL server

CREATE FUNCTION [dbo].[Encrypt] ( @InputString VARCHAR(4000) )

RETURNS NVARCHAR(4000) WITH ENCRYPTION AS

BEGIN

DECLARE @vEncryptedString NVARCHAR(4000)

DECLARE @vIdx INT

DECLARE @vBaseIncrement INT

SET @vIdx = 1

SET @vBaseIncrement = 128

SET @vEncryptedString = ''''

WHILE @vIdx <= LEN(@InputString)

BEGIN

SET @vEncryptedString = @vEncryptedString +

NCHAR(ASCII(SUBSTRING(@InputString, @vIdx, 1)) +

@vBaseIncrement + @vIdx - 1)

SET @vIdx = @vIdx + 1

END

RETURN @vEncryptedString

END

To decrypt check following


No comments:

Post a Comment