Re: Total confused and need help with small encryption and decryption methods
- From: "DeveloperX" <nntpDev@xxxxxxxxxxxxx>
- Date: 3 Apr 2007 11:55:52 -0700
On 3 Apr, 18:18, manmit.wa...@xxxxxxxxx wrote:
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[i] = 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[i] = (char)(binary[i] ^ 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[i]) ^ ((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
.
- Follow-Ups:
- Re: Total confused and need help with small encryption and decryption methods
- From: manmit . walia
- Re: Total confused and need help with small encryption and decryption methods
- References:
- Total confused and need help with small encryption and decryption methods
- From: manmit . walia
- Total confused and need help with small encryption and decryption methods
- Prev by Date: Can anyone suggest a PDF API?
- Next by Date: Re: multiple threads writing to WebBrowser, getting deadlocked
- Previous by thread: Total confused and need help with small encryption and decryption methods
- Next by thread: Re: Total confused and need help with small encryption and decryption methods
- Index(es):
Relevant Pages
|