well i did my best...
i wrote a code that reads a text file into a string variable and encode it then write it to another text file as encrypted and it worked i think i got an encrypted text in the file
then i decided to write another code to read the encoded text file and decode it and write the result in another file this time with no luck i got an empty text file and i do not know why...
here is the test text file:
file test3.txt:
Code: Select all
this is a test text file To test decoding encoding text file
rrrrrbbbbbbb77777777
here is the encoding code:
Code: Select all
Define.s plain_text, text2
Procedure.s aes_encoder(plain_text.s)
StringMemorySize = StringByteLength(plain_text) + SizeOf(Character) ; Space for the string and its null terminating character
*CipheredString = AllocateMemory(StringMemorySize)
*DecipheredString = AllocateMemory(StringMemorySize)
If AESEncoder(@plain_text, *CipheredString, StringByteLength(plain_text), ?Key, 128, ?InitializationVector)
; Print("Ciphered: "+PeekS(*CipheredString)) ; warning, it will stop on the first null byte, only for demo purpose
; AESDecoder(*CipheredString, *DecipheredString, StringByteLength(plain_text), ?Key, 128, ?InitializationVector)
; Print("Deciphered: "+PeekS(*DecipheredString))
EndIf
ProcedureReturn PeekS(*CipheredString)
DataSection
Key:
Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06
InitializationVector:
Data.b $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $41
EndDataSection
EndProcedure
Procedure.s FileToVar(File$)
ReadFile(0, File$)
While Not Eof(0)
VarString$ + ReadString(0) + #CRLF$
Wend
CloseFile(0)
ProcedureReturn VarString$
EndProcedure
Procedure writetofile(file.s, text2.s)
*MemoryID = AllocateMemory(1000) ; allocating a memory block
If *MemoryID
PokeS(*MemoryID, text2) ; we write a string into the memory block
EndIf
If CreateFile(0, file)
WriteData(0, *MemoryID, SizeOf(Character)*Len(text2)) ; write the text from the memory block into the file
CloseFile(0) ; close the previously opened file and so store the written data
Else
Debug "may not create the file!"
EndIf
EndProcedure
plain_text = FileToVar("test3.txt")
text2 = aes_encoder(plain_text)
writetofile("test4.txt", text2)
here is the result
file test4.txt:
Code: Select all
¢ו(_־€@E—גְֱ¬D<uט ¥•qֹֽ£d"ׂף(x;o%ף^‚[*a<VDZ½פƒ]™םy6A(׀כ]ֲ”U`Fו7₪j›¬u¾$ט*;[_רDY₪“ּײ׳ֳ<ר}׳Cפלfסfb lק—Xm״ב.}Y/cY דe₪לPXJ·›jD[i¨qygָֺֻ¦>ƒJVqtoֱYםh_ץקr
here is the decoding code:
Code: Select all
Define.s plain_text, text2
Procedure.s aes_encoder(plain_text.s)
StringMemorySize = StringByteLength(plain_text) + SizeOf(Character) ; Space for the string and its null terminating character
*CipheredString = AllocateMemory(StringMemorySize)
*DecipheredString = AllocateMemory(StringMemorySize)
; If AESEncoder(@plain_text, *CipheredString, StringByteLength(plain_text), ?Key, 128, ?InitializationVector)
; Print("Ciphered: "+PeekS(*CipheredString)) ; warning, it will stop on the first null byte, only for demo purpose
If AESDecoder(*CipheredString, *DecipheredString, StringByteLength(plain_text), ?Key, 128, ?InitializationVector)
; Print("Deciphered: "+PeekS(*DecipheredString))
EndIf
ProcedureReturn PeekS(*CipheredString)
DataSection
Key:
Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06
InitializationVector:
Data.b $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $41
EndDataSection
EndProcedure
Procedure.s FileToVar(File$)
ReadFile(0, File$)
While Not Eof(0)
VarString$ + ReadString(0) + #CRLF$
Wend
CloseFile(0)
ProcedureReturn VarString$
EndProcedure
Procedure writetofile(file.s, text2.s)
*MemoryID = AllocateMemory(1000) ; allocating a memory block
If *MemoryID
PokeS(*MemoryID, text2) ; we write a string into the memory block
EndIf
If CreateFile(0, file)
WriteData(0, *MemoryID, SizeOf(Character)*Len(text2)) ; write the text from the memory block into the file
CloseFile(0) ; close the previously opened file and so store the written data
Else
Debug "may not create the file!"
EndIf
EndProcedure
plain_text = FileToVar("test4.txt")
text2 = aes_encoder(plain_text)
writetofile("test5.txt", text2)
and the result is an empty test5.txt file...
i wonder why? and what am i doing wrong?