> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/encoding-and-decoding/basex/base64.md).

# Base64

## Convert in bash

### Base64 decode

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ echo 'VGhpcyBpcyBhIHRlc3QhCg==' | base64 -d
This is a test!
```

### Base64 encode

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ echo 'This is a test!' | base64
VGhpcyBpcyBhIHRlc3QhCg==
```

## Convert with Binary Refinery

You can convert from `base64` with [b64](https://binref.github.io/#refinery.b64) from Binary Refinery

```bash
┌──(kali㉿kali)-[~]
└─$ source ~/Python_venvs/Binary_Refinery/bin/activate

┌──(Binary_Refinery)─(kali㉿kali)-[~]
└─$ emit 'VGhpcyBpcyBhIHRlc3QhCg==' | b64            
This is a test!
```

## Convert with openssl

### Base64 decode

You can decode Base64 with `openssl enc -base64 -d`

```bash
┌──(kali㉿kali)-[~]
└─$ echo 'VGhpcyBpcyBhIHRlc3QhCg==' | openssl enc -base64 -d
This is a test!
```

### Base64 encode

```bash
┌──(kali㉿kali)-[~]
└─$ echo 'This is a test!' | openssl enc -base64
VGhpcyBpcyBhIHRlc3QhCg==
```

## Convert in PowerShell

### Base64 decode

Base64 decode string

```powershell
$Enc = 'YwB5AGIAZQByAGcAZQBkAGQAbwBuAA=='
[System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Enc))
```

### Base64 encode

Encode commands in PowerShell for use with `-EncodedCommand`

```powershell
$Text = 'Write-Host -ForegroundColor Red "This is a test!"'
```

```powershell
$Bytes = [System.Text.Encoding]::Unicode.GetBytes($Text)
$EncodedText = [Convert]::ToBase64String($Bytes)
$EncodedText
```

Then use with

```batch
powershell.exe -Enc VwByAGkAdABlAC0ASABvAHMAdAAgAC0ARgBvAHIAZQBnAHIAbwB1AG4AZABDAG8AbABvAHIAIABSAGUAZAAgACIAVABoAGkAcwAgAGkAcwAgAGEAIAB0AGUAcwB0ACEAIgA=
```

## Convert in Python

### Base64 decode

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ python                    
Python 3.11.9 (main, Apr 10 2024, 13:16:36) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> base64.b64decode(b'VGhpcyBpcyBhIHRlc3QhCg==')
b'This is a test!\n'
>>> base64.b64decode(b'VGhpcyBpcyBhIHRlc3QhCg==').decode()
'This is a test!\n'
>>> exit()
```

### Base64 encode

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ python
Python 3.11.9 (main, Apr 10 2024, 13:16:36) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> base64.b64encode(b'This is a test!')
b'VGhpcyBpcyBhIHRlc3Qh'
>>> exit()
```

## Convert with online services

You can also convert base64 with one of these online services:

Code Beautity: <https://codebeautify.org/base64-decode>

CyberChef: <https://gchq.github.io/CyberChef/#recipe=From_Base64('A-Za-z0-9%2B/%3D',true,false)>

dcode.fr: <https://www.dcode.fr/base-64-encoding>

## Resources

Binary Refinery - Documentation: <https://binref.github.io/>

Binary Refinery - GitHub: <https://github.com/binref/refinery/>

Base64 - Wikipedia: <https://en.wikipedia.org/wiki/Base64>

base64 - Linux manual page: <https://man7.org/linux/man-pages/man1/base64.1.html>

base64 module - Python: <https://docs.python.org/3/library/base64.html>

Convert.FromBase64String(String) Method - Microsoft Learn: <https://learn.microsoft.com/en-us/dotnet/api/system.convert.frombase64string?view=net-8.0>

Convert.ToBase64String Method - Microsoft Learn: <https://learn.microsoft.com/en-us/dotnet/api/system.convert.tobase64string?view=net-8.0>

Encoding.GetBytes Method - Microsoft Learn: <https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getbytes?view=net-8.0>

Encoding.GetString Method - Microsoft Learn: <https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getstring?view=net-8.0>
