Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I converted the following cryptcode from VB to C#. The Problem I am facing is that, if input s is ABCD,then in VB, it is getting converted as
æf~m

‡

and in C# it is converted as
æf~m

?


If input s is 15 characters, then it is converting properly. Here is the VB code:-
Private Function cryptEntry(s$) As String

    Dim k$, r$, i%

    k = "§$=)%;:-:*(""§%""§=("             

    r = Left$(s + String(15, Chr$(32)), 15)           

    For i = 1 To Len(r)

        Mid$(r, i, 1) = Chr$(Asc(Mid$(r, i, 1)) Xor Asc(Mid$(k, i, 1)))

    Next i

    cryptEntry = r

End Function


What I have tried:

My code in C# :-

private string cryptEntry(string s)
        {
            string k = "", r = "";
            try
            {
                k = "§$=)%;:-:*(\"§%\"§=(";
                string insideString = "", concatString = "", leftString = "";
                insideString = new String(' ', 15);
                concatString = String.Concat(s, insideString);
                leftString = concatString.Substring(0, 15);
                r = leftString;
                StringBuilder sb = new StringBuilder(r);
                for (int i = 0; i < r.Length; i++)
                {
                    string rmid = r.Substring(i, 1);
                    string kmid = k.Substring(i, 1);
                    sb[i] = Convert.ToChar((Convert.ToInt32(Convert.ToChar(rmid)) ^ Convert.ToInt32(Convert.ToChar(kmid))));
                }
                r = sb.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return r;

        }
Posted
Updated 24-Jun-19 9:04am
Comments
Maciej Los 23-Jun-19 6:44am    
This is very well formed question!
Priya Karthish 23-Jun-19 7:21am    
Thank u

The problem is that VB's Asc and Chr functions do not have a direct equivalent in C#. They work fine when you're dealing with characters whose ANSI code is less than 128. Beyond that, they will diverge from the Unicode values that C# deals with.

In your example, the 13th character of the output from your VB code will be 8225 (). The same character from the C# code will be 135*. It's visually the same character[^], but not the same codepoint.

If you can change your VB code to use AscW and ChrW, then your C# output will match.

Otherwise, you'll need to add a reference to the Microsoft.VisualBasic assembly, and use the VB functions:
C#
private string cryptEntry(string s)
{
    const string k = "§$=)%;:-:*(\"§%\"§=(";
    string r = s.PadRight(15, ' ').Substring(0, 15);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < r.Length; i++)
    {
        sb.Append(Strings.Chr(Strings.Asc(r[i]) ^ Strings.Asc(k[i])));
    }
    return sb.ToString();
}
Strings.Asc Method (Microsoft.VisualBasic) | Microsoft Docs[^]
Strings.Chr(Int32) Method (Microsoft.VisualBasic) | Microsoft Docs[^]

* NB: The output of Asc and Chr will vary depending on the code-page of the computer on which your code runs. If there's any possibility of your code running on computers with different locales, then you can't guarantee that the output will match.
 
Share this answer
 
v3
Comments
Maciej Los 25-Jun-19 15:37pm    
The part after "* NB" is the most important.
5ed!
Priya Karthish 25-Jun-19 16:22pm    
Thank you
Well...
I have changed cryptEntry method to this form:
C#
private string cryptEntry(string s)
{
    char[] kchars = "§$=)%;:-:*(\"§%\"§=(".ToArray();
	char[] rchars = s.PadRight(15, ' ').Substring(0,15).ToArray();
	
    for (int i = 0; i < rchars.Length; i++)
        rchars[i] = (char)((int)rchars[i] ^ (int)kchars[i]);

	return new string(rchars);
}

and tested it this way:
C#
string input  = "ABCD";
string output  = cryptEntry(input);
char[] o = output.ToArray();
for (int i = 0; i < o.Length; i++)
    Console.WriteLine("{0} {1}", o[i], (int)o[i]);


This produces the same set of chars as vb function:
æ 230
f 102
~ 126
m 109
05 5
1B 27
1A 26
13
1A 26

 10
08 8
02 2
‡ 135
05 5
02 2
 
Share this answer
 
v3
Comments
[no name] 23-Jun-19 11:14am    
Good one, 5. But this s+ new String((char)32, 15)).Substring(0,15) can be written much more easy: s.PadRight(15, ' ') :)
Maciej Los 23-Jun-19 11:15am    
My virtual 5, Bruno!
BTW: i focused to much on VB function ;)
[no name] 23-Jun-19 11:20am    
Thanks :). I was not completely correct. To be on the safe side it should be more s.PadRight(15, ' ').Substring(0,15). This in case s is allready longer than 15 chars.
Maciej Los 23-Jun-19 11:55am    
Good point!
Priya Karthish 23-Jun-19 16:57pm    
Thanks for your effort. I debugged my code and your code too. The XOR value is same in both the cases. The encrypted code is actually saved in the database as a varchar datatype. That's where the mismatch actually happened.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900