This is a simple example:
Code: Select all
;No Unicode!!!
String$ = "Hello this is a test for AES"
Debug String$
Debug "Len: " + Len(String$) ;28, we need 32
*CipheredString = AllocateMemory(Len(String$) + 1) ;with Zero_Byte
*DecipheredString = AllocateMemory(Len(String$) + 1) ;with Zero_Byte
AESEncoder(@String$, *CipheredString, Len(String$), ?KeyPB, 128, 0, #PB_Cipher_ECB)
Debug "Ciphered: "+PeekS(*CipheredString)
For i = 0 To Len(String$) - 1
If i % 16 = 0
Debug "==========================================" ;Optik
EndIf
Debug Hex(PeekA(*CipheredString + i))
Next
AESDecoder(*CipheredString, *DecipheredString, Len(String$), ?KeyPB, 128, 0, #PB_Cipher_ECB)
Debug "Deciphered: " + PeekS(*DecipheredString)
Debug "=========================================="
Debug "Internal:"
String1$ = Space(32)
For i = 0 To Len(String$) - 1
PokeA(@String1$ + i, PeekA(@String$ + i))
Next
;Length of last Block is 12 -> we need 4 Bytes
PokeA(@String1$ + 28, $5) ;copy Bytes from the Block before last Block into last Block (see Debug-Output)
PokeA(@String1$ + 29, $31)
PokeA(@String1$ + 30, $c8)
PokeA(@String1$ + 31, $94)
*CipheredString1 = AllocateMemory(Len(String1$) + 1) ;with Zero_Byte
*DecipheredString1 = AllocateMemory(Len(String1$) + 1) ;with Zero_Byte
AESEncoder(@String1$, *CipheredString1, Len(String1$), ?KeyPB, 128, 0, #PB_Cipher_ECB)
;Debug "Ciphered: " + PeekS(*CipheredString1)
For i = 0 To Len(String1$) - 1
If i % 16 = 0
Debug "=========================================="
EndIf
Debug Hex(PeekA(*CipheredString1 + i))
Next
If i % 16 = 0
Debug "=========================================="
EndIf
;now change the last Block with the Block before and set old Length
Buffer = AllocateMemory(32)
CopyMemory(*CipheredString1, Buffer + 16, 16)
CopyMemory(*CipheredString1 + 16, Buffer, 16)
For i = 0 To 31
If i % 16 = 0
Debug "=========================================="
EndIf
Debug Hex(PeekA(Buffer + i))
Next
If i % 16 = 0
Debug "=========================================="
EndIf
PokeB(Buffer + 28, 0) ;set old String-Length
For i = 0 To 27
If i % 16 = 0
Debug "=========================================="
EndIf
Debug Hex(PeekA(Buffer + i))
Next
If i % 16 = 0
Debug "=========================================="
EndIf
AESDecoder(Buffer, *DecipheredString1, Len(String$), ?KeyPB, 128, 0, #PB_Cipher_ECB)
Debug "Deciphered: " + PeekS(*DecipheredString1)
DataSection
KeyPB:
Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06
EndDataSection