Windows, Linux 및 Mac에서 Base64 인코딩 - 디코딩
파일을 base64로 급하게 인코딩해야 할 때
다음은 Windows, Linux 및 Mac에서 파일을 Base64로 인코딩하고 디코딩하는 방법에 대한 예시입니다.
Windows에서 파일을 Base64로 변환하는 방법
Windows에서 일반 파일을 Base64로 변환하려면 다음 방법을 사용할 수 있습니다:
Certutil을 사용한 명령 프롬프트
-
명령 프롬프트를 열어주세요.
-
다음 명령을 실행하세요:
certutil -encode
을 파일 경로로,
을 원하는 출력 파일 이름으로 바꿔주세요 (예:encoded.b64
). -
임시 파일에 저장하지 않고 바로 화면에 Base64 내용을 표시하려면 다음 명령을 실행하세요:
certutil -encode tmp.b64 && findstr /v /c:- tmp.b64 && del tmp.b64
처리 후
tmp.b64
파일을 삭제하여 임시 파일이 남지 않도록 합니다.
PowerShell 사용
- PowerShell을 열어주세요.
- 다음 명령을 실행하세요:
[Convert]::ToBase64String([System.IO.File]::ReadAllBytes("your_file_path"))
"your_file_path"
을 파일의 전체 경로로 바꿔주세요. 이 명령은 Base64 문자열을 바로 콘솔에 출력합니다.
커스텀 스크립트 사용
더 많은 유연성을 원한다면 ConvertTo-Base64.ps1
과 같은 스크립트를 사용할 수 있습니다. 이 스크립트는 파일을 Base64로 인코딩하고, 필요에 따라 출력을 파일에 저장하거나 JSON 형식으로 포맷할 수 있습니다.
이 방법들은 추가적인 소프트웨어 설치 없이도 빠르고 효율적으로 Base64 인코딩을 수행할 수 있도록 해줍니다.
Linux에서 파일을 Base64로 변환하는 방법
Linux에서 이진 파일을 Base64로 변환하려면 base64
명령을 사용할 수 있습니다. 이 명령은 대부분의 Linux 배포판에 사전 설치되어 있습니다. 이 방법은 Windows보다 훨씬 간단하고 효율적입니다! 😊
이렇게 하세요:
기본 구문
base64 [OPTIONS] [FILE]
파일을 인코딩하는 단계
-
터미널을 열어주세요.
-
다음 명령을 실행하세요:
base64 input_file > output_file
input_file
을 파일 경로로,output_file
을 Base64 인코딩된 내용을 저장할 파일 이름으로 바꿔주세요.예시:
base64 myfile.txt > encoded.txt
-
인코딩된 내용을 바로 터미널에 표시하려면 다음 명령을 실행하세요:
base64 input_file
선택적 플래그
-w
: 지정된 문자 수만큼 줄을 감싸기 (기본값은 76).-w 0
을 사용하면 줄 감싸기 없이 표시됩니다. 예시:base64 -w 0 input_file > output_file
Base64 파일 디코딩
Base64 인코딩된 파일을 원래 형식으로 디코딩하려면 다음 명령을 실행하세요:
base64 --decode encoded_file > decoded_file
이 방법은 대부분의 Linux 배포판에서 추가 설치 없이도 잘 작동합니다.
Mac에서 Base64 인코딩/디코딩
이 방법은 Linux와 유사하지만 약간 다릅니다.
macOS에서 일반 파일을 Base64로 변환하려면 내장된 base64
명령을 사용할 수 있습니다. 방법은 다음과 같습니다:
파일을 인코딩하는 단계
-
터미널을 열어주세요.
-
다음 명령을 실행하세요:
base64 -i input_file > output_file
input_file
을 파일 경로로,output_file
을 Base64 인코딩된 내용을 저장할 파일 이름으로 바꿔주세요.예시:
base64 -i myfile.txt > encoded.txt
-
Base64 인코딩된 내용을 바로 터미널에 표시하려면 다음 명령을 실행하세요:
base64 -i input_file
클립보드 통합 (선택 사항)
파일을 인코딩한 후 결과를 바로 클립보드에 복사하려면 다음 명령을 실행하세요:
base64 -i input_file | pbcopy
Base64 파일 디코딩
Base64 인코딩된 파일을 원래 형식으로 디코딩하려면 다음 명령을 실행하세요:
base64 -D -i encoded_file > decoded_file
이 명령들은 macOS 10.7 이상의 모든 최신 버전에서 잘 작동합니다.
base64 도구의 명령줄 매개변수 전체 목록
$ base64 --help
Usage: base64 [OPTION]... [FILE]
Base64 encode or decode FILE, or standard input, to standard output.
With no FILE, or when FILE is -, read standard input.
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
The data are encoded as described for the base64 alphabet in RFC 4648.
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.
...