Total confused and need help with small encryption and decryption methods

M

manmit.walia

Hello Everyone,
Long time ago, I posted a small problem I had about converting a VB6
program to C#. Well with the help with everyone I got it converted.
But I overlooked something and don't understand why it is doing this.
Below is my code, I would be greatfull if someone can guide me through
the right path or even help me solve this issue.

Problem: The old tool which was written in VB6 works perfect. But I
needed to convert this to C# using since there are new tools we are
creating that would like to leverage this style of security. The
problem is that the encryption method is not working as it should. The
decryption method works perfect. Just having problems with the
encryption part.

Test Values:
Current Encrypted Value - 0835262B27
Value returned using Decrypt method - anna

Current Decrypted Value - anna
Value returned using Encrypted method - 35262B27

*** As you can see the encryption method is returning something
different then the original encrypted value. *** PLEASE HELP !!! ***

CODE:

static byte[] ParseHex(string text)
{
byte[] ret = new byte[text.Length / 2];
for (int i = 0; i < ret.Length; i++)
{
ret = Convert.ToByte(text.Substring(i * 2, 2), 16);
}
return ret;
}

public static string Decrypt("THEFELDGROUP", string encrypted)
{
byte[] binary = ParseHex(encrypted);
char[] chars = new char[binary.Length];
for (int i = 0; i < chars.Length; i++)
{
chars = (char)(binary ^ password[i % password.Length]);
}
return new string(chars);
}


public static string Encrypt("THEFELDGROUP", string
strG)
{
char[] cHexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};

// the encoded integer value of the current character in the string
byte byteEncoded;

System.Text.StringBuilder sbReturned = new
System.Text.StringBuilder();

for (int i = 0; i < strG.Length; ++i)
{
// encode the current character
byteEncoded = (byte)(((int)strG) ^ ((int)strPass[i %
strPass.Length]));

// output the Hex character value of the above encoded value
sbReturned.Append(cHexDigits[byteEncoded >>
4]).Append(cHexDigits[byteEncoded & 0xF]);
}

return sbReturned.ToString();
}
 
D

DeveloperX

Hello Everyone,
Long time ago, I posted a small problem I had about converting a VB6
program to C#. Well with the help with everyone I got it converted.
But I overlooked something and don't understand why it is doing this.
Below is my code, I would be greatfull if someone can guide me through
the right path or even help me solve this issue.

Problem: The old tool which was written in VB6 works perfect. But I
needed to convert this to C# using since there are new tools we are
creating that would like to leverage this style of security. The
problem is that the encryption method is not working as it should. The
decryption method works perfect. Just having problems with the
encryption part.

Test Values:
Current Encrypted Value - 0835262B27
Value returned using Decrypt method - anna

Current Decrypted Value - anna
Value returned using Encrypted method - 35262B27

*** As you can see the encryption method is returning something
different then the original encrypted value. *** PLEASE HELP !!! ***

CODE:

static byte[] ParseHex(string text)
{
byte[] ret = new byte[text.Length / 2];
for (int i = 0; i < ret.Length; i++)
{
ret = Convert.ToByte(text.Substring(i * 2, 2), 16);
}
return ret;
}

public static string Decrypt("THEFELDGROUP", string encrypted)
{
byte[] binary = ParseHex(encrypted);
char[] chars = new char[binary.Length];
for (int i = 0; i < chars.Length; i++)
{
chars = (char)(binary ^ password[i % password.Length]);
}
return new string(chars);
}

public static string Encrypt("THEFELDGROUP", string
strG)
{
char[] cHexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};

// the encoded integer value of the current character in the string
byte byteEncoded;

System.Text.StringBuilder sbReturned = new
System.Text.StringBuilder();

for (int i = 0; i < strG.Length; ++i)
{
// encode the current character
byteEncoded = (byte)(((int)strG) ^ ((int)strPass[i %
strPass.Length]));

// output the Hex character value of the above encoded value
sbReturned.Append(cHexDigits[byteEncoded >>
4]).Append(cHexDigits[byteEncoded & 0xF]);
}

