Encode to Base64 a specific file by Windows Command Line
Asked Answered
D

3

7

I need to use a command line on Windows OS to generate the base64 data of a specific file on the screen (without generating a file).

I have see that on Unix system is sufficient to use

cat <file_name>| base64

to obtain the file's contents encoded as base64.

On Windows I'm not able to have the same result.

I have found this solution:

certutil -encode -f <file_name> tmp.b64 && findstr /v /c:- tmp.b64 && del tmp.b64

But this needs the system to generate a temporary file and so, at the end, go to destroy it. With just the certutil command, the result on the screen is contaminated by 3 lines which contain unrelated information.

Could someone help me to provide a command on Windows that produces only the base64 data?

UPDATE: I have improved the result on the screen, by this new version of the command:

certutil -encode -f <file_name> tmp.b64 && cls && findstr /v /c:- tmp.b64 && del tmp.b64

The result is more like my requirement, but I would like to avoid creating the temporary file tmp.b64 every time.

Driggers answered 5/1, 2021 at 10:37 Comment(0)
E
7

the usual windows way to generate a base 64 string is

Make any file (here simple text but can be PDF, docX or whatever.)

echo Hello World!>input.ext

Convert to base64

certutil -encodehex -f "input.ext" "output.txt" 0x40000001 1>nul

to see the result use

type output.txt
SGVsbG8gV29ybGQhDQo=

To send to another command use a pipe to use the TYPE output.txt as input. However like many conversion tasks it needs to be a file IO. To avoid any hint of a hard drive file, it would need a memory file system.

For looping a folder use a for loop with variables to suit

to decode output.txt

certutil -decode -f output.txt output.ext 1>nul

type output.ext

Hello World!

Answer

There is a very small (7.7 kB) downloadable exe c 2015 that can be used to encode/decode to file or console like this

b64 -e input.ext

Result on screen or use in a pipe

SGVsbG8gV29ybGQhDQo=

Note that's exactly the same as above however there has been no update as to most recent query about why it may not always work inbound as expected, but this is an outbound usage.

example of open issue

echo Hello World!|b64 -e

not an accurate result compared to above but an acceptable troughput SGVsbG8gV29ybGQhCg==

so when using

echo SGVsbG8gV29ybGQhCg==|b64 -d

It does correctly report

Hello World!

The reason for the difference is the last EOL
0000000C: 0A 0D
And depending on circumstances, may or may not be a material difference. Also note that command lines are limited in working length, so console usage is also limited for string handling too

Elbertina answered 8/6, 2023 at 21:11 Comment(1)
I didn't have openssl on the server, but this solution worked when I used this: certutil -encode <in-filename> <out-filename>Dippold
G
4

I suggest to install OpenSSL on your Windows machine. After you set the PATH variable it's easy as:

type <file_name> | openssl base64
Gabbert answered 5/1, 2021 at 11:8 Comment(4)
Hi, Unfortunatlly I haven't the possibility to install oher application on the serverDriggers
Changed to type for whatever reasonGabbert
That's pretty marginal but who knows if the wretchedness that is Windows requires it. The usual recommendation is to use redirection if the command doesn't simply accept a file name argument.Downy
But with "more" or with "type", i could see the content on a file. My idea was to does not create a temporary file, but have the base64 of a specific file like result to a command.Driggers
A
0

You can use convert command in powershell

[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((Get-Content -Path "input.file" -Raw)))

Also You can pipe the out put and write it in a file using Set-Content command if you need it.

[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((Get-Content -Path "input.file" -Raw))) | Set-Content -Path "base64.txt"
Amalia answered 6/9, 2024 at 9:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.