Kali linux decode base64

How to encode and decode data in base64 and base64URL by using unix commands?

Recent versions of coreutils include basenc(1) which supports several different encodings. From its help screen:

--base64 same as 'base64' program (RFC4648 section 4) --base64url file- and url-safe base64 (RFC4648 section 5) --base32 same as 'base32' program (RFC4648 section 6) --base32hex extended hex alphabet base32 (RFC4648 section 7) --base16 hex encoding (RFC4648 section 8) --base2msbf bit string with most significant bit (msb) first --base2lsbf bit string with least significant bit (lsb) first --z85 ascii85-like encoding (ZeroMQ spec:32/Z85); when encoding, input length must be a multiple of 4; when decoding, input length must be a multiple of 5 

Here is a string that illustrates the difference:

$ printf "%s" "$s" | xxd -b -c1 | cut -d' ' -f2 | nl 1 01111000 2 01110011 3 00111111 4 00111110 5 00111110 6 00111110 

And as 6 bit blocks (as base64 reads the data):

$ printf "%s" "$s" | xxd -b -c1 | cut -d' ' -f2 | tr -d '\n' | fold -w6 | nl 1 011110 2 000111 3 001100 4 111111 5 001111 6 100011 7 111000 8 111110 

Note that block 4 and block 8 map to / and + respectively (Base64 table on Wikipedia):

This is the same suggestion as @jps but shorter. Also remember that echo by default always adds newline at the end, so when you want to encode it, you must add -n .

echo -n "Some_data_to_be_converted" | base64 | tr '/+' '_-' | tr -d '=' 

Decoding it back with bulit-in bash tools is more complicated as I didn’t find an easy way to pad the string back with ‘=’ so that the length will dividable by 4. Probably can be done with awk but I didn’t dig deep enough. If you have local ruby it becomes trivial:

2.6.2 > require 'base64' 2.6.2 > Base64.urlsafe_encode64('test', padding: false) => "dGVzdA" 2.6.2 > Base64.urlsafe_decode64('dGVzdA') => "test" 

I took it as a challenge to come up with a bash or standard unix commands to pad the string back in order to decode: stackoverflow.com/questions/58957358/…

Adding on to the answer by Kaplan Ilya, here is a command using standard linux/unix commands that can decode base64url , including handling missing padding.

Note: some versions of base64 can handle missing padding, such as Mac/BSD base64 -D . However, GNU base64 -d requires correct padding.

Also, I used the test string ~~~. instead of the one in the original question Some_data_to_be_converted , so that it will generate + , / , = characters.

text='~~~. ' # encode base64 echo "$text" | base64 # fn5+Pz8/Cg== # encode base64url base64url=$( echo "$text" | base64 | tr '/+' '_-' | tr -d '=' ) echo "$base64url" # fn5-Pz8_Cg # decode base64url echo "$base64url"==== | fold -w 4 | sed '$ d' | tr -d '\n' | tr '_-' '/+' | base64 -d # ~~~. 

Explanation of the decode base64url commands:

  • echo «$str»==== appends 4 equal signs
  • fold -w 4 split every 4 characters into separate lines
  • sed ‘$ d’ deletes the last line (the extraneous padding)
  • tr -d ‘\n’ joins all lines. Now the padding is correct.
  • tr ‘_-‘ ‘/+’ converts _ to / , — to + .
Читайте также:  Установить базу данных mysql linux

(Side note: if you’re wondering why not use tr ‘-_’ ‘+/’ since that would be in alphanumeric order, it’s because that will result in invalid option because it thinks -_ is an option. You could do tr — ‘-_’ ‘+/’ , but it’s easier just to swap the order.)

If you already have a base64 encoded string you just need to replace «+» with «-» and «/» with » _» to get a base64url encoded string. To achieve this, you can use the following command:

echo Some_data_to_be_converted | base64 | sed ‘s/+/-/g; s,/,_,g’