return sbReturned.ToString();
}



Hi, I've had to tweak it a bit, and it seems you've posted the
password used to encrypt decrypt, although I couldn't have got the
same results without it :) Interestingly I get the second version
regardless of whether I decrypt or encrypt, and decrypting 0835262B27
gives me the completely wrong result. I.e. not anna

Can you confirm that you actually want 0835262B27 and not 35262B27?
The second seems correct to me as it's 4 bytes which I would expect as
anna is four chars long.

Now if the 08 is required that would convert to ascii bell(?) That's
assuming the text is encoded as ascii, I've not used much of the
encoding stuff as I have a small world view, so can't comment on
whether that would cause you issues or any other problems for that
matter.

Hope that helps
 
M

manmit.walia

Hello Everyone,
Long time ago, I posted a small problem I had about converting a VB6
program to C#. Well with the help with everyone I got it converted.
But I overlooked something and don't understand why it is doing this.
Below is my code, I would be greatfull if someone can guide me through
the right path or even help me solve this issue.
Problem: The old tool which was written in VB6 works perfect. But I
needed to convert this to C# using since there are new tools we are
creating that would like to leverage this style of security. The
problem is that the encryption method is not working as it should. The
decryption method works perfect. Just having problems with the
encryption part.
Test Values:
Current Encrypted Value - 0835262B27
Value returned using Decrypt method - anna
Current Decrypted Value - anna
Value returned using Encrypted method - 35262B27
*** As you can see the encryption method is returning something
different then the original encrypted value. *** PLEASE HELP !!! ***

static byte[] ParseHex(string text)
{
byte[] ret = new byte[text.Length / 2];
for (int i = 0; i < ret.Length; i++)
{
ret = Convert.ToByte(text.Substring(i * 2, 2), 16);
}
return ret;
}

public static string Decrypt("THEFELDGROUP", string encrypted)
{
byte[] binary = ParseHex(encrypted);
char[] chars = new char[binary.Length];
for (int i = 0; i < chars.Length; i++)
{
chars = (char)(binary ^ password[i % password.Length]);
}
return new string(chars);
}

public static string Encrypt("THEFELDGROUP", string
strG)
{
char[] cHexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
// the encoded integer value of the current character in the string
byte byteEncoded;
System.Text.StringBuilder sbReturned = new
System.Text.StringBuilder();
for (int i = 0; i < strG.Length; ++i)
{
// encode the current character
byteEncoded = (byte)(((int)strG) ^ ((int)strPass[i %
strPass.Length]));

// output the Hex character value of the above encoded value
sbReturned.Append(cHexDigits[byteEncoded >>
4]).Append(cHexDigits[byteEncoded & 0xF]);
}
return sbReturned.ToString();
}

Hi, I've had to tweak it a bit, and it seems you've posted the
password used to encrypt decrypt, although I couldn't have got the
same results without it :) Interestingly I get the second version
regardless of whether I decrypt or encrypt, and decrypting 0835262B27
gives me the completely wrong result. I.e. not anna

Can you confirm that you actually want 0835262B27 and not 35262B27?
The second seems correct to me as it's 4 bytes which I would expect as
anna is four chars long.

Now if the 08 is required that would convert to ascii bell(?) That's
assuming the text is encoded as ascii, I've not used much of the
encoding stuff as I have a small world view, so can't comment on
whether that would cause you issues or any other problems for that
matter.

Hope that helps- Hide quoted text -

- Show quoted text -


Hello,
I am actually looking for the outcome to be 0835262B27 but I am
getting 35262B27. So you are right, that it is missing '08' but I
don't know why it is missing that variable.

Thanks
 
R

rossum

Hello Everyone,
Long time ago, I posted a small problem I had about converting a VB6
program to C#. Well with the help with everyone I got it converted.
But I overlooked something and don't understand why it is doing this.
Below is my code, I would be greatfull if someone can guide me through
the right path or even help me solve this issue.

