Bash base64解码时忽略括号
我试图解码一个文件,它大部分是用base64编码的。我想做的是解码以下内容,同时仍然保持Bash base64解码时忽略括号,bash,encryption,base64,sh,decoding,Bash,Encryption,Base64,Sh,Decoding,我试图解码一个文件,它大部分是用base64编码的。我想做的是解码以下内容,同时仍然保持[\u*.] example.txt 有时会是这种形式 aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg== 期望输出 错误输出 我试过的 我尝试使用grep-o将[\u*.\ uquo]单独设置为base64。然后查找和 通过一种奇怪的数组排列替换它们,但我无法 让它工作起来。 将其全部建立基础,然后解码。结果为双基64行 该文件大大缩小了! 使用base64--wra
[\u*.]
example.txt
有时会是这种形式
aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
期望输出
错误输出
我试过的
我尝试使用grep-o
将[\u*.\ uquo]
单独设置为base64。然后查找和通过一种奇怪的数组排列替换它们,但我无法
让它工作起来。 将其全部建立基础,然后解码。结果为双基64行 该文件大大缩小了! 使用
base64--wrap=0
、while循环和if/else语句进行编码。
解码后,
[\u*.
仍然需要在那里。您需要一个循环来读取每一行,测试它是base64还是non-base64,并对其进行适当的处理
while read -r line
do
case "$line" in
\[*\]) echo "$line" ;;
*) base64 -d <<< "$line" ;;
esac
done << example.txt
读取时-r行
做
大写“$line”
\[*\])回显“$line”;;
*)base64-d我相信有人有比这更聪明的解决方案。但是试试这个
#! /bin/bash
MYTMP1=""
function printInlineB64()
{
local lines=($(echo $1 | 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}
我建议使用sh
以外的其他语言,但这里有一个使用cut
的解决方案。这将处理一行中有多个[\u常量]
的情况
#!/bin/bash
function decode() {
local data=""
local line=$1
while [[ -n $line ]]; do
data=$data$(echo $line | cut -d[ -f1 | base64 -d)
const=$(echo $line | cut -d[ -sf2- | cut -d] -sf1)
[[ -n $const ]] && data=$data[$const]
line=$(echo $line | cut -d] -sf2-)
done
echo "$data"
}
while read -r line; do
decode $line
done < example.txt
#/bin/bash
函数解码(){
本地数据=“”
本地线路=$1
而[[-n$line]];则
数据=$data$(回声$line | cut-d[-f1 | base64-d)
const=$(echo$行| cut-d[-sf2-| cut-d]-sf1)
[[-n$const]]&&data=$data[$const]
线条=$(回声$线条|切割-d]-sf2-)
完成
回显“$data”
}
而read-r行;做
解码$行
完成
如果Perl是一个选项,您可以这样说:
perl -MMIME::Base64 -lpe '$_ = join("", grep {/^\[/ || chomp($_ = decode_base64($_)), 1} split(/(?=\[)|(?<=\])/))' example.txt
perl-MMIME::Base64-lpe'$\[\/\[\\\[\\\]join($)、grep{/^\[/\\\[\\\]chomp($\\=decode\u Base64($))、1}split(/(?=\[)))(Base64块总是一行吗?不总是这样,有时类似于wq9cxyjjjg4qpxy/Crwo=[\u NOTBASE64ED\]aGkgdGhlcmUK
这与您在示例中发布的内容有什么不同?这是一个示例,其中同一行上的base64块由[\u*.]
分隔,如果您的意思是如果一个base64块超长而进入新行,或者如果两个base64块在同一行上没有分隔符,则不会。它似乎删除了之前的任何内容[
在wq9cXyjjg4QpXy/Crwo=[\u NOTBASE64ED\u]aGkgdGhlcmUK
示例中(我忘了在原始帖子中添加,现在添加了)。另外,您在*)的末尾忘了一个;
)
。在您添加编辑之前,我写了这个答案在一行中,<>代码>在更强大的语言(如代码> perl )中可能会更容易。
while read -r line
do
case "$line" in
\[*\]) echo "$line" ;;
*) base64 -d <<< "$line" ;;
esac
done << example.txt
#! /bin/bash
MYTMP1=""
function printInlineB64()
{
local lines=($(echo $1 | 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}
$ 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!
#!/bin/bash
function decode() {
local data=""
local line=$1
while [[ -n $line ]]; do
data=$data$(echo $line | cut -d[ -f1 | base64 -d)
const=$(echo $line | cut -d[ -sf2- | cut -d] -sf1)
[[ -n $const ]] && data=$data[$const]
line=$(echo $line | cut -d] -sf2-)
done
echo "$data"
}
while read -r line; do
decode $line
done < example.txt
perl -MMIME::Base64 -lpe '$_ = join("", grep {/^\[/ || chomp($_ = decode_base64($_)), 1} split(/(?=\[)|(?<=\])/))' example.txt
#!/bin/bash
perl -MMIME::Base64 -lpe '
@ary = split(/(?=\[)|(?<=\])/, $_);
foreach (@ary) {
if (! /^\[/) {
chomp($_ = decode_base64($_));
}
}
$_ = join("", @ary);
' example.txt