I miss something with Base64 and AES.

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: I miss something with Base64 and AES.

Post by AZJIO »

Code: Select all

EnableExplicit

Define *out, *Buffer, *buf
Define str$, Encoded$
Define size

str$ = "some text"
; str$ = "Some text to test. Some text too."
size = StringByteLength(str$) + SizeOf(Character)
*out = AllocateMemory(size)
If *out
	If AESEncoder(@str$, *out, size, ?key, 256, ?iv, #PB_Cipher_CBC)
		Debug "*out (Cipher) = " + PeekS(*out, size, #PB_Ascii)
		Encoded$ = Base64Encoder(*out, size)
		Debug "Base64 = " + Encoded$
	EndIf
	FreeMemory(*out)
EndIf


; *Buffer = ReceiveHTTPMemory("https://arahiotis.sites.sch.gr/PB/update.php")
*Buffer = Ascii(Encoded$) ; test
If *Buffer
	size = MemorySize(*Buffer)
	str$ = PeekS(*Buffer, size, #PB_Ascii)
	*buf = AllocateMemory(size)
	If *buf
		size = Base64Decoder(str$, *buf, size)
		Debug "*buf (Cipher) = " + PeekS(*buf, size, #PB_Ascii)
		*out = AllocateMemory(size)
		If *out
			If AESDecoder(*buf, *out, size, ?key, 256, ?iv, #PB_Cipher_CBC)
				Debug "Content: " + PeekS(*out)
			EndIf
			FreeMemory(*out)
		EndIf
		FreeMemory(*buf)
	EndIf
	FreeMemory(*Buffer)
EndIf

DataSection
key:
Data.a $97, $74, $91, $AC, $85, $CF, $CA, $37, $B7, $20, $B2, $FD, $D0, $97, $39, $4F, $BE, $E1, $C7, $C1, $B3, $50, $2C, $6C, $F6, $D4, $9A, $E3, $5A, $E1, $40, $AC
iv:
Data.a $86, $43, $1A, $35, $61, $C9, $8A, $E2, $EF, $7C, $49, $8C, $E6, $F4, $6A, $02
EndDataSection
Reduced

Code: Select all

EnableExplicit

Define *out, *Buffer
Define str$, Encoded$
Define size

str$ = "some text"
; str$ = "Some text to test. Some text too."
size = StringByteLength(str$) + SizeOf(Character)
*out = AllocateMemory(size)
If *out
	If AESEncoder(@str$, *out, size, ?key, 256, ?iv, #PB_Cipher_CBC)
		Debug "*out (Cipher) = " + PeekS(*out, size, #PB_Ascii)
		Encoded$ = Base64Encoder(*out, size)
		Debug "Base64 = " + Encoded$
	EndIf
	FreeMemory(*out)
EndIf


; *Buffer = ReceiveHTTPMemory("https://arahiotis.sites.sch.gr/PB/update.php")
*Buffer = Ascii(Encoded$) ; test
If *Buffer
	size = MemorySize(*Buffer)
	str$ = PeekS(*Buffer, size, #PB_Ascii)
	size = Base64Decoder(str$, *Buffer, size)
	Debug "*Buffer(Cipher) = " + PeekS(*Buffer, size, #PB_Ascii)
	If AESDecoder(*Buffer, @str$, size, ?key, 256, ?iv, #PB_Cipher_CBC)
		Debug "Content: " + str$
	EndIf
	FreeMemory(*Buffer)
EndIf

DataSection
key:
Data.a $97, $74, $91, $AC, $85, $CF, $CA, $37, $B7, $20, $B2, $FD, $D0, $97, $39, $4F, $BE, $E1, $C7, $C1, $B3, $50, $2C, $6C, $F6, $D4, $9A, $E3, $5A, $E1, $40, $AC
iv:
Data.a $86, $43, $1A, $35, $61, $C9, $8A, $E2, $EF, $7C, $49, $8C, $E6, $F4, $6A, $02
EndDataSection
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: I miss something with Base64 and AES.

Post by doctorized »

NicTheQuick wrote: Wed Apr 16, 2025 10:39 am Everything's fine here. The code works exactly as expected.
With all my love and respect, you keep telling that. How can you say so? The point is to get the Base64 string from the php file and decode it correctly and not just pass it in a string var. Uncomment ReceiveHTTPMemory, use it to get the string and tell as how it works fine.
User avatar
NicTheQuick
Addict
Addict
Posts: 1519
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: I miss something with Base64 and AES.

Post by NicTheQuick »

doctorized wrote: Wed Apr 16, 2025 2:03 pm
NicTheQuick wrote: Wed Apr 16, 2025 10:39 am Everything's fine here. The code works exactly as expected.
With all my love and respect, you keep telling that. How can you say so? The point is to get the Base64 string from the php file and decode it correctly and not just pass it in a string var. Uncomment ReceiveHTTPMemory, use it to get the string and tell as how it works fine.
That's what I am saying. I did that and it works.

Code: Select all

*Buffer = ReceiveHTTPMemory("https://arahiotis.sites.sch.gr/PB/update.php")
If *Buffer
	size = MemorySize(*Buffer)
	Debug(size)
	
	str$ = PeekS(*Buffer, size, #PB_UTF8|#PB_ByteLength)
	Debug str$ 
	
	size = Len(str$)
	Debug "size 1 = " + Str(size)
	*buf = AllocateMemory(size)
	*out = AllocateMemory(size)
	size = Base64Decoder(str$, *buf, size)
	Debug "size 2 = " + Str(size)
	AESDecoder(*buf,*out,size,?key,256,?iv,#PB_Cipher_CBC)
	
	Debug "Content: " + PeekS(*out)
EndIf


DataSection
	key:
	Data.a $97, $74, $91, $AC, $85, $CF, $CA, $37, $B7, $20, $B2, $FD, $D0, $97, $39, $4F, $BE, $E1, $C7, $C1, $B3, $50, $2C, $6C, $F6, $D4, $9A, $E3, $5A, $E1, $40, $AC
	iv:
	Data.a $86, $43, $1A, $35, $61, $C9, $8A, $E2, $EF, $7C, $49, $8C, $E6, $F4, $6A, $02
EndDataSection
It outputs:
92
iV7bpLUqiqGhk/1B/ArzzUnr9sqxVHRdkDks95cT+iZ4jRZx6Qj8hZbKny5egUWIr2BCB/YTglNQjZZYBP+jW5EPZtU=
size 1 = 92
size 2 = 68
Content: Some text to test. Some text too.
I see no error. It's working. But if you do not tell us what error you see, no one can help.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: I miss something with Base64 and AES.

Post by doctorized »

That's it, I quit. I will start fishing from now on. Thank you very much for your replays and your time! Be good!!
User avatar
NicTheQuick
Addict
Addict
Posts: 1519
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: I miss something with Base64 and AES.

Post by NicTheQuick »

doctorized wrote: Wed Apr 16, 2025 2:42 pm That's it, I quit. I will start fishing from now on. Thank you very much for your replays and your time! Be good!!
Don't quit programming. :D
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply