Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 22nd, 2006, 01:12 AM
Authorized User
 
Join Date: Nov 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jvraman
Default Encryption & Decryption

how to encrypt a data before storing it in the database and how to decrypt when retrieved.

Regards
Venkat
__________________
Regards
Venkat
 
Old June 22nd, 2006, 02:48 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Use this function for very simple encryption/decryption
CREATE FUNCTION dbo.fnSimpleEncDec
(
    @StringText VARCHAR(8000),
    @PasswordCharacter CHAR(1)
)
RETURNS VARCHAR(8000)
AS

BEGIN
    DECLARE @Index SMALLINT,
        @ReturnText VARCHAR(8000)

    SELECT @Index = DATALENGTH(@StringText),
        @ReturnText = ''

    WHILE @Index > 0
        SELECT @ReturnText = CHAR(ASCII(SUBSTRING(@StringText, @Index, 1)) ^ ASCII(@PasswordCharacter)) + @ReturnText,
            @Index = @Index - 1

    RETURN @ReturnText
END

To encrypt the fields, write

UPDATE MyTable
SET first_name = dbo.fnSimpleEncDec(first_name, '?'),
       last_name = dbo.fnSimpleEncDec(last_name, '*')

To decrypt the fields, write

UPDATE MyTable
SET first_name = dbo.fnSimpleEncDec(first_name, '?'),
       last_name = dbo.fnSimpleEncDec(last_name, '*')

You can use whatever character you like as password. There is one caveat or drawback! You can't use a password character that normally exist in the field, such as 'e'. If you do, SQL server truncates the string at the position where field contains same character as password character used.

For names, I would prefer using
SELECT dbo.fnSimpleEncDec(first_name, CHAR(31))
because then I will XOR 5 bits out of 8 for every character in the text to be encrypted or decrypted! And the text still looks somewhat normal.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Encryption/Decryption with rijndael ?!? vbmo Classic ASP Basics 3 November 17th, 2009 04:14 AM
Encryption/Decryption Problem param99 SQL Server 2000 4 December 14th, 2006 05:20 AM
Encryption and Decryption kvanchi General .NET 1 February 2nd, 2005 02:04 PM
Encryption and Decryption kvanchi General .NET 2 January 24th, 2005 02:52 PM
Encryption/Decryption using IUSR msrnivas General .NET 0 January 12th, 2005 12:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.