SelfE by Dare2 help (pb 2 vb)

Just starting out? Need help? Post your questions and find answers here.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

SelfE by Dare2 help (pb 2 vb)

Post by Shannara »

Referenced Thread: viewtopic.php?t=15203&highlight=selfe

On the bottom of the referenced thread, Dare2 posted a nice encryption code that is actually quite wonderful. A copy of it is found below:

Code: Select all

Procedure.s selfE(src.s,en.l)
  k1=Len(src)
  If k1>0
    *p=@src
    k2=PeekB(*p) & $FF
    r=k1 ! k2
    If r<>0 : PokeB(*p,r) : EndIf
    For i=2 To Len(src)
      *p+1
      If en : k1=PeekB(*p-1) & $FF : Else : k1=k2 : EndIf
      k2=PeekB(*p)
      r=k1 ! k2
      If r<>0 : PokeB(*p,r) : EndIf
    Next
  EndIf
  ProcedureReturn src
EndProcedure

w.s="Wooo! Hooo! This is self encrypting"
x.s=selfE(w,#True)
y.s=selfE(x,#False)
Debug w
Debug x
Debug y
I am trying to convert the code over to VB6 due to the client is in PureBasic while the server is in Visual Basic 6. I wanted the communications to be encrypted between the two programs with no external DLLs), so, in order to do that, the encryption would need to be in pure code.

The following is what I have so far in VB6 ...

Code: Select all

Option Explicit

'\\ API declarations...
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Long, Source As Long, ByVal Length As Long)
Private Declare Sub CopyMemoryByte Lib "kernel32" Alias "RtlMoveMemory" (Destination As Byte, Source As Long, ByVal Length As Long)
Private Declare Sub CopyMemoryFromByte Lib "kernel32" Alias "RtlMoveMemory" (Destination As Long, Source As Byte, ByVal Length As Long)

Public Function SelfE(SRC As String, EN As Long) As String
    Dim ptrP    As Long
    Dim I       As Long
    Dim K1      As Byte
    Dim K2      As Long
    Dim R       As Long
    
    K1 = LenB(SRC)

    If K1 > 0 Then
        ptrP = VarPtr(ByVal SRC)
        K2 = PeekByte(VarPtr(ptrP)) & &HFF
    
        R = K1 Or K2
       
        If R <> 0 Then
            Call PokeByte(VarPtr(ptrP), R)
        End If

        For I = 2 To LenB(SRC)
            ptrP = ptrP + 1
    
            If EN = True Then
                K1 = PeekByte(VarPtr(ptrP) - 1) & &HFF
            Else
                K1 = K2
            End If
            
            K2 = PeekByte(VarPtr(ptrP))
            R = K1 Or K2
            
            If R <> 0 Then
                Call PokeByte(VarPtr(ptrP), R)
            End If
        Next I
    End If

    SelfE = SRC

End Function

Public Function Peek(Address As Long) As Long
    Call CopyMemory(Peek, ByVal Address, Len(Address))
End Function

Public Function PeekByte(Address As Long) As Byte
    Call CopyMemoryByte(PeekByte, ByVal Address, Len(PeekByte))
End Function
Public Function Poke(Address As Long, Value As Long)
    CopyMemory ByVal Address, Value, LenB(Value)
End Function

Public Function PokeByte(Address As Long, Value As Byte)
    CopyMemoryFromByte ByVal Address, Value, LenB(Value)
End Function
The problem is overflow errors and for the life of me, I cannot figure out to get the bugger working. I figured the resulting is a byte overflow, which makes it a long, but it is referenced as a byte elsewhere ..

Any clues?
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

Just for info, the '!' operator is 'Exclusive OR' not 'OR' as you have done in the VB source code..

Also you should have a look at the Peek routines in your VB code, the arguments you pass to copymemory seems odd..
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Hmm, i was doing a search on the net for VB6 equivilent of Peek and Poke since MS took out those commands since VB4 or so. So i had to find some sort of equivilent :) thanks for the info about exclusive or, I'll see if I can find the VB Equivilent.
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

On second thought, maybe the arguments you pass to copymemory are not so odd, i'm just not familiar with VB syntax, i assumed 'Len' was only used to get the length of strings, not longs, bytes etc :)
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Yeah, the difference in vb and pb, is that VB's len gets the .. oh wiat, good point. Len in vb gets the length in number of characters in the string while LenB gets the number of bytes :) Im basically stumped now. I know there are quite a few VBers on these forums and am hoping they can help :)

btw: You have been a GREAT help here, translating is always fun :)
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi Shannara,

In VB the MID is both function and statement, I think? That is, Mid(Str, Pos, Len) = Str is acceptable? If so, something like this, perhaps:

Code: Select all

