.
Verfasst: 17.06.2012 04:38
.
^ ist in PureBasic anscheinend nicht vorhanden. Das entspricht meiner LogicXOR Funktion und führt ein bitweises, exklusives Oder aus.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?
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)Das habe ich gesucht.NicTheQuick hat geschrieben:Ein bitweises XOr geht in PB mit '!'. Schon immer.
Ja, aber wie gesagt nur arithmetisches Shifting.NicTheQuick hat geschrieben:Und Shift gibt es in PB auch.