Base64 encoding maps the input bytes (8 bit) to a 6 bit representation. 4 base64 characters can encode 4*6=24 bits, which equals 3 bytes. Whenever the number of bytes in your input can’t be divided by 3, padding is required according to the standard.

As the = character is used for key-value pairs in URLs, you can’t use it directly for padding if you intend to use the encoded value in an URL. You can either just ommit the padding, because most implementations will still work and just ignore the 2 or 4 unused bits on the end. Or, if the receiver really needs padding, you have to replace the = by it’s URL safe representation %3d .

Источник

Tool Documentation:

Decode the given base64-encoded string ( -b “S2FsaSBMaW51eAo=” ) and display the result:

[email protected]:~# hURL -b "S2FsaSBMaW51eAo=" Original string :: S2FsaSBMaW51eAo= base64 DEcoded string :: Kali Linux 

Packages and Binaries:

hurl

This package contains a hexadecimal & URL (en/de)coder.

Installed size: 187 KB
How to install: sudo apt install hurl

hURL
[email protected]:~# hURL -h . [ hURL - hexadecimal & URL (en/de)coder ]. v2.1 @COPYLEFT -> fnord0 riseup net USAGE: /usr/bin/hURL [ -flag|--flag ] [ -f , ] [ string ] COMMAND LINE ARGUMENTS -M|--menu => Menu-driven GUI ; /usr/bin/hURL -M -U|--URL => URL encode ; /usr/bin/hURL -U "hello world" -u|--url => uRL decode ; /usr/bin/hURL -u "hello%20world" -D|--DURL => Double URL encode ; /usr/bin/hURL -D "hello world" -d|--durl => double URL decode ; /usr/bin/hURL -d "hello%2520world" -B|--BASE64 => Base64 encode ; /usr/bin/hURL -B "hello world" -b|--base64 => base64 decode ; /usr/bin/hURL -b "aGVsbG8gd29ybGQ=" -H|--HTML => HTML encode ; /usr/bin/hURL -H "" -h|--html => hTML decode ; /usr/bin/hURL -h "<hello world>" -X|--HEX => ascii -> heX ; /usr/bin/hURL -X "hello world" --esc :: output in escaped string ; "\x00\x01\x02\x03 . " --pair :: output in hexpair format ; 00010203 . -x|--hex => hex -> ascii ; /usr/bin/hURL -x "68656c6c6f20776f726c64" -I|--INT => Int -> hex ; /usr/bin/hURL -I "10" -i|--int => hex -> int ; /usr/bin/hURL -i "0xa" -n|--nint => -int -> hex ; /usr/bin/hURL -n -- -77 -N|--NHEX => -hex -> iNt ; /usr/bin/hURL -N 0xffffffb3 -T|--INTB => inT -> bin ; /usr/bin/hURL -T 30 -t|--bint => bin -> int ; /usr/bin/hURL -t 1010 -F|--FLOATH => Float -> hex ; /usr/bin/hURL -F 3.33 -l|--hfloat => hex -> float ; /usr/bin/hURL -l 0x40551ed8 -o|--octh => octal -> hex ; /usr/bin/hURL -o 35 -O|--HOCT => hex -> Octal ; /usr/bin/hURL -O 0x12 -0|--binh => bin -> hex ; /usr/bin/hURL -0 1100011 -1|--hexb => hex -> bin ; /usr/bin/hURL -1 0x63 -2|--SHA1 => SHA1 checksum ; /usr/bin/hURL -2 "hello world" -3|--SHA224 => SHA224 checksum ; /usr/bin/hURL -3 "hello world" -4|--SHA256 => SHA256 checksum ; /usr/bin/hURL -4 "hello world" -5|--SHA384 => SHA384 checksum ; /usr/bin/hURL -5 "hello world" -6|--SHA512 => SHA512 checksum ; /usr/bin/hURL -6 "hello world" -7|--ROT13 => ROT13 encode ; /usr/bin/hURL -7 "hello world" -8|--rot13 => ROT13 decode ; /usr/bin/hURL -8 "uryyb jbeyq" -9|--stack => push string 2 stack (corelan) ; /usr/bin/hURL -9 "hello world" --esc :: output in escaped string ; "\x00\x01\x02\x03 . " --pair :: output in hexpair format ; 00010203 . --ansiC :: output in C format ; 0x00, 0x01, 0x02, 0x03 . -m|--md5 => md5 digest ; /usr/bin/hURL -m "hello world" -e|--net => int -> hex (net-byte order) ; /usr/bin/hURL -e 4444 -E|--NET => hex (nEt-byte order) -> int ; /usr/bin/hURL -E 5c11 -w|--wbin => hex [file] -> binary [file] ; /usr/bin/hURL -w -f  -r|--rbin => binary [file] -> hex (corelan); /usr/bin/hURL -r -f /tmp/msgbox.bin --esc :: output in escaped string ; "\x00\x01\x02\x03 . " --pair :: output in hexpair format ; 00010203 . --ansiC :: output in C format ; 0x00, 0x01, 0x02, 0x03 . --color|--nocolor => enable/disable colored output [default is ENABLED] --corelan => display corelan reference --help => displays help --man => displays extended help with examples --version => displays version information -s => suppress (display result only) -f|--file , => use file(s) as input [string] => string as input 

