Re: how these 2 functions may differ?
- From: "James" <noone@xxxxxxxxxxx>
- Date: Wed, 7 Jan 2009 14:30:34 -0500
Hello Ben,
thanks for the reply. The original encryption and base64 encoding is done from a Windows Script Host vbscript. The c# end of this is a web app that must decode/decrypt this data.
I attached vbscript code that does the encryption/encoding if you want to see. Also, a copy/paste of a c# translation of this, which is what I'm using on the web app to try to decode/decrypt is below....
public class RC4Utility
{
// This is a translation of Mike Shaffer's vbscript code for RC4 done by Chris Scott.
// From 4guysfromrolla.com
protected int[] sbox = new int[256];
protected int[] key = new int[256];
protected string plaintext, password;
public string PlainText
{
set { plaintext = value; }
get { return plaintext; }
}
public string Password
{
set { password = value; }
get { return password; }
}
private void RC4Initialize(string strPwd)
{
int intLength = strPwd.Length;
for (int a = 0; a <= 255; a++)
{
char ctmp = (strPwd.Substring((a % intLength), 1).ToCharArray()[0]);
key[a] = Microsoft.VisualBasic.Strings.Asc(ctmp);
sbox[a] = a;
}
int x = 0;
for (int b = 0; b <= 255; b++)
{
x = (x + sbox[b] + key[b]) % 256;
int tempSwap = sbox[b];
sbox[b] = sbox[x];
sbox[x] = tempSwap;
}
}
public string EnDeCrypt()
{
int i = 0;
int j = 0;
string cipher = "";
RC4Initialize(password);
for (int a = 1; a <= plaintext.Length; a++)
{
int itmp = 0;
i = (i + 1) % 256;
j = (j + sbox[i]) % 256;
itmp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = itmp;
int k = sbox[(sbox[i] + sbox[j]) % 256];
char ctmp = plaintext.Substring(a - 1, 1).ToCharArray()[0];
itmp = Microsoft.VisualBasic.Strings.Asc(ctmp);
int cipherby = itmp ^ k;
cipher += Microsoft.VisualBasic.Strings.Chr(cipherby);
}
return cipher;
}
}
"Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx> wrote in message news:uS4I8pPcJHA.4412@xxxxxxxxxxxxxxxxxxxxxxx
James wrote:'--------------------------------------------------------------------yes, I'm pretty sure they are. I am sure that the text representation
of them on screen (displayed in text box control) is identical.
I see the vbscript code formatting was lost in my post... I'll try to
attach in a .txt file (windows notepad.exe)
I'm reading through msdn docs now and I'm thinking this has something
to do with the fact that the rc4 cipher is using ascii characters
0-255 and the .net asciiEncoding class only uses 0-127.... maybe I
need to add a step where I change the encrypted text to unicode, then
base64 it?
Encrypted text is not text. It is binary data. I suggest you stop using a string to hold it. Doesn't the encryption routine give you a byte[] out? And the Base-64 encoding functions take a byte[] as input. No Unicode needed, no encoding needed, just pass the byte[].
"Family Tree Mike" <FamilyTreeMike@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:474F2B84-167E-4113-B368-89AD0C327D1A@xxxxxxxxxxxxxxxx
"James" wrote:
Hello,
I'm having an issue that a previously posted on but after
re-reading that post I realize its a mess and it would be a real
pain for someone to lend a
hand based on that.. so here is a much more direct question:
could anyone tell just how these .net base64 functions differ from
the vbscript base64 functions? When using them if I encode the text
'test' I get
the same result from them all, however, if I first RC4 encrypt the
text 'test' and then encode that, the results differ between the
vbscript and ..net functions?
Are the encrypted strings in both cases identical?
Mike
'start: Mike Shaffer's RC4 functions --------------------------------
'--------------------------------------------------------------------
Dim sbox(255)
Dim key(255)
Sub RC4Initialize(strPwd)
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: This routine called by EnDeCrypt function. Initializes the :::
'::: sbox and the key array) :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
dim tempSwap
dim a
dim b
'below added to Mike Shaffer's code only to comply with Option Explicit
Dim intLength
intLength = len(strPwd)
For a = 0 To 255
key(a) = asc(mid(strpwd, (a mod intLength)+1, 1))
sbox(a) = a
next
b = 0
For a = 0 To 255
b = (b + sbox(a) + key(a)) Mod 256
tempSwap = sbox(a)
sbox(a) = sbox(b)
sbox(b) = tempSwap
Next
End Sub
Function EnDeCrypt(plaintxt, psw)
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: This routine does all the work. Call it both to ENcrypt :::
'::: and to DEcrypt your data. :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
dim temp
dim a
dim i
dim j
dim k
dim cipherby
dim cipher
i = 0
j = 0
RC4Initialize psw
For a = 1 To Len(plaintxt)
i = (i + 1) Mod 256
j = (j + sbox(i)) Mod 256
temp = sbox(i)
sbox(i) = sbox(j)
sbox(j) = temp
k = sbox((sbox(i) + sbox(j)) Mod 256)
cipherby = Asc(Mid(plaintxt, a, 1)) Xor k
cipher = cipher & Chr(cipherby)
Next
EnDeCrypt = cipher
End Function
'----------------------------------------------------------------------------
'end: Mike Shaffer's RC4 code -----------------------------------------------
'----------------------------------------------------------------------------
- Follow-Ups:
- Re: how these 2 functions may differ?
- From: Ben Voigt [C++ MVP]
- Re: how these 2 functions may differ?
- References:
- how these 2 functions may differ?
- From: James
- RE: how these 2 functions may differ?
- From: Family Tree Mike
- Re: how these 2 functions may differ?
- From: James
- Re: how these 2 functions may differ?
- From: Ben Voigt [C++ MVP]
- how these 2 functions may differ?
- Prev by Date: Seeking an Experienced Systems Analyst
- Next by Date: Re: how these 2 functions may differ?
- Previous by thread: Re: how these 2 functions may differ?
- Next by thread: Re: how these 2 functions may differ?
- Index(es):
Relevant Pages
|