Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on new decode function for password recovery tool. I tried code in c++ and worked (i did not write this function):

C#
void poco_pwd(u_char *pwd, int type) {
    int     len,
            tmp;
    u_char  *out;
    short   azz;

    if(type) azz = 0x2537;  // encrypt message
        else azz = 0x2a9a;  // other passwords

    len = strlen(pwd) >> 1;
    out = pwd;
    while(len--) {
        sscanf(pwd, "%02X", &tmp);
        pwd += 2;
        *out++ = tmp ^ (azz >> 8);
        azz = ((tmp + azz) * 0x8141) + 0x3171;
    }
    *out = 0;
}



I tried to convert this code to vb.net and c# but it throws arithmetic overflow operation. This function new value is put to "azz" variable. "azz" is a short variable but this these values are very long (((tmp + azz) * 0x8141) + 0x3171). Strange is that it works in c++.

What I have tried:

I converted this code to vb.net:

VB.NET
Dim encpass As String = "1EF66D8BD3C32476CEC8CF" 'Hex Value
Dim encpassByte As Byte() = Encoding.UTF8.GetBytes(HexToString(encpass))
Dim azz As Integer = &H2A9A
Dim len As Integer = encpassByte.Length >> 1
Dim storage(len) As Char

For i = 0 To len
    storage(i) = (ChrW(encpassByte(i) Xor (azz >> 8)))
    azz = ((encpassByte(i) + azz) * &H8141) + &H3171 'Error: arithmetic operation resulted in an overflow.
Next

Console.WriteLine(storage.ToString)
Console.ReadKey()


Hex to string function

VB.NET
Function HexToString(ByVal hex As String) As String
    Dim text As New System.Text.StringBuilder(hex.Length \ 2)
    For i As Integer = 0 To hex.Length - 2 Step 2
        text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
    Next
    Return text.ToString
End Function
Posted
Updated 26-Oct-16 8:30am
v3

1 solution

An Integer has 32 bits. If the number gets bigger than that, you may get that ArithmeticOverflow exception. Old C++ didn't care: it just threw away the value of the 33rd byte and beyond. That means you need here some way to simulate that dirty behavior of C++. With C#, you can use the unchecked keyword, but there seems to be no equivalent in VB.Net.
That said, I don't see a solution with VB.Net's standard functionality. I'd look for a library with a 64bit integer type, doing the calculation with a temporary variable of that type, then do a bitwise and to get it back to 32bits.
 
Share this answer
 

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