Problem: The old tool which was written in VB6 works perfect. But I
needed to convert this to C# using since there are new tools we are
creating that would like to leverage this style of security. The
problem is that the encryption method is not working as it should. The
decryption method works perfect. Just having problems with the
encryption part.

Test Values:
Current Encrypted Value - 0835262B27
Value returned using Decrypt method - anna

Current Decrypted Value - anna
Value returned using Encrypted method - 35262B27

*** As you can see the encryption method is returning something
different then the original encrypted value. *** PLEASE HELP !!! ***

CODE:

static byte[] ParseHex(string text)
{
byte[] ret = new byte[text.Length / 2];
for (int i = 0; i < ret.Length; i++)
{
ret = Convert.ToByte(text.Substring(i * 2, 2), 16);
}
return ret;
}

public static string Decrypt("THEFELDGROUP", string encrypted)

This line does not compile. You probably should have written
something like:
public static string Decrypt(string password, string encrypted)

{
byte[] binary = ParseHex(encrypted);
char[] chars = new char[binary.Length];
for (int i = 0; i < chars.Length; i++)
{
chars = (char)(binary ^ password[i % password.Length]);
}
return new string(chars);
}

public static string Encrypt("THEFELDGROUP", string strG)
Again this line does not compile:
public static string Encrypt(string strPass, string strG)
{
char[] cHexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};

// the encoded integer value of the current character in the string
byte byteEncoded;

System.Text.StringBuilder sbReturned = new
System.Text.StringBuilder();

for (int i = 0; i < strG.Length; ++i)
{
// encode the current character
byteEncoded = (byte)(((int)strG) ^ ((int)strPass[i %
strPass.Length]));

// output the Hex character value of the above encoded value
sbReturned.Append(cHexDigits[byteEncoded >>
4]).Append(cHexDigits[byteEncoded & 0xF]);
}

return sbReturned.ToString();
}

Your code does not have the "08" because you are encoding a four
character plaintext into a four byte (= 8 hex digits) cyphertext. I
suggest you have a close look at the VB original to see where the
initial "08" comes from, then you need to reproduce that in your C#
version. We do not have enough information here to see its origin.

Your encryption scheme, a stream cypher with a repeating key, is not
very secure. Good enough to deter casual observation but easily
breakable by anyone seriously interested. Repeating any part of the
keystream is a big weakness for any stream cypher.

rossum
 
D

DeveloperX

Hello Everyone,
Long time ago, I posted a small problem I had about converting a VB6
program to C#. Well with the help with everyone I got it converted.
But I overlooked something and don't understand why it is doing this.
Below is my code, I would be greatfull if someone can guide me through
the right path or even help me solve this issue.
Problem: The old tool which was written in VB6 works perfect. But I
needed to convert this to C# using since there are new tools we are
creating that would like to leverage this style of security. The
problem is that the encryption method is not working as it should. The
decryption method works perfect. Just having problems with the
encryption part.
Test Values:
Current Encrypted Value - 0835262B27
Value returned using Decrypt method - anna
Current Decrypted Value - anna
Value returned using Encrypted method - 35262B27
*** As you can see the encryption method is returning something
different then the original encrypted value. *** PLEASE HELP !!! ***

static byte[] ParseHex(string text)
{
byte[] ret = new byte[text.Length / 2];
for (int i = 0; i < ret.Length; i++)
{
ret = Convert.ToByte(text.Substring(i * 2, 2), 16);
}
return ret;
}

public static string Decrypt("THEFELDGROUP", string encrypted)

This line does not compile. You probably should have written
something like:
public static string Decrypt(string password, string encrypted)
{
byte[] binary = ParseHex(encrypted);
char[] chars = new char[binary.Length];
for (int i = 0; i < chars.Length; i++)
{
chars = (char)(binary ^ password[i % password.Length]);
}
return new string(chars);
}

public static string Encrypt("THEFELDGROUP", string strG)

