Search
Duplicate

bash 쉘에서 base64 인코딩 및 디코딩

로그 분석 시 평문이 아닌 base64로 인코딩 된 값을 디코딩 해야 하는 상황이 발생된다.
반대로 평문을 인코딩해야하는 경우도 있다.
이 때 구글에 'base64 encoding & decoding' 키워드로 검색을 해서
웹에 구현된 인코더 서비스를 쓸 수 있지만,
bash shell에서 base64라는 명령어를 활용하는 방법을 소개한다.

base64 명령어가 존재하는지 확인

[root@parkjw bin]# pwd /usr/bin [root@parkjw bin]# ls -trl | grep base64 -rwxr-xr-x. 1 root root 37360 Aug 20 2019 base64 [root@parkjw bin]# base64 --help Usage: base64 [OPTION]... [FILE] Base64 encode or decode FILE, or standard input, to standard output. Mandatory arguments to long options are mandatory for short options too. -d, --decode decode data -i, --ignore-garbage when decoding, ignore non-alphabet characters -w, --wrap=COLS wrap encoded lines after COLS character (default 76). Use 0 to disable line wrapping --help display this help and exit --version output version information and exit With no FILE, or when FILE is -, read standard input. The data are encoded as described for the base64 alphabet in RFC 3548. When decoding, the input may contain newlines in addition to the bytes of the formal base64 alphabet. Use --ignore-garbage to attempt to recover from any other non-alphabet bytes in the encoded stream. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> For complete documentation, run: info coreutils 'base64 invocation'
Bash
복사

base64 인코딩

echo '인코딩할 평문 문자' | base64
[root@parkjw ~]# echo 'hello world' | base64 aGVsbG8gd29ybGQK
Bash
복사

base64 디코딩

echo '디코딩할 base64 문자' | base64 --decode
[root@parkjw ~]# echo 'aGVsbG8gd29ybGQK' | base64 --decode hello world
Bash
복사