Seite 1 von 2

.

Verfasst: 17.06.2012 04:38
von NoUser
.

Re: TEA C++ nach PureBasic

Verfasst: 17.06.2012 11:40
von DarkDragon
Hallo,

Eine 1:1 Übersetzung ist kaum möglich. Daher die 3 Funktionen mit Inline Assembler. Die Shifts sind lediglich logische Shifts, keine arithmetischen und das XOr gibt es erstaunlicherweise nicht in PureBasic (obwohl ich mich an etwas derartiges erinnere).

(Korrigierte Version siehe unten)

Re: TEA C++ nach PureBasic

Verfasst: 17.06.2012 12:26
von NoUser
.

Re: TEA C++ nach PureBasic

Verfasst: 17.06.2012 12:34
von DarkDragon
marroh hat geschrieben:Zum C++ Code hätte ich da noch ein zwei Verständnis fragen:

^ ist in PureBasic Pow()?
+= entspricht in PureBasic einem x = x + ... ?
<< und >> sind also immer logische und keine arithmetischen Shifts oder kann dass auch anders sein?
^ ist in PureBasic anscheinend nicht vorhanden. Das entspricht meiner LogicXOR Funktion und führt ein bitweises, exklusives Oder aus.
x += y ist in PureBasic x = x + y, aber PureBasic versteht auch ein einfaches x + y.
<< und >> sind arithmetische Shifts, allerdings gibt es in PureBasic keine unsigned Datentypen für 32- und 64bit Ganzzahlen. Da unsigned Datentypen kein Vorzeichen haben kommt ein arithmetischer Shift hierbei dem logischen Shift gleich.

Re: TEA C++ nach PureBasic

Verfasst: 17.06.2012 12:43
von NoUser
.

Re: TEA C++ nach PureBasic

Verfasst: 17.06.2012 12:59
von ts-soft
Als 64-Bit kompiliert funktioniert es gut :mrgreen:

Re: TEA C++ nach PureBasic

Verfasst: 17.06.2012 13:09
von DarkDragon
Ah .. ebx ist ja reserviert, setze es um auf ecx:

Code: Alles auswählen

Procedure.l ShiftLR(Value.l, Bits.b)
  !MOV eax, dword [p.v_Value]
  !MOV cl, byte [p.v_Bits]
  !SHR eax, cl
  !MOV dword [p.v_Value], eax
  
  ProcedureReturn Value
EndProcedure

Procedure.l ShiftLL(Value.l, Bits.b)
  !MOV eax, dword [p.v_Value]
  !MOV cl, byte [p.v_Bits]
  !SHL eax, cl
  !MOV dword [p.v_Value], eax
  
  ProcedureReturn Value
EndProcedure

Procedure.l LogicXOR(A.l, B.l)
  !MOV eax, dword [p.v_A]
  !MOV ecx, dword [p.v_B]
  !XOR eax, ecx
  !MOV dword [p.v_A], eax
  
  ProcedureReturn A
EndProcedure

Procedure TeaEncrypt(*InData.Long, *OutData.Long, *Key.Long)
  Protected.l y = PeekL(*InData)
  Protected.l z = PeekL(*InData + SizeOf(Long))
  Protected.l sum = 0
  Protected.l delta = $9E3779B9
  Protected.l a = PeekL(*Key)
  Protected.l b = PeekL(*Key + SizeOf(Long))
  Protected.l c = PeekL(*Key + SizeOf(Long) * 2)
  Protected.l d = PeekL(*Key + SizeOf(Long) * 3)
  Protected.l n = 32
  
  While n > 0
    n - 1
    sum + delta
    y = y + LogicXOR(LogicXOR(ShiftLL(z, 4) + a, z + sum), ShiftLR(z, 5) + b)
    z = z + LogicXOR(LogicXOR(ShiftLL(y, 4) + c, y + sum), ShiftLR(y, 5) + d)
  Wend
  
  PokeL(*OutData, y)
  PokeL(*OutData + SizeOf(Long), z)
EndProcedure

Procedure TeaDecrypt(*InData.Long, *OutData.Long, *Key.Long)
  Protected.l y = PeekL(*InData)
  Protected.l z = PeekL(*InData + SizeOf(Long))
  Protected.l sum = $C6EF3720
  Protected.l delta = $9E3779B9
  Protected.l a = PeekL(*Key)
  Protected.l b = PeekL(*Key + SizeOf(Long))
  Protected.l c = PeekL(*Key + SizeOf(Long) * 2)
  Protected.l d = PeekL(*Key + SizeOf(Long) * 3)
  Protected.l n = 32
  
  While n > 0
    n = n - 1
    z = z - LogicXOR(LogicXOR(ShiftLL(y, 4) + c, y + sum), ShiftLR(y, 5) + d)
    y = y - LogicXOR(LogicXOR(ShiftLL(z, 4) + a, z + sum), ShiftLR(z, 5) + b)
    sum - delta
  Wend
  
  PokeL(*OutData, y)
  PokeL(*OutData + SizeOf(Long), z)
EndProcedure

Define TestValueDecrypted1.q = $DEADBEEFCAFEBABE
Define TestValueEncrypted.q
Define TestValueDecrypted2.q

Dim Key.l(3)
Key(0) = $12345678
Key(1) = $12345678
Key(2) = $12345678
Key(3) = $12345678

TeaEncrypt(@TestValueDecrypted1, @TestValueEncrypted, @Key(0))
TeaDecrypt(@TestValueEncrypted, @TestValueDecrypted2, @Key(0))

Debug Hex(TestValueDecrypted1, #PB_Quad)
Debug Hex(TestValueEncrypted, #PB_Quad)
Debug Hex(TestValueDecrypted2, #PB_Quad)

Re: TEA C++ nach PureBasic

Verfasst: 17.06.2012 14:14
von NicTheQuick
Ein bitweises XOr geht in PB mit '!'. Schon immer. Und Shift gibt es in PB auch.

Re: TEA C++ nach PureBasic

Verfasst: 17.06.2012 16:26
von DarkDragon
NicTheQuick hat geschrieben:Ein bitweises XOr geht in PB mit '!'. Schon immer.
Das habe ich gesucht.
NicTheQuick hat geschrieben:Und Shift gibt es in PB auch.
Ja, aber wie gesagt nur arithmetisches Shifting.

Re: TEA C++ nach PureBasic

Verfasst: 17.06.2012 17:40
von NoUser
.