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:
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.