转换VB代码问题
表B.6 加密算法
Const unsigned long M1 =A;
Const unsigned long IA1 =B;
Const unsigned long IC1 =C;
Void encrypt(
unsigned long key,
unsigned char* buffer,
unsigned short size )
{
unsigned short idx = 0;
if( key == 0 ) key = 1;
while( idx < size )
{
key = IA1 * ( key % M1 ) + IC1;
buffer[idx++] ^= (unsigned char)((key>>20)&0xFF);
}
}
===================================================
求教,如上C程序代码如何转成VB代码,谢谢各位大神。 ?
[解决办法]
代码不全,只能随便转转
const M1 as long = A
const IA1 as long = B
const IC1 as long = C
sub encrypt(byval key as long, byref buffer() as byte, byval size as integer)
dim idx as integer
idx = 0
if not(key) then key = 1
while idx < size
key = IA1 * (key mod M1) + IC1
idx = idx + 1
buffer(idx) = buffer(idx) xor (key / 524288 and &HFF)
wend
end sub
Option Explicit
'Const unsigned long M1 =A;
'Const unsigned long IA1 =B;
'Const unsigned long IC1 =C;
' 常量的具体值你自己写上, VB中16进制数用 &H前缀
Private Const M1 As Long = A
Private Const IA1 As Long = B
Private Const IC1 As Long = C
'Void encrypt(
'unsigned long key,
'unsigned char* buffer,
'unsigned short size )
'{
' unsigned short idx = 0;
' if( key == 0 ) key = 1;
' While (idx < Size)
' {
' key = IA1 * ( key % M1 ) + IC1;
' buffer[idx++] ^= (unsigned char)((key>>20)&0xFF);
' }
'}
Private Sub encrypt(ByVal key As Long, _
buffer() As Byte, ByVal size As Long)
Dim idx As Long
If (k = 0) Then key = 1
For idx = 0 To size - 1
key = IA1 * (key Mod M1) + IC1
buffer(idx) = buffer(idx) Xor ((key And &HFF00000) \ &H100000)
Next
' 由于 key 在循环过程中不发生变化,实际应该这样优化:
'If (k = 0) Then key = 1
'key = (IA1 * (key Mod M1) + IC1 And &HFF00000) \ &H100000
'For idx = 0 To size - 1
' buffer(idx) = buffer(idx) Xor key
'Next
End Sub