Function selfE( src As String, en As Bool)
  k1 = Len(src)
  If k1 > 0 Then
    tmpStr = src
    ptr = 1
    k2 = Asc(Mid(tmpStr, ptr, 1))
    r = k1 Xor k2
    If r <> 0 Then
      Mid(tmpStr, ptr, 1) = Chr(r)
    End If
    For i = 2 To Len(Src)
      ptr + 1
      If en Then
         k1 = mid( tmpStr, ptr-1, 1)
       Else
         k1 = k2
      End If
      k2 = Mid ( tmpStr, ptr, 1)
      r = k1 Xor k2
      If r <> 0 Then
         Mid(tmpStr, ptr, 1) = Chr(r)
      End If
    Next
   End If
   selfE = tmpStr
End Function
But this may not be VB code (or any basic code for all I know) :) but it is hopefully close enough to be useful?
@}--`--,-- A rose by any other name ..
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Hmm, I think it's getting close. As i understand it, K2 and K1 in PB are long values as the default deftype is long (ref: manual). Mid and ASC in VB returns a string...

Hmm, Im not that well verse for a capable conversion. But you may be on to something .. I'll dink around on this some more. And meanwhile, if there are any PB converts from VB out there that can help, please do!
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Ah.

Perhaps just Asc() around the Mid extractions and Chr() around the Mid inserts?

Code: Select all

    k2 = Asc(Mid(tmpStr, ptr, 1))
    k1 = Asc(mid( tmpStr, ptr-1, 1))
    k2 = Asc(Mid ( tmpStr, ptr, 1))
The basic idea is just to XOR previous byte against current byte (in most circumstances) and longs were used as work variable because there is no unsigned bye in PureBasic.

Similarly the Peek and Poke and pointer are just to get characters out of and into strings. So with VB using Mid (adjusted to turn resulting string into decimal equiv) and an index into the string it should do the trick.

Sorry can't be more help.
@}--`--,-- A rose by any other name ..
RemyVincent
New User
New User
Posts: 9
Joined: Tue Oct 25, 2005 8:47 pm
Contact:

Re: SelfE by Dare2 help (pb 2 vb)

Post by RemyVincent »

Shannara wrote:The problem is overflow errors and for the life of me...
...
a long, but it is referenced as a byte elsewhere ..
Any clues?

"Overflow errors" should happen if your internal encription PROCEDURE returns bytes in range 1..255... What if your crypted STRING is catched by a firewall???

"Overflow errors" SHOULDN'T happen if your internal ENCRYPTION PROCEDURE returns bytes in range '0'..'9','A'..'F', JUST BECAUSE these codes are very usual and are not able to damage anything in any Internet Data transfert...


Procedure.s selfE(src.s,en.l)

Current selfE encryption

Conversion from range 1..255 ==TO==> '0'..'9','A'..'F'

ProcedureReturn src
EndProcedure


:roll: :oops: :oops: ...ANY BODY HAS ANOTHER IDEA... :oops: :oops: :roll:


(((If my english is not clear enough, I could post this explanation in French language and you could find an automatic-translator or a friend)))
Groups lower your IQ
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

I think I may get what your saying, but I thought overflow would be passing a value bigger then allowed. Either way, I'll wait for some former VBer take a crack at this.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi Shannara,

The idea of the code is to XOR each byte with the value of the previous byte, and to XOR the first byte with some value that will not change regardless of "encrypted state" - here the length of the string was used.

This just uses the fact that XORing a "target value" with the same "XOR value" or key will flip-flop or toggle the target result.

There are several coding ways to approach this.

An easy way would be to use the first string to build a second string. You just XOR the next sourceString$ character and concatenate with the destString$. The VB equivalent of:

Code: Select all

destString$ + Chr(cryptChar)
For eg, you could work backwards through the string to encrypt and forwards to decrypt - always using the earlier (index position - 1) character to work on the current (index) character and some known and unchanging value (in context) to mod the first.


BTW: This is pretty weak encryption and perhaps should be used in conjunction with something else. When I feel paranoic then I use things like this to polish something already encrypted using more robust algos.
@}--`--,-- A rose by any other name ..
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Heh, this was the only encryption on all three forums (english, french, and deutch (sp?)) AND all pb related sites, that had the slightest chance of being able to be converted over to VB, and end up with the correct result.

Thanks for the explaination though, I'll keep plugging away at this.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Just my cent with PB optimized code :

Code: Select all

Structure Bytes
  b.b[0]
EndStructure

Procedure.s Encod_Decod(String.s, Encode.l)
  lString = Len(String)
  If lString > 0
    lString1 = lString - 1
    *p.Bytes = @String
    Byte.l = *p\b[0]
    XORByte = lString ! Byte
    If r <> 0
        *p\b[0] = XORByte
    EndIf
    For i = 1 To lString1
      If Encode
          lString = *p\b[i - 1]
        Else
          lString = Byte
      EndIf
      Byte = *p\b[i]
      XORByte = lString ! Byte
      If XORByte <> 0
          *p\b[i] = XORByte
      EndIf
    Next
  EndIf
  ProcedureReturn String
EndProcedure
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
Post Reply