Mike Morehead Mike Morehead - 4 years ago 124
Vb.net Question

Convert this function from VB to c#

I've been banging my head against the wall trying to convert this classic asp (vb) to asp.net c# with no luck.

Function Decrypt1(s)
if isnull(s) then
Decrypt1 = ""
else
Dim r, i, ch
For i = 1 To Len(s)/2
ch = "&H" & Mid(s, (i-1)*2+1, 2)
ch = ch Xor 111
r = r & Chr(ch)
Next
Decrypt1 = strReverse(r)
end if
End Function


any takers?

Thanks in advance!

EDIT - "0B031D00180003030A07" should decrypt to "helloworld"

Answer Source

Updated

Here's your c-sharp method for decrypting your string:

    public static string Decrypt1(string s)
    {
        string functionReturnValue = null;
        if (string.IsNullOrEmpty(s))
        {
            functionReturnValue = "";
        }
        else
        {
            string r = null;
            int ch = null;

            for (int i = 0; i < s.Length / 2; i++)
            {
                ch = int.Parse(s.Substring((i) * 2, 2), NumberStyles.AllowHexSpecifier);
                ch = ch ^ 111;
                r = r + (char)(ch);
            }

            var charArray = r.ToCharArray();
            Array.Reverse(charArray);
            functionReturnValue = new string(charArray);
        }
        return functionReturnValue;
    }

Try it on Net Fiddle

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download