hello, I can not fix this simple project (encrypt string) because I have problems with the replacement of StrPtr
Can anyone explain how to fix?

into form
Imports System.Runtime.InteropServices
Public Class frmMain

    Private Sub btnEnc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnc.Click
        'encrypt
        Dim chiave As String = "aonyregpnxmtxppm"
        txtEnc.Text = mXOR.ASMXORString(txtClear.Text, chiave)
    End Sub

    Private Sub btnDec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDec.Click
        'decrypt
        Dim chiave As String = "aonyregpnxmtxppm"
        txtDec.Text = mXOR.ASMXORString(txtEnc.Text, chiave)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'test for convert StrPtr method.....
        Dim str As String = "username"
        Dim Password As String = "Pass"

        Dim handleStr As GCHandle = GCHandle.Alloc(Str, GCHandleType.Pinned)
        Dim strPtr1 As IntPtr = handleStr.AddrOfPinnedObject()

        Dim handlePwd As GCHandle = GCHandle.Alloc(Password, GCHandleType.Pinned)
        Dim strPtr2 As IntPtr = handlePwd.AddrOfPinnedObject()

        MsgBox(strPtr1)
        MsgBox(strPtr2)
        handleStr.Free()
        handlePwd.Free()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class
into module
Imports System.Runtime.InteropServices
Imports System.Text
Module mXOR

   
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcW" (ByVal ptrMC As Long, ByVal P1 As Long, ByVal P2 As Long, ByVal P3 As Long, ByVal P4 As Long) As Long
    Private MyByteXor(93) As Byte ' Byte array (hold the ASM ShellCode)
    Private ptrMC As Long 'Byte array Pointer Store Value

    Public Function ASMXORString(ByVal Str As String, ByVal Password As String) As String
        Try
            Dim handleStr As GCHandle = GCHandle.Alloc(Str, GCHandleType.Pinned)
            Dim strPtr1 As IntPtr = handleStr.AddrOfPinnedObject()

            Dim handlePwd As GCHandle = GCHandle.Alloc(Password, GCHandleType.Pinned)
            Dim strPtr2 As IntPtr = handlePwd.AddrOfPinnedObject()

            Call LeggiCodiceMasm32()
            Call CallWindowProc(ptrMC, strPtr1, Len(Str), strPtr2, Len(Password))
            handleStr.Free()
            handlePwd.Free()
            ASMXORString = Str
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Function

    Public Sub ASMXORByte(ByVal ByteA() As Byte, ByVal Password As String)
        Call LeggiCodiceMasm32()
        Call CallWindowProc(ptrMC, VarPtr(ByteA(0)), UBound(ByteA), Marshal.StringToCoTaskMemAnsi(Password), Len(Password))
    End Sub


    Sub LeggiCodiceMasm32()
        MyByteXor(0) = 85
        MyByteXor(1) = 137
        MyByteXor(2) = 229
        MyByteXor(3) = 83
        MyByteXor(4) = 86
        MyByteXor(5) = 87
        MyByteXor(6) = 139
        MyByteXor(7) = 69
        MyByteXor(8) = 12
        MyByteXor(9) = 133
        MyByteXor(10) = 192
        MyByteXor(11) = 116
        MyByteXor(12) = 70
        MyByteXor(13) = 139
        MyByteXor(14) = 69
        MyByteXor(15) = 20
        MyByteXor(16) = 133
        MyByteXor(17) = 192
        MyByteXor(18) = 116
        MyByteXor(19) = 63
        MyByteXor(20) = 139
        MyByteXor(21) = 77
        MyByteXor(22) = 12
        MyByteXor(23) = 209
        MyByteXor(24) = 225
        MyByteXor(25) = 139
        MyByteXor(26) = 85
        MyByteXor(27) = 8
        MyByteXor(28) = 1
        MyByteXor(29) = 202
        MyByteXor(30) = 247
        MyByteXor(31) = 217
        MyByteXor(32) = 139
        MyByteXor(33) = 93
        MyByteXor(34) = 20
        MyByteXor(35) = 209
        MyByteXor(36) = 227
        MyByteXor(37) = 139
        MyByteXor(38) = 69
        MyByteXor(39) = 16
        MyByteXor(40) = 1
        MyByteXor(41) = 216
        MyByteXor(42) = 137
        MyByteXor(43) = 69
        MyByteXor(44) = 16
        MyByteXor(45) = 247
        MyByteXor(46) = 219
        MyByteXor(47) = 137
        MyByteXor(48) = 93
        MyByteXor(49) = 20
        MyByteXor(50) = 139
        MyByteXor(51) = 4
        MyByteXor(52) = 10
        MyByteXor(53) = 3
        MyByteXor(54) = 93
        MyByteXor(55) = 16
        MyByteXor(56) = 50
        MyByteXor(57) = 3
        MyByteXor(58) = 43
        MyByteXor(59) = 93
        MyByteXor(60) = 16
        MyByteXor(61) = 129
        MyByteXor(62) = 195
        MyByteXor(63) = 2
        MyByteXor(64) = 0
        MyByteXor(65) = 0
        MyByteXor(66) = 0
        MyByteXor(67) = 117
        MyByteXor(68) = 3
        MyByteXor(69) = 139
        MyByteXor(70) = 93
        MyByteXor(71) = 20
        MyByteXor(72) = 137
        MyByteXor(73) = 4
        MyByteXor(74) = 10
        MyByteXor(75) = 129
        MyByteXor(76) = 193
        MyByteXor(77) = 2
        MyByteXor(78) = 0
        MyByteXor(79) = 0
        MyByteXor(80) = 0
        MyByteXor(81) = 117
        MyByteXor(82) = 223
        MyByteXor(83) = 49
        MyByteXor(84) = 192
        MyByteXor(85) = 95
        MyByteXor(86) = 94
        MyByteXor(87) = 91
        MyByteXor(88) = 137
        MyByteXor(89) = 236
        MyByteXor(90) = 93
        MyByteXor(91) = 194
        MyByteXor(92) = 16
        MyByteXor(93) = 0
        ptrMC = VarPtr(MyByteXor(0))

    End Sub
    ' a delegate that can point to the VarPtrCallback method
    Private Delegate Function VarPtrCallbackDelegate( _
       ByVal address As Integer, ByVal unused1 As Integer, _
       ByVal unused2 As Integer, ByVal unused3 As Integer) As Integer

    ' two aliases for the CallWindowProcA Windows API method
    ' notice that 2nd argument is passed by-reference
    Private Declare Function CallWindowProc Lib "user32" _
       Alias "CallWindowProcA" _
       (ByVal wndProc As VarPtrCallbackDelegate, ByRef var As Short, _
       ByVal unused1 As Integer, ByVal unused2 As Integer, _
       ByVal unused3 As Integer) As Integer

    Private Declare Function CallWindowProc Lib "user32" _
       Alias "CallWindowProcA" _
       (ByVal wndProc As VarPtrCallbackDelegate, ByRef var As Integer, _
       ByVal unused1 As Integer, ByVal unused2 As Integer, _
       ByVal unused3 As Integer) As Integer
    ' ...add more overload to support other data types...

    ' the method that is indirectly executed when calling CallVarPtrSupport
    ' notice that 1st argument is declared by-value (this is the
    ' argument that receives the 2nd value passed to CallVarPtrSupport)
    Private Function VarPtrCallback(ByVal address As Integer, _
          ByVal unused1 As Integer, ByVal unused2 As Integer, _
          ByVal unused3 As Integer) As Integer
        Return address
    End Function

    ' two overloads of VarPtr
    Public Function VarPtr(ByRef var As Short) As Integer
        Return CallWindowProc(AddressOf VarPtrCallback, var, 0, 0, 0)
    End Function
End Module
Exception: Attempted to read or write protected memory. This is often an indication that other memory is corrupt
Responder

Volver a “VB/.NET”