Again this line does not compile:
public static string Encrypt(string strPass, string strG)


{
char[] cHexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
// the encoded integer value of the current character in the string
byte byteEncoded;
System.Text.StringBuilder sbReturned = new
System.Text.StringBuilder();
for (int i = 0; i < strG.Length; ++i)
{
// encode the current character
byteEncoded = (byte)(((int)strG) ^ ((int)strPass[i %
strPass.Length]));

// output the Hex character value of the above encoded value
sbReturned.Append(cHexDigits[byteEncoded >>
4]).Append(cHexDigits[byteEncoded & 0xF]);
}
return sbReturned.ToString();
}

Your code does not have the "08" because you are encoding a four
character plaintext into a four byte (= 8 hex digits) cyphertext. I
suggest you have a close look at the VB original to see where the
initial "08" comes from, then you need to reproduce that in your C#
version. We do not have enough information here to see its origin.

Your encryption scheme, a stream cypher with a repeating key, is not
very secure. Good enough to deter casual observation but easily
breakable by anyone seriously interested. Repeating any part of the
keystream is a big weakness for any stream cypher.

rossum


This is the version I tweaked. Also be aware there were some broken
lines which I imagine will reappear. Manmit, can you post a link to
the original code or the original VB6 code? I still reckon it's
working as intended, the 08 isn't a bell, I misread the original
code. Can you try with your working code (the vb6 one I guess) and
see if it is always 08? You may have a bug in the VB6 that just
happens to have been obfuscated because it's consistent between
encrypt and decrypt.. Oops, just read the end of Rossum's post and
he's said everything I was about to :)
If you need backwards compatability with existing data, I'd consider
decrypting with your algorithm and then re-encrypting it with
something stronger. The enterprise library has a good encryption
block. You could knock up a batch decrypter in VB6 with the working
code, then a batch re-encrypter in dotnet. Back up your data first ;)


class Program
{
static void Main(string[] args)
{
Console.WriteLine(Encrypt("anna"));
Console.WriteLine(Decrypt("35262B27"));
//SocketTest t = new SocketTest();
//t.Connect("www.wu2.co.uk", 80);
}
static byte[] ParseHex(string text)
{
byte[] ret = new byte[text.Length / 2];
for (int i = 0; i < ret.Length; i++)
{
ret = Convert.ToByte(text.Substring(i * 2,
2), 16);
}
return ret;
}

public static string Decrypt(string encrypted)
{
string password = "THEFELDGROUP";
byte[] binary = ParseHex(encrypted);
char[] chars = new char[binary.Length];
for (int i = 0; i < chars.Length; i++)
{
chars = (char)(binary ^ password[i %
password.Length]);
}
return new string(chars);
}

public static string Encrypt(string strG)
{
string strPass = "THEFELDGROUP";
char[] cHexDigits = {
'0', '1', '2', '3', '4', '5', '6',
'7',
'8', '9', 'A', 'B', 'C', 'D', 'E',
'F'
};

// the encoded integer value of the current character in
the string
byte byteEncoded;

System.Text.StringBuilder sbReturned = new
System.Text.StringBuilder();

for (int i = 0; i < strG.Length; ++i)
{
// encode the current character
byteEncoded = (byte)(((int)strG) ^
((int)strPass[i % strPass.Length]));

// output the Hex character value of the above
encoded value
sbReturned.Append(cHexDigits[byteEncoded >>
4]).Append(cHexDigits[byteEncoded & 0xF]);
}

return sbReturned.ToString();
}
}
 
M

manmit.walia

I agree. It's better to use more secure methods such as
System.Security.Cryptography. There is little space here to explain in
detail, but I can give you an example if you want.

Freiddiehttp://fei.yuanbw.googlepages.com/http://freiddy.blogspot.com/http://crazibe.blogspot.com/

I would have used the Cryptography class in .NET but as I said the
current cryptology that is being used was written in VB6 or so. And I
can not change the way the text is encrypted or decrypted becuase
there are applications written using the exisiting code. All I am
charge to do is convert it to an C# dll file so that future apps can
use the .NET version rather then the vb version.

