Vb.net算术运算导致解码溢出 [英] Vb.net arithmetic operation resulted in an overflow in decoding

查看:497
本文介绍了Vb.net算术运算导致解码溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为密码恢复工具开发新的解码功能。我尝试用c ++编写代码并且工作(我没有写这个函数):



  void  poco_pwd(u_char * pwd, int  type){
int len,
tmp;
u_char * out;
简短 azz;

if (type)azz = 0x2537; // 加密邮件
else azz = 0x2a9a; // 其他密码

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 ;
}







我试图将此代码转换为vb.net和c#但它抛出算术溢出操作。此函数的新值被赋予azz变量。 azz是一个短变量,但这些值非常长(((tmp + azz)* 0x8141)+ 0x3171)。奇怪的是它适用于c ++。



我尝试过的事情:



我将此代码转换为vb.net:



  Dim  encpass 作为 字符串 =  < span class =code-string> 1EF66D8BD3C32476CEC8CF '  Hex值 
Dim encpassByte 作为 字节()=编码.UTF8.GetBytes(HexToString(encpass))
Dim azz As 整数 =& H2A9A
Dim len As < span class =code-keyword>整数 = encpassByte.Length>> 1
Dim storage(len) As 字符

对于 i = 0 len
storage(i)=(ChrW(encpassByte(i) Xor (azz>> 8 )))
azz =((encpassByte(i)+ azz)*& H8141)+ & H3171 ' 错误:算术运算导致溢出。
下一步

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





十六进制到字符串函数



 函数 HexToString( ByVal  hex  As   String 作为 字符串 
Dim 文字 As System.Text.StringBuilder(hex.Length \ 2
对于 i 作为 整数 = 0 hex.Length - 2 步骤 2
text.Append(Chr(Convert.ToByte) (hex.Substring(i, 2 ), 16 )))
下一步
返回 text.ToString
结束 功能

解决方案

整数有32位。如果数字大于那个,你可能会得到ArithmeticOverflow异常。旧的C ++并不关心:它只是丢掉了第33个字节及以上的值。这意味着你需要在某种程度上模拟C ++的脏行为。使用C#,您可以使用未选中关键字,但VB.Net中似乎没有等效项。

也就是说,我没有看到VB.Net标准功能的解决方案。我会寻找一个64位整数类型的库,使用该类型的临时变量进行计算,然后按按位和将其恢复为32位。 / BLOCKQUOTE>

I am working on new decode function for password recovery tool. I tried code in c++ and worked (i did not write this function):

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:

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

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

解决方案

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.


这篇关于Vb.net算术运算导致解码溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