Click here to Skip to main content
14,329,627 members
Rate this:
Please Sign up or sign in to 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
Rate this:
Please Sign up or sign in to vote.

Solution 1

Well...
I have changed cryptEntry method to this form:
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:
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
   
v3
Comments
0x01AA 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 ;)
0x01AA 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.
Maciej Los 23-Jun-19 16:59pm
   
OK.
If your issue has been resolve, please, accept my answer.
Priya Karthish 24-Jun-19 5:18am
   
Sorry it is not resolved. While saving in the sql database, still the mistmatch is there. In my project encrypting is happening with c# and decrypting is done with a dll made in VB. After the encryption, instead of doubledagger(‡), questionmark(?) is there, I need a solution for that. Look at the Question above and you can see the difference.
Maciej Los 24-Jun-19 5:38am
   
As i mentioned, i tested c# and vb method. Both return the same set of chars. So, i don't know why you have 2 different results...
Richard Deeming 24-Jun-19 15:09pm
   
How did you test the VB method? Using Strings.Chr and Strings.Asc gives me 8225 as the 13th character, not 135.

If I change it to ChrW and AscW, I get the same result as the C# code.
Maciej Los 24-Jun-19 15:23pm
   
I have copied cryptEntry() function to VBA module then:
Sub Main()
    Dim s As String, o As String

    s = "ABCD"
    o = cryptEntry(s)
    
    For i = 1 To Len(o)
        Debug.Print Mid(o, i, 1), Asc(Mid(o, i, 1))
    Next

End Sub

Result: 135 on 13th character.
Richard Deeming 24-Jun-19 15:25pm
   
Maybe it's a code-page issue. Asc and Chr return different results depending on the current code page, whereas AscW and ChrW always use Unicode.
Maciej Los 24-Jun-19 15:33pm
   
You're probably right, Richard.
I'm using MS Office Polish version, where default code-page is Windows-1250 (or 1252). VBA code pane uses Curier New (Central Europe) font, so...
I'll try to use different ways and let you know.
Richard Deeming 24-Jun-19 15:38pm
   
1252 here.

Some possibly useful info in this Server Fault answer:
windows - How can I manually determine the CodePage and Locale of the current OS - Server Fault[^]
Maciej Los 24-Jun-19 15:54pm
   
Well, VBA code editor uses the same code page from Windows OS Regional Settings: MS Access VBA code editor character encoding and copy/paste - Stack Overflow[^]
[EDIT]
1250 here. Checked this way:
    Dim enc As Variant
    enc = Application.DefaultWebOptions.Encoding
    Debug.Print enc


DefaultWebOptions.Encoding property (Excel) | Microsoft Docs[^]
Rate this:
Please Sign up or sign in to vote.

Solution 2

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

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100