Here is the original VB. version

Attribute VB_Name = "Module1"
Option Explicit


Dim pass$
Dim Strg$


Function decrypt(ByVal H$) As String


Dim i As Integer, J$


pass$ = "THEFELDGROUP"


'H$ = the buffered encrypted data


H$ = Mid$(H$, 3, Val(Left$(H$, 2)))


' debuffer it


Strg$ = ""
For i = 1 To Len(H$) Step 2
J$ = Mid$(H$, i, 2)
Strg$ = Strg$ + Chr$(Val("&H" + J$))
Next


'Strg$ now contains the encrypted string, which you can now
'decrypt.


Call Crypt(pass$, Strg$)


'strg$ now is decrypted


decrypt = Strg$


End Function


Function Crypt(pass$, Strg$)
Dim a, b
Dim i As Integer


a = 1
For i = 1 To Len(Strg$)
b = Asc(Mid$(pass$, a, 1)): a = a + 1: If a > Len(pass$) Then a
=
1
Mid$(Strg$, i, 1) = Chr$(Asc(Mid$(Strg$, i, 1)) Xor b)
Next


End Function
 
R

rossum

I would have used the Cryptography class in .NET but as I said the
current cryptology that is being used was written in VB6 or so. And I
can not change the way the text is encrypted or decrypted becuase
there are applications written using the exisiting code. All I am
charge to do is convert it to an C# dll file so that future apps can
use the .NET version rather then the vb version.

Here is the original VB. version

Attribute VB_Name = "Module1"
Option Explicit


Dim pass$
Dim Strg$


Function decrypt(ByVal H$) As String
Dim i As Integer, J$
pass$ = "THEFELDGROUP"
If you were using a stronger encryption, holding the key in plaintext
would be a weakness; but with your encryption scheme it does not make
the attacker's job that much easier.
' H$ = the buffered encrypted data
H$ = Mid$(H$, 3, Val(Left$(H$, 2)))
This is where your problem is. It is chopping off the first two
characters of the H$ parameter string, "Left$(H$, 2)", your extra
"08", and using them "Val(Left$(H$, 2))" to determine the length of
text to extract and decode: H$ = Mid$(H$, 3, Val("08"))
The first two characters appear to represent the length in decimal of
the following cyphertext.

You need to amend your C# Decrypt function to account for these two
characters:
Parse the first two characters as a decimal number.
Truncate the remaining cyphertext to that length
Decypher the truncated cyphertext

You also need to add these two characters to the start of any
encrypted string after you have converted it to Hex digits. Best to
check the VB original to see if there are any other things done that I
cannot determine here. For instance, what happens if the cyphertext
is more than 99 hex digits long?

rossum
 
M

manmit.walia

If you were using a stronger encryption, holding the key in plaintext
would be a weakness; but with your encryption scheme it does not make
the attacker's job that much easier.




This is where your problem is. It is chopping off the first two
characters of the H$ parameter string, "Left$(H$, 2)", your extra
"08", and using them "Val(Left$(H$, 2))" to determine the length of
text to extract and decode: H$ = Mid$(H$, 3, Val("08"))
The first two characters appear to represent the length in decimal of
the following cyphertext.

You need to amend your C# Decrypt function to account for these two
characters:
Parse the first two characters as a decimal number.
Truncate the remaining cyphertext to that length
Decypher the truncated cyphertext

You also need to add these two characters to the start of any
encrypted string after you have converted it to Hex digits. Best to
check the VB original to see if there are any other things done that I
cannot determine here. For instance, what happens if the cyphertext
is more than 99 hex digits long?

rossum










- Show quoted text -- Hide quoted text -

- Show quoted text -

Thanks Rossum, I think I got the idea. Basically the first two
characters that are missing is a numerical value of the length of the
encrypted string. This should work. Let me try it and will keep you
updated.
Thanks again for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top