Page 2 of 2

Re: I miss something with Base64 and AES.

Posted: Wed Apr 16, 2025 12:13 pm
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

Re: I miss something with Base64 and AES.

Posted: Wed Apr 16, 2025 2:03 pm
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.

Re: I miss something with Base64 and AES.

Posted: Wed Apr 16, 2025 2:12 pm
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.

Re: I miss something with Base64 and AES.

Posted: Wed Apr 16, 2025 2:42 pm
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!!

Re: I miss something with Base64 and AES.

Posted: Wed Apr 16, 2025 2:52 pm
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