Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

How can i make base64 encode a file multiple times and output it in a file?(bash)

Currently this is my working code. How do I make this better with some output file and file appending. Example output the base64 encode text to sample1.txt, sample2.txt and so on..

Any help is much appreciated.

#!/bin/bash

state=$(<myfiletoencode)
for i in {1..5}; do
   state=$(<<<"$state")
done
echo "$state"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understood you correctly, you want to have

base64 file > sample1.txt 
base64 file | base64 > sample2.txt
base64 file | base64 | base64 > sample3.txt
...

Since you want to have a file for each step, you don't need variables. Just encode the last file to generate the next one:

#!/bin/bash
ln myfiletoencode sample0.txt
for i in {1..5}; do
   base64 "sample$((i-1)).txt" > "sample$i.txt"
done
rm sample0.txt

Keep in mind that equivalent base64 encodings can be formatted differently. You can add linebreaks whenever you like. Example:

$ seq 23 | base64
MQoyCjMKNAo1CjYKNwo4CjkKMTAKMTEKMTIKMTMKMTQKMTUKMTYKMTcKMTgKMTkKMjAKMjEKMjIK
MjMK
$ seq 23 | base64 -w0
MQoyCjMKNAo1CjYKNwo4CjkKMTAKMTEKMTIKMTMKMTQKMTUKMTYKMTcKMTgKMTkKMjAKMjEKMjIKMjMK

When you encode this encoding again you get different encodings depending on the formatting (note the different length and suffix wpNak1LCg== != 01qTUs=):

$ seq 23 | base64 | base64
TVFveUNqTUtOQW8xQ2pZS053bzRDamtLTVRBS01URUtNVElLTVRNS01UUUtNVFVLTVRZS01UY0tN
VGdLTVRrS01qQUtNakVLTWpJSwpNak1LCg==
$ seq 23 | base64 -w0 | base64
TVFveUNqTUtOQW8xQ2pZS053bzRDamtLTVRBS01URUtNVElLTVRNS01UUUtNVFVLTVRZS01UY0tN
VGdLTVRrS01qQUtNakVLTWpJS01qTUs=

Therefore I'd suggest do disable all linebreaks in the output using base64 -w0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...