What is wrong and Base64 creates a chinese string?

Just starting out? Need help? Post your questions and find answers here.
User avatar
doctorized
Addict
Addict
Posts: 856
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

What is wrong and Base64 creates a chinese string?

Post by doctorized »

My question may be stupid but, Base64EncoderBuffer() should create a string like "VABoAGkAcwAgAGkAcwAgAGEAIAB0AGUAcwB0ACAAcwB0AHIAaQBuAGcAIQA=" which is produced by the sample code in help, right?
In my cace, a chinese string is produced, what am I missing? Is this normal?

Code: Select all

InitNetwork()

key.s = "12345678901234567890123456789012"
InitializationVector.s = "09876543210987654321098765432100"
String$ = "string coming from the client..."

StringMemorySize = StringByteLength(String$) + SizeOf(Character) ; Space for the string and its null terminating character
*CipheredString = AllocateMemory(StringMemorySize)   
*DecipheredString = AllocateMemory(StringMemorySize) 
outsize.i = StringMemorySize*3; for Base64
*str = AllocateMemory(outsize); for Base64
If AESEncoder(@String$, *CipheredString, StringByteLength(String$), @Key, 256, @InitializationVector)
	Debug "Ciphered: "+PeekS(*CipheredString)
	Debug "base64 = " + Base64EncoderBuffer(*CipheredString, StringByteLength(String$) + SizeOf(Character),*str,outsize)
	Debug "str = " + PeekS(*str,#PB_UTF8)
	Debug "str = " + PeekS(*str,#PB_Unicode)
	Debug "str = " + PeekS(*str,#PB_Ascii)
	
	Decoded$ = Space(1024)
	Debug Base64DecoderBuffer(*str,outsize,@Decoded$, StringByteLength(Decoded$))
	Debug "decoded = " + Decoded$
	
	AESDecoder(@Decoded$, *DecipheredString, StringByteLength(String$), @Key, 256, @InitializationVector)
	Debug "Deciphered: "+PeekS(*DecipheredString)
EndIf

Debug "ok"
User avatar
STARGÅTE
Addict
Addict
Posts: 2086
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: What is wrong and Base64 creates a chinese string?

Post by STARGÅTE »

PeekS(*str,#PB_UTF8) has a length as second parameter.

Code: Select all

   Debug "str = " + PeekS(*str,-1,#PB_UTF8)
   Debug "str = " + PeekS(*str,-1,#PB_Unicode)
   Debug "str = " + PeekS(*str,-1,#PB_Ascii)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
doctorized
Addict
Addict
Posts: 856
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: What is wrong and Base64 creates a chinese string?

Post by doctorized »

Oh God! I am loosing my mind! I new I was doing something stupid. Thank you very much!
Post Reply