Best way to store AES encoded strings

Just starting out? Need help? Post your questions and find answers here.
User avatar
Catdaddy
User
User
Posts: 30
Joined: Wed Mar 23, 2022 3:40 pm

Best way to store AES encoded strings

Post by Catdaddy »

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:

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

I confirm that this works by running this program:

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$

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:

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)
This stores three encrypted strings, one string per line. Now, if I retrieve these strings and try to decrypt them, I use this program:

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$
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.
User avatar
HeX0R
Addict
Addict
Posts: 1219
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Best way to store AES encoded strings

Post by HeX0R »

The problem are the zero bytes which the output of the AES encoder can contain.
Best would be to use base64 additionally.
User avatar
Catdaddy
User
User
Posts: 30
Joined: Wed Mar 23, 2022 3:40 pm

Re: Best way to store AES encoded strings

Post by Catdaddy »

So, I changed Cipher.pbi to this:

Code: Select all

Procedure.s DecodeString(TextString.s)
	*DecodedBuffer = AllocateMemory(1024)
	Base64Decoder(TextString, *DecodedBuffer, 1024)	
	StringMemorySize = StringByteLength(PeekS(*DecodedBuffer)) + SizeOf(Character)
	*DecipheredString = AllocateMemory(StringMemorySize) 
	If AESDecoder(*DecodedBuffer, *DecipheredString, StringByteLength(PeekS(*DecodedBuffer)), ?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)
		Encoded$ = Base64Encoder(*CipheredString, StringByteLength(PeekS(*CipheredString), #PB_UTF8))
		ProcedureReturn Encoded$
	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

And it works like a charm. Thank you so much, HeXOR
.
Post Reply