Monday, 19 March 2018
Data encrytion code VB,net Tsql
Found this knocking around the next and it works well if you need some encryption.
Create the following function in tsql
/****** Object: UserDefinedFunction [dbo].[fnEncDecRc4] Script Date: 03/19/2018 12:31:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fnEncDecRc4]
(
@Pwd VARCHAR(256),
@Text VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @Box TABLE (i TINYINT, v TINYINT)
INSERT @Box
(
i,
v
)
SELECT i,
v
FROM dbo.fnInitRc4(@Pwd)
DECLARE @Index SMALLINT,
@i SMALLINT,
@j SMALLINT,
@t TINYINT,
@k SMALLINT,
@CipherBy TINYINT,
@Cipher VARCHAR(8000)
SELECT @Index = 1,
@i = 0,
@j = 0,
@Cipher = ''
WHILE @Index <= DATALENGTH(@Text)
BEGIN
SELECT @i = (@i + 1) % 256
SELECT @j = (@j + b.v) % 256
FROM @Box b
WHERE b.i = @i
SELECT @t = v
FROM @Box
WHERE i = @i
UPDATE b
SET b.v = (SELECT w.v FROM @Box w WHERE w.i = @j)
FROM @Box b
WHERE b.i = @i
UPDATE @Box
SET v = @t
WHERE i = @j
SELECT @k = v
FROM @Box
WHERE i = @i
SELECT @k = (@k + v) % 256
FROM @Box
WHERE i = @j
SELECT @k = v
FROM @Box
WHERE i = @k
SELECT @CipherBy = ASCII(SUBSTRING(@Text, @Index, 1)) ^ @k,
@Cipher = @Cipher + CHAR(@CipherBy)
SELECT @Index = @Index +1
END
RETURN @Cipher
END
Then use the following to call it this is VB.net.
first call sets the encryption
select database name.dbo.fnEncDecRc4('encryption key','OrderID')
second call decodes.
select database name.dbo.fnEncDecRc4('encryption key','OrderID')
I have used this to encrypt passwords in tables and to pass query parameters in the address bar of web pages.
Subscribe to:
Post Comments (Atom)
Multi Point USB Charger
USB Plug Charger, 4-Port USB Fast Charger Plug with 33W Intelligent Quick Charge 3.0 Wall Charger, Multi USBPower Adapter UK Fast Charging...
-
If DCOB Fault comes on your main phone this means that there is no external lines available and you will need to call you service pro...
-
Crystal Reports datepart is slightly different than SQL and VB Interval type Description yyyy Extr...
-
Today, emergencies happen often. Below is a list of emergency supplies that you should have available. This list is not made in stone. For...
No comments:
Post a Comment