Jump to content

Help with inserting Null in a Base64 string


Recommended Posts

I am really having a headache with this one... :(

; #FUNCTION# ====================================================================================================================
; Name ..........: _Base64_Encode
; Description ...: Encode the $vData in Base64
; Syntax ........: _Base64_Encode($vData)
; Parameters ....: $vData               - $vData to Encode.
; Return values .: Success: Base64 encoded $vData in the form of a string.
;                  Failure: False and @error set to:
;                           1 - If "error calculating the length of the buffer needed"
;                           2 - If "error encoding"
; Author ........: trancexx
; Modified ......: Damon Harris (TheDcoder)
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Base64_Encode($vData)
    $vData = Binary($vData)
    Local $tByteStruct = DllStructCreate("byte[" & BinaryLen($vData) & "]")
    DllStructSetData($tByteStruct, 1, $vData)
    Local $tIntStruct = DllStructCreate("int")
    Local $aDllCall = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _
            "ptr", DllStructGetPtr($tByteStruct), _
            "int", DllStructGetSize($tByteStruct), _
            "int", 1, _
            "ptr", 0, _
            "ptr", DllStructGetPtr($tIntStruct))
    If @error Or Not $aDllCall[0] Then
        Return SetError(1, 0, False) ; error calculating the length of the buffer needed
    EndIf
    Local $tCharStruct = DllStructCreate("char[" & DllStructGetData($tIntStruct, 1) & "]")
    $aDllCall = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _
            "ptr", DllStructGetPtr($tByteStruct), _
            "int", DllStructGetSize($tByteStruct), _
            "int", 1, _
            "ptr", DllStructGetPtr($tCharStruct), _
            "ptr", DllStructGetPtr($tIntStruct))
    If @error Or Not $aDllCall[0] Then
        Return SetError(2, 0, False) ; error encoding
    EndIf
    Return DllStructGetData($tCharStruct, 1)
EndFunc

ConsoleWrite(_Base64_Encode("jilles" & Null & "jilles" & Null & "sesame")) ; It should be "amlsbGVzAGppbGxlcwBzZXNhbWU="
Sleep(1000)

:mad2::wacko2::ranting:

 

Thanks in Advance, TD.

Edited by TheDcoder
Changed Title

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Developers

I see you started a second topic with "the Null" question. In C++ any regular string is terminated with a Null character, as explained in the other thread.
This means with the example you posted I guess the only characters encoded to Base64 are the characters before the first Null?

So what is it you are really trying here as a string in general never contains a Null character, only Binary data does?

Jos

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@Jos I am aware, but I was confused while deciding the Topic Title, will correct it...

But this didn't work either:

ConsoleWrite(_Base64_Encode("jilles" & Binary(Null) & "jilles" & Binary(Null) & "sesame"))

 

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Developers

Did you read my whole post and do you have any questions after reading it? Also try answering the questions asked. ;)
Might help if you first understand things before trying other similar things that wil also fail!

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

NULL in AutoIt like a null ptr not null character.

 

YOu should do this

_Base64_Encode("jilles" & chr(0) & "jilles" & chr(0) & "sesame")

 

Saludos

 

Link to comment
Share on other sites

@Danyfirex Its "amlsbGVzAGppbGxlcwBzZXNhbWU=", that is not what I wanted...

@Jos Thanks for locking the topic, still reading your previous posts.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

17 minutes ago, Jos said:

This means with the example you posted I guess the only characters encoded to Base64 are the characters before the first Null?

No, all the characters are encoded in Base64. (expect the Null)

 

I also changed the topic :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Developers

Let me see I understand this: So you want all characters except the Asc(0) characters encoded with Base64 and have leave the Asc(0) characters as is?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Just now, Jos said:

So you want all characters except the Asc(0) characters encoded with Base64 and have leave the Asc(0) characters as is?

No, that is not what I want... What I want is:

jilles<NULL>jilles<NULL>sesame

encoded in Base64...

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Developers
3 minutes ago, TheDcoder said:

 I want it to be exactly "amlsbGVzAGppbGxlcwBzZXNhbWU="

Try this:

ConsoleWrite(_Base64_Encode("jilles" & chr(0) & "jilles" & Chr(0) & "sesame")) ; It should be "amlsbGVzAGppbGxlcwBzZXNhbWU="

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@Jos

ConsoleWrite(_Base64_Encode("jilles" & chr(0) & "jilles" & Chr(0) & "sesame") = 'amlsbGVzAGppbGxlcwBzZXNhbWU=')

 

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Developers
$x = _Base64_Encode("jilles" & chr(0) & "jilles" & Chr(0) & "sesame")
$x = StringReplace($x,@crlf,"")
ConsoleWrite($x & ":" & stringLen($x) & @CRLF) ; It should be "amlsbGVzAGppbGxlcwBzZXNhbWU="

if $x == 'amlsbGVzAGppbGxlcwBzZXNhbWU=' then
    ConsoleWrite("Yes!!!!" & @CRLF)
Else
    ConsoleWrite("No!!!!" & @CRLF)
EndIf

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

First thought my glasses weren't functioning properly as the consolewrite showed the correct string, but the additional @crlf was making your test fail.
Not sure why that is returned by the UDF.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I apologize for a typo I made in the other thread: I typed Asc(0) but meant Chr(0). Can't edit it after lockdown.  (Jos did it, thanks)

@TheDcoder

You really should start using NUL for the ASCII control char 0x00 and Null for the AutoIt (and SQL) "I don't know" keyword.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...