Источник

Читайте также:  Adding permanent routes in linux

How to base64 encode and decode from command-line

In this tutorial, you will learn how to base64 encode and decode from the command-line on Linux. You will also learn what base64 encoding is and why it should never be used to protect data from unauthorized access.

Base64 encoding and decoding data has many use cases. One being is ensuring data integrity when transferring data over the network, while another is storing Secrets in Kubernetes.

After reading this tutorial you will understand how to easily encode files or strings, and then decode them back.

How to base64 encode on Ubuntu, Debian, OSX, and Red Hat

If you are running popular linux distributions, such as Ubuntu, Debian, CentOS, or Red Hat, the base64 command-line tool is typically pre-installed. You should not have to perform any additional steps.

OSX also comes bundled with its own version of base64.

Why Base64 Encode Data

Transferring an ASCII file over the network can cause corruption if not decoded correctly. The reason is ASCII files are string converted to bytes, and when those bytes are decoded incorrectly back to ASCII your data becomes corrupt.

Base64 was introduced as a way to convert your ASCII data into arbitrary bytes, where they could then be transferred as bytes, and decoded correctly back to ASCII.

In short, base64 encoding ensures the integrity of our data when transferred over the network.

Base64 is not Encryption

Encoding files is not encryption and should never be used to secure sensitive data on disk. Rather it is a useful way of transferring or storing large data in the form of a string.

Читайте также:  Linux home dir variable

While it may obfuscate that actual data from should surfers, anyone who has access to base64 encoded data can easily decode it.

Base64 Encoding a String

To base64 encode string you can pipe an echo command into the base64 command-line tool. To ensure no extra, hidden characters are added use the -n flag.

Without the -n flag you may capture a hidden characters, like line returns or spaces, which will corrupt your base64 encoding.

Which will output the following

Base64 Encoding a File

This will output a very long, base64 encoded string. You may want to write the stdout to file instead.

bas64 /path/to/file > output.txt

Decoding Strings

To decode with base64 you need to use the —decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it.

Using the example encoding shown above, let’s decode it back into its original form.

echo -n 'bXktc3RyaW5n' | base64 --decode

Provided your encoding was not corrupted the output should be your original string.

Decoding Files

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the —decode flag.

As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

base64 --decode /path/to/file > output.txt

Conclusion

In this tutorial, you learned how to base64 encode files and strings. This something commonly done to transfer files in such a way that it remains

Источник

Оцените статью
Adblock
detector