Skip to main content
corrected typo
Source Link
wisbucky
  • 3.7k
  • 1
  • 34
  • 21

GNU's base64 -d requires proper padding (input length must be a multiple of 4). Other bash64base64 decoders may be smarter and not require padding (e.g. Mac/BSD bash64base64 -D does not require padding).

Here's a bash command that can automatically pad the base64 string properly. Then you won't get the "invalid input" error.

str="eyJmb28iOiJiYXIiLCJiYXoiOiJiYXQifQ"

echo "$str"==== | fold -w 4 | sed '$ d' | tr -d '\n' | base64 --decode

Explanation:

  • 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

GNU's base64 -d requires proper padding (input length must be a multiple of 4). Other bash64 decoders may be smarter and not require padding (e.g. Mac/BSD bash64 -D does not require padding).

Here's a bash command that can automatically pad the base64 string properly. Then you won't get the "invalid input" error.

str="eyJmb28iOiJiYXIiLCJiYXoiOiJiYXQifQ"

echo "$str"==== | fold -w 4 | sed '$ d' | tr -d '\n' | base64 --decode

Explanation:

  • 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

GNU's base64 -d requires proper padding (input length must be a multiple of 4). Other base64 decoders may be smarter and not require padding (e.g. Mac/BSD base64 -D does not require padding).

Here's a bash command that can automatically pad the base64 string properly. Then you won't get the "invalid input" error.

str="eyJmb28iOiJiYXIiLCJiYXoiOiJiYXQifQ"

echo "$str"==== | fold -w 4 | sed '$ d' | tr -d '\n' | base64 --decode

Explanation:

  • 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
Source Link
wisbucky
  • 3.7k
  • 1
  • 34
  • 21

GNU's base64 -d requires proper padding (input length must be a multiple of 4). Other bash64 decoders may be smarter and not require padding (e.g. Mac/BSD bash64 -D does not require padding).

Here's a bash command that can automatically pad the base64 string properly. Then you won't get the "invalid input" error.

str="eyJmb28iOiJiYXIiLCJiYXoiOiJiYXQifQ"

echo "$str"==== | fold -w 4 | sed '$ d' | tr -d '\n' | base64 --decode

Explanation:

  • 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