代码转换问题
===============================C#代码:public static byte[] dencrypt(byte[] by, int size)
{
uint M1 = 0xFAFAFAFA;
uint IA1 = 0xF7F7F7F7;
uint IC1 = 0xF5F5F5F5;
uint key = 0x000004d2;
int idx = 0;
if (key == 0) key = 1;
byte[] byy = new byte[by.Length];
while (idx < size)
{
key = (uint)IA1 * (key % M1) + IC1;
uint ms = ((key >> 20) & 0xFF);
uint sss = (uint)by[idx];
uint mss = sss ^ ms;
byy[idx] = (byte)mss;
idx++;
}
return byy;
}
===================================转换成VB代码:
'Private Function encrypt(ByVal key As Long, _
' buffer() As Byte, ByVal size As Long) As Byte()
Private Sub encrypt(buffer() As Byte, ByVal size As Long)
Const M1 As Long = &HFAFAFAFA
Const IA1 As Long = &HF7F7F7F7
Const IC1 As Long = &HF5F5F5F5
Dim idx As Integer
Dim key As Long
Dim dVm As Double
'Dim dVa As Double
Dim dVc As Double
Dim dTa As Double
'ReDim B(size) As Byte
ReDim b(size - 1) As Byte
key = &H4D2
'If (key = 0) Then key = 1
' Long 类型运算溢出,只能用Double来“中转”
dVm = ConvToULONG(M1)
'dVa = ConvToULONG(IA1)
dVc = ConvToULONG(IC1)
For idx = 0 To size - 1
' key = IA1 * (key % M1) + IC1;
dTa = ConvToULONG(key)
key = ConvToLong(dTa - dVm * Fix(dTa / dVm))
key = ConvToLong(OverflowMULP(key, IA1) + dVc)
'B(idx) = Buffer(idx) Xor (key / 524288 And &HFF)
' >> 20位,应该是 1048576
b(idx) = buffer(idx) Xor (key And &HFF00000) \ 1048576
Next
End Sub
' encrypt = B ' 如果字义成函数,就这样返回字节数组
'End Function
Private Function ConvToULONG(ByVal v As Long) As Double
If (&H80000000 And v) Then
ConvToULONG = 4294967296# + v
Else
ConvToULONG = v
End If
End Function
Private Function ConvToLong(ByVal dv As Double) As Long
dv = dv - 4294967296# * Fix(dv / 4294967296#)
If (dv < 2147483648#) Then
ConvToLong = dv
Else
ConvToLong = dv - 4294967296#
End If
End Function
Private Function OverflowMULP(Va As Long, Vb As Long) As Double
Const HIGHMASK As Long = &HFFFF0000
Const LOWMASK As Long = &HFFFF&
Dim dVaH As Double, dVaL As Double
Dim dVbH As Double, dVbL As Double
dVaH = ConvToULONG(HIGHMASK And Va)
dVbH = ConvToULONG(HIGHMASK And Vb)
dVaL = LOWMASK And Va
dVbL = LOWMASK And Vb
OverflowMULP = dVaH * dVbL + dVbH * dVaL + dVaL * dVbL
End Function
=========================================================================
在正常情况下,运行正常。
但当需要处理的数据量大时,无论是编辑好的可执行程序,或用代码直接运行,运行一段时间都会报处理异常错误。然后整个程序退出。
先前Chen8013仁兄,帮助解决转换问题,再次请教各位仁兄。谢谢! 请问具体是什么异常啊?
它报异常,都有一个错误号的,你要把错误号说一下啊,要不然,我怎么去猜呢。
还有,既然在IDE下也会出异常,那就更好了呀。
它报异常时,点“调试”,这样它就在出错的那一句上中断。
你说下是哪句,
如果是进行运算的语句,那这句中出错时,相关的几个变量值是多少说下。
要找清楚原因,才好“对症下药”呀。
补充:VB , 基础类