嘿,我只是想弄清楚如何让下面的东西开始工作或替换,这样才能发挥作用:
Module1:
Private oTest As Class1
Private InitDone As Boolean
Private Map1(0 To 63) As Byte
Private Map2(0 To 127) As Byte
#If VBA7 Then
Public Declare PtrSafe Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
#Else
Public Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
#End If
Private Declare Sub CopyMemoryByref Lib "Kernel32.dll" & _
Alias "RtlMoveMemory" (ByRef dest As Integer, ByRef & _
source As Integer, ByVal numBytes As Integer)
Private Declare Function VarPtr Lib "vb40032.dll" & _
Alias "VarPtr" (lpObject As Integer) As Long
Public Function EncryptData(ByRef bytMessage() As Byte, ByRef bytPassword() As Byte) As Byte()
Dim bytKey(31) As Byte
Dim bytIn() As Byte
Dim bytOut() As Byte
Dim bytTemp(31) As Byte
Dim lCount, lLength As Integer
Dim lEncodedLength, lPosition As Integer
Dim bytLen(3) As Byte
If Not IsInitialized(bytMessage) Then Exit Function
If Not IsInitialized(bytPassword) Then Exit Function
For lCount = 0 To UBound(bytPassword)
bytKey(lCount) = bytPassword(lCount) : If lCount = 31 Then Exit For
Next lCount
gentables()
gkey(8, 8, bytKey)
lLength = UBound(bytMessage) + 1 : lEncodedLength = lLength + 4
If lEncodedLength Mod 32 <> 0 Then lEncodedLength = lEncodedLength + 32 - (lEncodedLength Mod 32)
ReDim bytIn(lEncodedLength - 1) : ReDim bytOut(lEncodedLength - 1)
Try
CopyMemory(VarPtr(bytIn(0)), VarPtr(lLength), 4)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
For lCount = 0 To lEncodedLength - 1 Step 32
CopyMemory(VarPtr(bytTemp(0)), VarPtr(bytIn(lCount)), 32)
Encrypt(bytTemp)
CopyMemory(VarPtr(bytOut(lCount)), VarPtr(bytTemp(0)), 32)
Next lCount
End FunctionUserForm:
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Dim sTemp, sPassword As String
sTemp = "this is a test"
Debug.Print(" To encrypt: " & sTemp)
sPassword = "blk@vjdii:5@fAB5"
Debug.Print(StrReverse(sPassword))
sPassword = Str2Hex(strEncrypt(sPassword, StrReverse(sPassword)))
Debug.Print(" Secret key: " & sPassword)
sTemp = Str2Hex(strEncrypt(sTemp, sPassword))
Debug.Print(" Encrypt: " & sTemp)
sTemp = Base64EncodeString(sTemp)
Debug.Print("Encrypt w/ Base64: " & sTemp)
sTemp = Base64DecodeString(sTemp)
Debug.Print("Decrypt w/ Base64: " & sTemp)
sTemp = Hex2Str(sTemp)
Debug.Print(" Decrypt: " & strDecrypt(sTemp, sPassword))
End Sub
This code works just fine when using it within the VBA excel code. This is the VB6 output:加密:这是一个测试 密钥: F050C1C2B61E8DCC349DC498D9993F8D11330F12D9E0071B4B83D172FEBE5AED 加密: F899ABA853D21B20F889CFD18BB42C472187B4E1CF613139370313DFD8A492DE 加密w/ Base64: Rjg5OUFCQTg1M0QyMUIyMEY4ODlDRkQxOEJCNDJDNDcyMTg3QjRFMUNGNjEzMTM5MzcwMzEzREZEOEE 0OTJERQ== 解密w/ Base64: F899ABA853D21B20F889CFD18BB42C472187B4E1CF613139370313DFD8A492DE 解密:这是一个测试
但是,当将其转换为.net时,我会得到以下错误:
算术运算导致溢出。
CopyMemory(VarPtr(bytIn(0)),VarPtr(lLength),4)。
为了使它能够工作,我如何重写它,以便VB.net和VB6都可以共享代码库,以便来回地加密/解密字符串消息?
发布于 2017-10-05 14:07:11
在VB.NET中使用VB.NET方法。VB.NET有一个与VB6完全不同的内存模型。不能从VB6!.NET中的VB6 DLL中应用VB.NET函数,.NET有自己的加密系统。请参阅:System.Security.Cryptography命名空间
Array.Copy(bytTemp, 0, bytIn, lCount, 32)VB.NET不仅仅是一个VB7。VB.NET是一种全新的语言,它有一个全新的类型系统,新的库,新的DLL和EXE结构,新的运行时基础设施,新的.(嗯,几乎所有东西都是新的)
正如汉斯·帕桑特已经暗示的那样:拥有VB6和VB.NET的通用代码库几乎是不可能的。
发布于 2022-06-08 04:46:59
Public Function StrToByte(ByRef strInput As String) As Byte()
Dim lPntr As Integer
Dim bTmp() As Byte
Dim bArray() As Byte
If Len(strInput) = 0 Then Exit Function
'999999999---------------------------------------LenB()strInput
ReDim bTmp(LenB(strInput) - 1) 'Memory length
'Dim size As Integer = System.Runtime.InteropServices.Marshal.SizeOf(strInput) - 1
ReDim bArray(Len(strInput) - 1) 'String length
'999999999=---------------------------------------- CopyMemory(strInput, lPntr, 0)
CopyMemory(StrPtr(strInput), System.Runtime.InteropServices.Marshal.SizeOf(strInput))
' CopyMemory(strInput, lPntr, 0)
'Examine every second byte
For lPntr = 0 To UBound(bArray)
If bTmp(lPntr * 2 + 1) > 0 Then
'bArray(lPntr) = Asc(Mid$(strInput, lPntr + 1, 1))
StrToByte = CopyArra(bTmp) ' StrToByte = System.Array.Copy(strInput, lPntr + 1, 1)
Exit Function
Exit Function
Else
bArray(lPntr) = bTmp(lPntr * 2)
End If
Next lPntr
StrToByte = CopyArray(bArray)
End Functionhttps://stackoverflow.com/questions/46587514
复制相似问题