1

Was trying to convert a code from VB to C#, see VB code below

Dim StrCount As Int16
Dim str1, str2, EncryptedStr As String
EncryptedStr = String.Empty
theString = "Test@1234"

For StrCount = 1 To Len(theString)
    str1 = Asc(Mid(theString, StrCount, 1)) - 1
    str2 = str1 + Asc(Mid(StrCount, 1))
    EncryptedStr = EncryptedStr + Chr(str2)
Next

Converted C# code

string EncryptedStr = string.Empty;
Encoding encode1 = Encoding.ASCII;
Byte[] encodedBytes = encode1.GetBytes("Test@1234");  

for (int count = 0; count < encodedBytes.Length; count++)
{
    int str1 = encodedBytes[count] - 1;
    Encoding encode2 = Encoding.ASCII;
    Byte[] encodedBytes1 = encode2.GetBytes((count + 1).ToString());  
    int str2 = str1 + (int)encodedBytes1[0];
    EncryptedStr += Convert.ToChar(str2);
}

Its working fine, but the problem i am facing is the encrypted password is different in VB & C#

I tried encrypting a string "Test@1234", the encrypted result was

VB: „–¥§tfhjl

C#: ¥§tfhjl

I debug and noticed that in C# Convert.ToChar(132) & Convert.ToChar(150) is giving empty value.

Can anybody explain, what is going wrong here

Konamiman
  • 49,681
  • 17
  • 108
  • 138
N Khan
  • 366
  • 1
  • 6
  • 15

3 Answers3

1

The easiest solution and the one that will be identical to your original code is to use the Asc and Chr methods in the VisualBasic namespace as these have identical functionality.

But

Asc uses ANSI which can change on different locales and different machine, so if you really want to be stubborn, you can try and emulate this by explicitly defining the encoding to use.

This produces the same result on my machine (but as be careful to test it on other machines):

Public Function EncryptString(aString As String) As String
    Dim sb As New StringBuilder
    Dim enc = Encoding.GetEncoding("windows-1252")
    For i = 0 To aString.Length - 1
        Dim x = enc.GetBytes(aString.Substring(i, 1))(0) - 1
        Dim y = enc.GetBytes((i + 1).ToString)(0) + x
        Dim b() As Byte = {y}
        sb.Append(enc.GetString(b))
    Next
    Return sb.ToString
End Function

So the (untested) C# equivalent is:

public string EncryptString(string aString)
{
    StringBuilder sb = new StringBuilder();
    var enc = Encoding.GetEncoding("windows-1252");
    for (var i = 0; i < aString.Length; i++)
    {
        var x = enc.GetBytes(aString.Substring(i, 1))[0] - 1;
        var y = enc.GetBytes((i + 1).ToString())[0] + x;
        byte[] b = {y};
        sb.Append(enc.GetString(b));
    }
    return sb.ToString();
}
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • i will try and let you know – N Khan Oct 22 '15 at 08:17
  • 1
    I have made a change to specify the encoding explicitly, so this should match `Asc` more closely – Matt Wilko Oct 23 '15 at 09:33
  • 1) Thanks 2) Cannot implicitly convert int to byte so modified line byte[] b = BitConverter.GetBytes(y); 3) Result i am getting is double quote " I did exactly as you said... – N Khan Oct 25 '15 at 06:44
1

As explained in the comment for this answer here, VB.NET returns ANSI codes in the current windows code page, not ASCII codes. Don't expect to get the same output unless you use the same function, reference Microsoft.VisualBasic and use Strings.Asc and Strings.Chr to get the same result.

Community
  • 1
  • 1
0

Vb.net legacy Asc function, really is using System.Text.Encoding.Default. In your C# version, you are using ASCII. Check that:

        string EncryptedStr = string.Empty;
        Encoding encode1 = Encoding.Default; //.ASCII;
        Byte[] encodedBytes = encode1.GetBytes("Test@1234");

        for (int count = 0; count < encodedBytes.Length; count++)
        {
            int str1 = encodedBytes[count] - 1;
            Encoding encode2 = Encoding.Default;
            Byte[] encodedBytes1 = encode2.GetBytes((count + 1).ToString());
            int str2 = str1 + (int)encodedBytes1[0];
            EncryptedStr += Convert.ToChar(str2);
        }
Morcilla de Arroz
  • 2,104
  • 22
  • 29
  • 1
    Not strictly True. Asc uses System.Text.SBCSCodePageEncoding, but In mono, on Ubuntu 12.04, System.Text.Encoding.Default is System.Text.Encoding.UTF8, while on Windows 7, it seems to be System.Text.SBCSCodePageEncoding. – Matt Wilko Oct 22 '15 at 08:03
  • 1
    @MattWilko Ok, but I'm not saying that, I say that he is using Default encoding (whichever) in Vb.net, and forcing to ASCII on C#. – Morcilla de Arroz Oct 22 '15 at 08:07
  • @NKhan -1 with no more info about "it's not working" it's not a good incentive to improve an answer – Morcilla de Arroz Oct 22 '15 at 08:23
  • 2
    But `Encoding.Default` is not necessarily the same as Asc (which uses ANSI) because the Default can change on different locales and machines – Matt Wilko Oct 22 '15 at 09:29
  • 1
    @MattWilko aaah I understand you now. Agree then, thanks for explanation – Morcilla de Arroz Oct 22 '15 at 09:41