(WIN) AESDecoder - reload Ascii -file - in Memory problem
Posted: Sat Jun 29, 2019 7:25 am
Hello,
I hope someone can help me, the problem is that I do not get the contents of the text file completely in memory. And this text is not decoded by the AESDecoder command. Maybe I miss a small thing, but maybe not knowing how to do it . Thank you in advance, if I come to the solution before I post it, of course
I hope someone can help me, the problem is that I do not get the contents of the text file completely in memory. And this text is not decoded by the AESDecoder command. Maybe I miss a small thing, but maybe not knowing how to do it . Thank you in advance, if I come to the solution before I post it, of course
Code: Select all
Global *Buffer = AllocateMemory(256) ; Allocate a global memory buffer to avoid realloc
Global *MemoryID = AllocateMemory(256)
Global Text$
Enumeration
#SelectFileGadget
EndEnumeration
; Easy procedure to convert a datasection to an allocated memory block
;
Procedure DataSectionToMemory(Size)
Protected *Buffer
*Buffer = AllocateMemory(Size)
For k = 0 To Size-1
Read.b value
PokeB(*Buffer, k, value)
Next
ProcedureReturn *Buffer
EndProcedure
Procedure CallbackS(Status, Filename$, File, SizeRead)
Select Status
Case #PB_Status_Saved
; File correctly saved
Case #PB_Status_Error
; File saving has failed
EndSelect
EndProcedure
Procedure ReadCallback(Status, Filename$, File, Size)
If Status = #PB_Status_Loaded
; Read all data into the memory buffer
length = Lof(0)
Debug "LL "+ length
; get the length of opened file
Text$= ReadString(0, #PB_Ascii, length) ; read all data into the memory block
Debug "read: " + Text$
; maybe the problem part Beginn
If *MemoryID
Debug "Decodieren"
For k = 0 To 72
TestView$ + Hex(PokeB(*MemoryID,1, k), #PB_Byte) + " "
Next
Debug "Test1:"+ Testview$
PokeS(*MemoryID, 0, Text$, 128, #PB_Ascii)
Dummy$=PeekS(*MemoryID, -1, #PB_Ascii)
Debug "Test2 : "+ Dummy$
Debug "Decodieren2"
*DecodedText = AllocateMemory(256)
AESDecoder(*MemoryID, 0, *DecodedText, 0, 128, *AESKey1, 256, *AESInitializationVector1)
Debug "Decoded text: " + PeekS(*DecodedText, 0, 128, #PB_Ascii)
Debug "Fertig"
CloseFile(0)
EndIf
ElseIf Status = #PB_Status_Error
Debug "Error when loading the file: " + Filename$
EndIf
; Decode it with the same InitializationVector and private key
EndProcedure
; maybe the problem part END
Procedure OpenFileRequesterCallback()
If NextSelectedFile()
If ReadFile(0, SelectedFileID(), @ReadCallback(), #PB_LocalFile )
EndIf
EndIf
EndProcedure
Procedure ChooseFileEvent()
OpenFileRequester("*.txt", @OpenFileRequesterCallback())
EndProcedure
; Mainprg BEGIN
Text$ = "Single block msg1 Single block msg2 Single block msg3 Single block msg4"
Debug "Original text: " + Text$
; AES encoder can only work with memory buffer, so put our text in a memory buffer
*AsciiText = AllocateMemory(256)
PokeS(*AsciiText, 0, Text$, -1, #PB_Ascii)
; Get the InitializationVector value (needed for CBC encoding)
Restore AESInitializationVector1
*AESInitializationVector1 = DataSectionToMemory(16)
; Our private key
Restore AESKey1
*AESKey1 = DataSectionToMemory(32)
; The output buffer, needs to be input buffer size+1, aligned to the next 16-byte block
;
*Output = AllocateMemory(256)
AESEncoder(*AsciiText, 0, *Output, 0, 128, *AESKey1, 256, *AESInitializationVector1)
; Display the encoded message
;
For k = 0 To 72
HexView$ + Hex(PeekB(*Output, k), #PB_Byte) + " "
Next
Debug "Text encoded (hex view): " + HexView$
If CreateFile(0, "Text.txt", @CallbackS())
WriteStringN(0, HexView$)
ExportFile(0, "text/plain")
CloseFile(0)
Debug "Datei geschrieben"
EndIf
Debug "Datei wiedereinlesen"
If OpenWindow(0, 0, 0, 300, 80, "Decoder", #PB_Window_ScreenCentered)
ButtonGadget(#SelectFileGadget, 10, 10, 280, 25, "Please choose text.txt ...")
BindGadgetEvent(0, @ChooseFileEvent())
EndIf
; Main END
; *****************************
; Data
; *****************************
DataSection
AESInitializationVector1:
Data.b $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $41,$3d
AESKey1:
Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06,$06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06
EndDataSection