0

below is my vb6 code

    Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)


Public Property Let Key(New_Value As String)

  Dim i As Long
  Dim j As Long
  Dim K As Long
  Dim dataX As Long
  Dim datal As Long
  Dim datar As Long
  Dim Key() As Byte
  Dim KeyLength As Long

  'Do nothing if the key is buffered
  If (m_KeyValue = New_Value) Then Exit Property
  m_KeyValue = New_Value

  'Convert the new key into a bytearray
  KeyLength = Len(New_Value)
  Key() = StrConv(New_Value, vbFromUnicode)

  'Create key-dependant p-boxes
  j = 0
  For i = 0 To (ROUNDS + 1)
    dataX = 0
    For K = 0 To 3
      Call CopyMem(ByVal VarPtr(dataX) + 1, dataX, 3) 'the problem is here
      dataX = (dataX Or Key(j))
      j = j + 1
      If (j >= KeyLength) Then j = 0
    Next
    m_pBox(i) = m_pBox(i) Xor dataX
  Next

End Property

CopyMem sub lib how do i use it in vb.net

now here is my vb.net code for the same

  Private Declare Sub CopyMem Lib "KERNEL32" Alias "RtlMoveMemory" (ByVal pDst As Object, ByVal pSrc As Object, ByVal ByteLen As Integer)


 Public WriteOnly Property Key() As String
        Set(ByVal Value As String)

            Dim i As Long
            Dim j As Long
            Dim K As Long
            Dim dataX As Long
            Dim datal As Long
            Dim datar As Long
            Dim Key() As Byte
            Dim KeyLength As Long

            'Do nothing if the key is buffered
            If (m_KeyValue = Value) Then Exit Property
            m_KeyValue = Value

            'Convert the new key into a bytearray
            KeyLength = Len(Value)

            Key = System.Text.Encoding.Unicode.GetBytes(Value)

            'Create key-dependant p-boxes
            j = 0

            For i = 0 To (ROUNDS + 1)
                dataX = 0
                For K = 0 To 3


                    CopyMem(VarPtr(dataX) + 1, dataX, 3) ' the problem is here
                    dataX = (dataX Or Key(j))
                    j = j + 1
                    If (j >= KeyLength) Then j = 0

                Next
                m_pBox(i) = m_pBox(i) Xor dataX
            Next
 End Property

here is code for VarPtr

Public Function VarPtr(ByVal e As Object) As Object
        Dim GC As GCHandle = GCHandle.Alloc(e, GCHandleType.Pinned)
        Dim GC2 As Integer = GC.AddrOfPinnedObject.ToInt32
        GC.Free()
        Return GC2
    End Function

i have refered to Equivalent of CopyMemory in .NET

but still i am not getting this

please somebody help!!!

Dandy
  • 467
  • 1
  • 10
  • 33
  • Sorry, I cannot see a question (stating "it does not work" is not a question). – Richard Feb 04 '15 at 09:48
  • CopyMem(VarPtr(dataX) + 1, dataX, 3) ' the problem is here its just goes into infinity or hang up no error nothing – Dandy Feb 04 '15 at 09:58
  • Please put that *in the question*. PS. what's wrong with `Array.Copy` with `byte` arrays? PPS. you appear to be trying to write your own cryptography: "anyone can write an algorithm they cannot break; this says nothing about whether others can break it". – Richard Feb 04 '15 at 10:02
  • its basically memory copying i dont knw how to achieve the same in vb.net (its a blowfish cipher) – Dandy Feb 04 '15 at 10:09

1 Answers1

4

If you want to access data using pointers in .NET then you need to keep them pinned during the whole operation. The VarPtr method pins the object while getting the address to it, but then it unpins the object. That means that the object can be moved while you are doing the CopyMem call. Most of the time the object isn't moved, so it would seem to work fine, but when it is moved the CopyMem operation could change some other data. That could make any object in your application behave strangely, or crash the application.

Anyhow, using memory copy is definitely overkill for moving a few bits in an integer. (The Long data type in VB 6 corresponds to the Integer data type in VB.NET by the way.)

You can convert the integer to a byte array, use the Array.Copy method, and then convert it back:

Dim temp As Byte() = BitConverter.GetBytes(dataX)
Array.Copy(temp, 0, temp, 1, 3)
dataX = BitConverter.ToInt32(temp, 0)

You can also do it using bit operations:

dataX = (dataX And &HFF) Or (dataX << 8)

Side note: The Encoding.Unicode is for the UTF-16 encoding. That means that the byte array that GetBytes returns will be twice the length of the string, so you will be using only half of the string.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • a small request i am just begineer for this stuff will you explain what your code actully does and also the "side note" you stated pl. – Dandy Feb 04 '15 at 10:37
  • 2
    @Dandy: The code copies three bytes of the four bytes in the integer, from positions 1,2,3 to positions 0,1,2. The bit operation does the same by masking out the single byte that stays in place and combining it with the three bytes shifted eight bits (one byte). Regarding the encoding, if you for example have the string `"ABC"`, the `Encoding.Unicode.GetBytes` method won't return the array `{ 65, 66, 67 }`, it will return the array `{ 65, 0, 66, 0, 67, 0 }` as each character is encoded into two bytes. – Guffa Feb 04 '15 at 10:45
  • does BitConverter.GetBytes does same thing as Encoding.Unicode.GetBytes what i mean to say is will BitConverter.GetBytes also returns will be twice the length of the string ? – Dandy Feb 04 '15 at 11:19
  • @Dandy: You can't use the [`Bitconverter.GetBytes` method](https://msdn.microsoft.com/en-us/library/system.bitconverter.getbytes%28v=vs.110%29.aspx) on a string. It's used to convert value types to their byte representation. – Guffa Feb 04 '15 at 11:42
  • Array.Copy(temp, 1, temp, 0, 3) is not giving same result as CopyMem(VarPtr(dataX) + 1, dataX, 3) – Dandy Feb 05 '15 at 06:21
  • @Dandy: Sorry, got the source and destination swapped when I read the `CopyMem` parameters. I have corrected the code to copy from positions 0,1,2 to positions 1,2,3. – Guffa Feb 05 '15 at 08:14