base64 decode while ignoring brackets
up vote
2
down vote
favorite
I'm trying to decode a file, which is mostly encoded with base64. What I want to do is to decode the following, while still maintaining the [_*_].
example.txt
wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==
Sometimes it'll be in this form
aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
Desired output
¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
hi there[_CONSTANT_]Hello World!
Error output
¯_(ツ)_/¯
4��!:�@�H�B�8ԓ��[��ܛBbase64: invalid input
What I've tried
base64 -di example.txt
base64 -d example.txt
base64 --wrap=0 -d -i example.txt
I tried to individually base64 the [_*_] using grep -o. Then find and
replacing them through a weird arrangement with arrays, but I couldn't
get it to work.
base64ing it all, then decoding. Results in double base64ed rows.
The file is significantly downsized!
Encoded using base64 --wrap=0, while loop, and if/else statement.
The [_*_] still need to be there after being decoded.
bash encryption base64 sh decoding
add a comment |
up vote
2
down vote
favorite
I'm trying to decode a file, which is mostly encoded with base64. What I want to do is to decode the following, while still maintaining the [_*_].
example.txt
wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==
Sometimes it'll be in this form
aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
Desired output
¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
hi there[_CONSTANT_]Hello World!
Error output
¯_(ツ)_/¯
4��!:�@�H�B�8ԓ��[��ܛBbase64: invalid input
What I've tried
base64 -di example.txt
base64 -d example.txt
base64 --wrap=0 -d -i example.txt
I tried to individually base64 the [_*_] using grep -o. Then find and
replacing them through a weird arrangement with arrays, but I couldn't
get it to work.
base64ing it all, then decoding. Results in double base64ed rows.
The file is significantly downsized!
Encoded using base64 --wrap=0, while loop, and if/else statement.
The [_*_] still need to be there after being decoded.
bash encryption base64 sh decoding
Are the base64 blocks always a single line?
– Barmar
Nov 9 at 23:47
Not always, sometimes its likewq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK
– Caucasian Malaysian
Nov 9 at 23:50
How is that different from what you posted in the example?
– Barmar
Nov 9 at 23:51
Its an example where the base64 blocks on the same line separated by the[_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.
– Caucasian Malaysian
Nov 9 at 23:54
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to decode a file, which is mostly encoded with base64. What I want to do is to decode the following, while still maintaining the [_*_].
example.txt
wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==
Sometimes it'll be in this form
aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
Desired output
¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
hi there[_CONSTANT_]Hello World!
Error output
¯_(ツ)_/¯
4��!:�@�H�B�8ԓ��[��ܛBbase64: invalid input
What I've tried
base64 -di example.txt
base64 -d example.txt
base64 --wrap=0 -d -i example.txt
I tried to individually base64 the [_*_] using grep -o. Then find and
replacing them through a weird arrangement with arrays, but I couldn't
get it to work.
base64ing it all, then decoding. Results in double base64ed rows.
The file is significantly downsized!
Encoded using base64 --wrap=0, while loop, and if/else statement.
The [_*_] still need to be there after being decoded.
bash encryption base64 sh decoding
I'm trying to decode a file, which is mostly encoded with base64. What I want to do is to decode the following, while still maintaining the [_*_].
example.txt
wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==
Sometimes it'll be in this form
aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
Desired output
¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
hi there[_CONSTANT_]Hello World!
Error output
¯_(ツ)_/¯
4��!:�@�H�B�8ԓ��[��ܛBbase64: invalid input
What I've tried
base64 -di example.txt
base64 -d example.txt
base64 --wrap=0 -d -i example.txt
I tried to individually base64 the [_*_] using grep -o. Then find and
replacing them through a weird arrangement with arrays, but I couldn't
get it to work.
base64ing it all, then decoding. Results in double base64ed rows.
The file is significantly downsized!
Encoded using base64 --wrap=0, while loop, and if/else statement.
The [_*_] still need to be there after being decoded.
bash encryption base64 sh decoding
bash encryption base64 sh decoding
edited Nov 10 at 0:13
asked Nov 9 at 23:42
Caucasian Malaysian
857
857
Are the base64 blocks always a single line?
– Barmar
Nov 9 at 23:47
Not always, sometimes its likewq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK
– Caucasian Malaysian
Nov 9 at 23:50
How is that different from what you posted in the example?
– Barmar
Nov 9 at 23:51
Its an example where the base64 blocks on the same line separated by the[_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.
– Caucasian Malaysian
Nov 9 at 23:54
add a comment |
Are the base64 blocks always a single line?
– Barmar
Nov 9 at 23:47
Not always, sometimes its likewq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK
– Caucasian Malaysian
Nov 9 at 23:50
How is that different from what you posted in the example?
– Barmar
Nov 9 at 23:51
Its an example where the base64 blocks on the same line separated by the[_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.
– Caucasian Malaysian
Nov 9 at 23:54
Are the base64 blocks always a single line?
– Barmar
Nov 9 at 23:47
Are the base64 blocks always a single line?
– Barmar
Nov 9 at 23:47
Not always, sometimes its like
wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK– Caucasian Malaysian
Nov 9 at 23:50
Not always, sometimes its like
wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK– Caucasian Malaysian
Nov 9 at 23:50
How is that different from what you posted in the example?
– Barmar
Nov 9 at 23:51
How is that different from what you posted in the example?
– Barmar
Nov 9 at 23:51
Its an example where the base64 blocks on the same line separated by the
[_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.– Caucasian Malaysian
Nov 9 at 23:54
Its an example where the base64 blocks on the same line separated by the
[_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.– Caucasian Malaysian
Nov 9 at 23:54
add a comment |
4 Answers
4
active
oldest
votes
up vote
0
down vote
accepted
I am sure someone has a more clever solution than this. But try this
#! /bin/bash
MYTMP1=""
function printInlineB64()
sed -e 's/[/n[/g' -e 's/]/]n/g'))
OUTPUT=""
for line in "$lines[@]"; do
MYTMP1=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
OUTPUT="$OUTPUT$line"
else
OUTPUT="$OUTPUT$MYTMP1"
fi;
done
echo "$OUTPUT"
MYTMP2=""
function printB64Line()
local line=$1
# not fully base64 line
if [[ ! "$line" =~ ^[A-Za-z0-9+/=]+$ ]]; then
printInlineB64 "$line"
return
fi;
# likely base64 line
MYTMP2=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
echo $line
else
echo $MYTMP2
fi;
FILE=$1
if [ -z "$FILE" ]; then
echo "Please give a file name in argument"
exit 1;
fi;
while read line; do
printB64Line "$line"
done < $FILE
and here is output
$ cat example.txt && echo "==========================" && ./base64.sh example.txt
wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==
==========================
¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
$ cat example2.txt && echo "==========================" && ./base64.sh example2.txt
aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
==========================
hi there[_CONSTANT_]Hello World!
add a comment |
up vote
0
down vote
You need a loop that reads each line and tests whether it's base64 or non-base64, and processes it appropriately.
while read -r line
do
case "$line" in
[*]) echo "$line" ;;
*) base64 -d <<< "$line" ;;
esac
done << example.txt
It seems to delete, anything before the[in thewq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUKexample (which I forgot to add in original post, now its added). Also you forgot a;;at the end of*).
– Caucasian Malaysian
Nov 10 at 0:12
I wrote this answer before you added the edit with[_NOTBASE64_]in the middle of a line.
– Barmar
Nov 10 at 0:59
1
This would probably be easier in a more powerful language likeperl.
– Barmar
Nov 10 at 1:00
add a comment |
up vote
0
down vote
I would suggest using other languages other than sh but here is a solution using cut. This would handle the case where there are more than one [_constant_] in a line.
#!/bin/bash
function decode() base64 -d)
const=$(echo $line
while read -r line; do
decode $line
done < example.txt
add a comment |
up vote
0
down vote
If Perl is an option, you can say something like:
perl -MMIME::Base64 -lpe '$_ = join("", grep chomp($_ = decode_base64($_)), 1 split(/(?=[)|(?<=])/))' example.txt
The code below is equivalent to the above but is broken down into steps for the explanation purpose:
#!/bin/bash
perl -MMIME::Base64 -lpe '
@ary = split(/(?=[)|(?<=])/, $_);
foreach (@ary)
if (! /^[/)
chomp($_ = decode_base64($_));
$_ = join("", @ary);
' example.txt
-MMIME::Base64option loads the base64 codec module.-lpeoption makes Perl bahave like AWK to loop over input lines and implicitly handle newlines.- The regular expression
(?=[)|(?<=])matches the boundary between the base64 block and the maintaining block surrounded by [...]. - The
splitfunction divides the line into blocks on the boundary and store them in an array. - Then loop over the array and decode the base64-encoded entry if found.
- Finally merge the substring blocks into a line to print.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I am sure someone has a more clever solution than this. But try this
#! /bin/bash
MYTMP1=""
function printInlineB64()
sed -e 's/[/n[/g' -e 's/]/]n/g'))
OUTPUT=""
for line in "$lines[@]"; do
MYTMP1=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
OUTPUT="$OUTPUT$line"
else
OUTPUT="$OUTPUT$MYTMP1"
fi;
done
echo "$OUTPUT"
MYTMP2=""
function printB64Line()
local line=$1
# not fully base64 line
if [[ ! "$line" =~ ^[A-Za-z0-9+/=]+$ ]]; then
printInlineB64 "$line"
return
fi;
# likely base64 line
MYTMP2=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
echo $line
else
echo $MYTMP2
fi;
FILE=$1
if [ -z "$FILE" ]; then
echo "Please give a file name in argument"
exit 1;
fi;
while read line; do
printB64Line "$line"
done < $FILE
and here is output
$ cat example.txt && echo "==========================" && ./base64.sh example.txt
wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==
==========================
¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
$ cat example2.txt && echo "==========================" && ./base64.sh example2.txt
aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
==========================
hi there[_CONSTANT_]Hello World!
add a comment |
up vote
0
down vote
accepted
I am sure someone has a more clever solution than this. But try this
#! /bin/bash
MYTMP1=""
function printInlineB64()
sed -e 's/[/n[/g' -e 's/]/]n/g'))
OUTPUT=""
for line in "$lines[@]"; do
MYTMP1=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
OUTPUT="$OUTPUT$line"
else
OUTPUT="$OUTPUT$MYTMP1"
fi;
done
echo "$OUTPUT"
MYTMP2=""
function printB64Line()
local line=$1
# not fully base64 line
if [[ ! "$line" =~ ^[A-Za-z0-9+/=]+$ ]]; then
printInlineB64 "$line"
return
fi;
# likely base64 line
MYTMP2=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
echo $line
else
echo $MYTMP2
fi;
FILE=$1
if [ -z "$FILE" ]; then
echo "Please give a file name in argument"
exit 1;
fi;
while read line; do
printB64Line "$line"
done < $FILE
and here is output
$ cat example.txt && echo "==========================" && ./base64.sh example.txt
wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==
==========================
¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
$ cat example2.txt && echo "==========================" && ./base64.sh example2.txt
aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
==========================
hi there[_CONSTANT_]Hello World!
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I am sure someone has a more clever solution than this. But try this
#! /bin/bash
MYTMP1=""
function printInlineB64()
sed -e 's/[/n[/g' -e 's/]/]n/g'))
OUTPUT=""
for line in "$lines[@]"; do
MYTMP1=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
OUTPUT="$OUTPUT$line"
else
OUTPUT="$OUTPUT$MYTMP1"
fi;
done
echo "$OUTPUT"
MYTMP2=""
function printB64Line()
local line=$1
# not fully base64 line
if [[ ! "$line" =~ ^[A-Za-z0-9+/=]+$ ]]; then
printInlineB64 "$line"
return
fi;
# likely base64 line
MYTMP2=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
echo $line
else
echo $MYTMP2
fi;
FILE=$1
if [ -z "$FILE" ]; then
echo "Please give a file name in argument"
exit 1;
fi;
while read line; do
printB64Line "$line"
done < $FILE
and here is output
$ cat example.txt && echo "==========================" && ./base64.sh example.txt
wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==
==========================
¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
$ cat example2.txt && echo "==========================" && ./base64.sh example2.txt
aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
==========================
hi there[_CONSTANT_]Hello World!
I am sure someone has a more clever solution than this. But try this
#! /bin/bash
MYTMP1=""
function printInlineB64()
sed -e 's/[/n[/g' -e 's/]/]n/g'))
OUTPUT=""
for line in "$lines[@]"; do
MYTMP1=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
OUTPUT="$OUTPUT$line"
else
OUTPUT="$OUTPUT$MYTMP1"
fi;
done
echo "$OUTPUT"
MYTMP2=""
function printB64Line()
local line=$1
# not fully base64 line
if [[ ! "$line" =~ ^[A-Za-z0-9+/=]+$ ]]; then
printInlineB64 "$line"
return
fi;
# likely base64 line
MYTMP2=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
echo $line
else
echo $MYTMP2
fi;
FILE=$1
if [ -z "$FILE" ]; then
echo "Please give a file name in argument"
exit 1;
fi;
while read line; do
printB64Line "$line"
done < $FILE
and here is output
$ cat example.txt && echo "==========================" && ./base64.sh example.txt
wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==
==========================
¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
$ cat example2.txt && echo "==========================" && ./base64.sh example2.txt
aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
==========================
hi there[_CONSTANT_]Hello World!
edited Nov 10 at 1:50
answered Nov 10 at 1:43
kainr2
616
616
add a comment |
add a comment |
up vote
0
down vote
You need a loop that reads each line and tests whether it's base64 or non-base64, and processes it appropriately.
while read -r line
do
case "$line" in
[*]) echo "$line" ;;
*) base64 -d <<< "$line" ;;
esac
done << example.txt
It seems to delete, anything before the[in thewq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUKexample (which I forgot to add in original post, now its added). Also you forgot a;;at the end of*).
– Caucasian Malaysian
Nov 10 at 0:12
I wrote this answer before you added the edit with[_NOTBASE64_]in the middle of a line.
– Barmar
Nov 10 at 0:59
1
This would probably be easier in a more powerful language likeperl.
– Barmar
Nov 10 at 1:00
add a comment |
up vote
0
down vote
You need a loop that reads each line and tests whether it's base64 or non-base64, and processes it appropriately.
while read -r line
do
case "$line" in
[*]) echo "$line" ;;
*) base64 -d <<< "$line" ;;
esac
done << example.txt
It seems to delete, anything before the[in thewq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUKexample (which I forgot to add in original post, now its added). Also you forgot a;;at the end of*).
– Caucasian Malaysian
Nov 10 at 0:12
I wrote this answer before you added the edit with[_NOTBASE64_]in the middle of a line.
– Barmar
Nov 10 at 0:59
1
This would probably be easier in a more powerful language likeperl.
– Barmar
Nov 10 at 1:00
add a comment |
up vote
0
down vote
up vote
0
down vote
You need a loop that reads each line and tests whether it's base64 or non-base64, and processes it appropriately.
while read -r line
do
case "$line" in
[*]) echo "$line" ;;
*) base64 -d <<< "$line" ;;
esac
done << example.txt
You need a loop that reads each line and tests whether it's base64 or non-base64, and processes it appropriately.
while read -r line
do
case "$line" in
[*]) echo "$line" ;;
*) base64 -d <<< "$line" ;;
esac
done << example.txt
edited Nov 10 at 0:57
answered Nov 9 at 23:54
Barmar
413k34238339
413k34238339
It seems to delete, anything before the[in thewq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUKexample (which I forgot to add in original post, now its added). Also you forgot a;;at the end of*).
– Caucasian Malaysian
Nov 10 at 0:12
I wrote this answer before you added the edit with[_NOTBASE64_]in the middle of a line.
– Barmar
Nov 10 at 0:59
1
This would probably be easier in a more powerful language likeperl.
– Barmar
Nov 10 at 1:00
add a comment |
It seems to delete, anything before the[in thewq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUKexample (which I forgot to add in original post, now its added). Also you forgot a;;at the end of*).
– Caucasian Malaysian
Nov 10 at 0:12
I wrote this answer before you added the edit with[_NOTBASE64_]in the middle of a line.
– Barmar
Nov 10 at 0:59
1
This would probably be easier in a more powerful language likeperl.
– Barmar
Nov 10 at 1:00
It seems to delete, anything before the
[ in the wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK example (which I forgot to add in original post, now its added). Also you forgot a ;; at the end of *).– Caucasian Malaysian
Nov 10 at 0:12
It seems to delete, anything before the
[ in the wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK example (which I forgot to add in original post, now its added). Also you forgot a ;; at the end of *).– Caucasian Malaysian
Nov 10 at 0:12
I wrote this answer before you added the edit with
[_NOTBASE64_] in the middle of a line.– Barmar
Nov 10 at 0:59
I wrote this answer before you added the edit with
[_NOTBASE64_] in the middle of a line.– Barmar
Nov 10 at 0:59
1
1
This would probably be easier in a more powerful language like
perl.– Barmar
Nov 10 at 1:00
This would probably be easier in a more powerful language like
perl.– Barmar
Nov 10 at 1:00
add a comment |
up vote
0
down vote
I would suggest using other languages other than sh but here is a solution using cut. This would handle the case where there are more than one [_constant_] in a line.
#!/bin/bash
function decode() base64 -d)
const=$(echo $line
while read -r line; do
decode $line
done < example.txt
add a comment |
up vote
0
down vote
I would suggest using other languages other than sh but here is a solution using cut. This would handle the case where there are more than one [_constant_] in a line.
#!/bin/bash
function decode() base64 -d)
const=$(echo $line
while read -r line; do
decode $line
done < example.txt
add a comment |
up vote
0
down vote
up vote
0
down vote
I would suggest using other languages other than sh but here is a solution using cut. This would handle the case where there are more than one [_constant_] in a line.
#!/bin/bash
function decode() base64 -d)
const=$(echo $line
while read -r line; do
decode $line
done < example.txt
I would suggest using other languages other than sh but here is a solution using cut. This would handle the case where there are more than one [_constant_] in a line.
#!/bin/bash
function decode() base64 -d)
const=$(echo $line
while read -r line; do
decode $line
done < example.txt
edited Nov 10 at 7:31
answered Nov 10 at 5:55
ssemilla
2,537423
2,537423
add a comment |
add a comment |
up vote
0
down vote
If Perl is an option, you can say something like:
perl -MMIME::Base64 -lpe '$_ = join("", grep chomp($_ = decode_base64($_)), 1 split(/(?=[)|(?<=])/))' example.txt
The code below is equivalent to the above but is broken down into steps for the explanation purpose:
#!/bin/bash
perl -MMIME::Base64 -lpe '
@ary = split(/(?=[)|(?<=])/, $_);
foreach (@ary)
if (! /^[/)
chomp($_ = decode_base64($_));
$_ = join("", @ary);
' example.txt
-MMIME::Base64option loads the base64 codec module.-lpeoption makes Perl bahave like AWK to loop over input lines and implicitly handle newlines.- The regular expression
(?=[)|(?<=])matches the boundary between the base64 block and the maintaining block surrounded by [...]. - The
splitfunction divides the line into blocks on the boundary and store them in an array. - Then loop over the array and decode the base64-encoded entry if found.
- Finally merge the substring blocks into a line to print.
add a comment |
up vote
0
down vote
If Perl is an option, you can say something like:
perl -MMIME::Base64 -lpe '$_ = join("", grep chomp($_ = decode_base64($_)), 1 split(/(?=[)|(?<=])/))' example.txt
The code below is equivalent to the above but is broken down into steps for the explanation purpose:
#!/bin/bash
perl -MMIME::Base64 -lpe '
@ary = split(/(?=[)|(?<=])/, $_);
foreach (@ary)
if (! /^[/)
chomp($_ = decode_base64($_));
$_ = join("", @ary);
' example.txt
-MMIME::Base64option loads the base64 codec module.-lpeoption makes Perl bahave like AWK to loop over input lines and implicitly handle newlines.- The regular expression
(?=[)|(?<=])matches the boundary between the base64 block and the maintaining block surrounded by [...]. - The
splitfunction divides the line into blocks on the boundary and store them in an array. - Then loop over the array and decode the base64-encoded entry if found.
- Finally merge the substring blocks into a line to print.
add a comment |
up vote
0
down vote
up vote
0
down vote
If Perl is an option, you can say something like:
perl -MMIME::Base64 -lpe '$_ = join("", grep chomp($_ = decode_base64($_)), 1 split(/(?=[)|(?<=])/))' example.txt
The code below is equivalent to the above but is broken down into steps for the explanation purpose:
#!/bin/bash
perl -MMIME::Base64 -lpe '
@ary = split(/(?=[)|(?<=])/, $_);
foreach (@ary)
if (! /^[/)
chomp($_ = decode_base64($_));
$_ = join("", @ary);
' example.txt
-MMIME::Base64option loads the base64 codec module.-lpeoption makes Perl bahave like AWK to loop over input lines and implicitly handle newlines.- The regular expression
(?=[)|(?<=])matches the boundary between the base64 block and the maintaining block surrounded by [...]. - The
splitfunction divides the line into blocks on the boundary and store them in an array. - Then loop over the array and decode the base64-encoded entry if found.
- Finally merge the substring blocks into a line to print.
If Perl is an option, you can say something like:
perl -MMIME::Base64 -lpe '$_ = join("", grep chomp($_ = decode_base64($_)), 1 split(/(?=[)|(?<=])/))' example.txt
The code below is equivalent to the above but is broken down into steps for the explanation purpose:
#!/bin/bash
perl -MMIME::Base64 -lpe '
@ary = split(/(?=[)|(?<=])/, $_);
foreach (@ary)
if (! /^[/)
chomp($_ = decode_base64($_));
$_ = join("", @ary);
' example.txt
-MMIME::Base64option loads the base64 codec module.-lpeoption makes Perl bahave like AWK to loop over input lines and implicitly handle newlines.- The regular expression
(?=[)|(?<=])matches the boundary between the base64 block and the maintaining block surrounded by [...]. - The
splitfunction divides the line into blocks on the boundary and store them in an array. - Then loop over the array and decode the base64-encoded entry if found.
- Finally merge the substring blocks into a line to print.
answered Nov 12 at 0:22
tshiono
1,443134
1,443134
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234657%2fbase64-decode-while-ignoring-brackets%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are the base64 blocks always a single line?
– Barmar
Nov 9 at 23:47
Not always, sometimes its like
wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK– Caucasian Malaysian
Nov 9 at 23:50
How is that different from what you posted in the example?
– Barmar
Nov 9 at 23:51
Its an example where the base64 blocks on the same line separated by the
[_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.– Caucasian Malaysian
Nov 9 at 23:54