Best way to store AES encoded strings
Posted: Wed Oct 23, 2024 7:23 pm
I'm trying to experiment with ciphers in PB but I have a question. I have a simple library I use to encrypt/decrypt strings:
I confirm that this works by running this program:
So far, so good. If I want to store the encoded strings for Server, Login and Password I simply write out the strings to a file. I wrote this program for that:
This stores three encrypted strings, one string per line. Now, if I retrieve these strings and try to decrypt them, I use this program:
The Debug window does not always show a complete decryption. The decrypted Login string is usually gibberish. So, I guess I'm trying to figure what I'm doing wrong. Is it in my cipher library (which I basically copied out of the Help file)? Is it my read and write to the text file? Maybe I should write the strings as plain text to the file and then just encrypt the whole file??? Any help would be appreciated. Thanks.
Code: Select all
; Cipher.pbi
;===========
Procedure.s DecodeString(TextString.s)
StringMemorySize = StringByteLength(TextString) + SizeOf(Character)
*DecipheredString = AllocateMemory(StringMemorySize)
If AESDecoder(@TextString, *DecipheredString, StringByteLength(TextString), ?Key, 128, ?InitializationVector)
ProcedureReturn PeekS(*DecipheredString)
EndIf
ProcedureReturn ""
EndProcedure
Procedure.s EncodeString(TextString.s)
StringMemorySize = StringByteLength(TextString) + SizeOf(Character)
*CipheredString = AllocateMemory(StringMemorySize)
If AESEncoder(@TextString, *CipheredString, StringByteLength(TextString), ?Key, 128, ?InitializationVector)
ProcedureReturn PeekS(*CipheredString)
EndIf
ProcedureReturn ""
EndProcedure
DataSection
Key:
Data.b $1E, $D6, $F0, $6B, $D1, $75, $BF, $A6, $DD, $10, $9F, $64, $3B, $E7, $3F, $90
InitializationVector:
Data.b $9F, $8F, $B0, $19, $30, $D0, $A4, $91, $44, $4C, $9F, $CB, $A2, $FC, $BA, $12
EndDataSection
Code: Select all
IncludeFile "Cipher.pbi"
Server$ = "sftp://super.secret.server"
Login$ = "superlogin"
Password$ = "secretpassword"
EncodedServer$ = EncodeString(Server$)
EncodedLogin$ = EncodeString(Login$)
EncodedPassword$ = EncodeString(Password$)
DecodedServer$ = DecodeString(EncodedServer$)
DecodedLogin$ = DecodeString(EncodedLogin$)
DecodedPassword$ = DecodeString(EncodedPassword$)
Debug EncodedServer$
Debug EncodedLogin$
Debug EncodedPassword$
Debug " "
Debug DecodedServer$
Debug DecodedLogin$
Debug DecodedPassword$
Code: Select all
IncludeFile "Cipher.pbi"
#File = 0
Server$ = "sftp://super.secret.server"
Login$ = "superlogin"
Password$ = "secretpassword"
EncodedServer$ = EncodeString(Server$)
EncodedLogin$ = EncodeString(Login$)
EncodedPassword$ = EncodeString(Password$)
CreateFile(#File, "TestFile.txt")
WriteStringN(#File, EncodedServer$)
WriteStringN(#File, EncodedLogin$)
WriteStringN(#File, EncodedPassword$)
CloseFile(#File)
Code: Select all
IncludeFile "Cipher.pbi"
#File = 0
OpenFile(#File, "TestFile.txt")
Server$ = ReadString(#File)
Login$ = ReadString(#File)
Password$ = ReadString(#File)
CloseFile(#File)
DecodedServer$ = DecodeString(Server$)
DecodedLogin$ = DecodeString(Login$)
DecodedPassword$ = DecodeString(Password$)
Debug DecodedServer$
Debug DecodedLogin$
Debug DecodedPassword$