Friday, February 21, 2014

Linux : Base64 Encode & Decode with OpenSSL

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.

Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that is designed to deal with textual data. This is to ensure that the data remains intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML.

In Linux you can achieve Base64 Encoding and Decoding using pre-installed OpenSSL package.


Below command will encode any text contents within the quotes and display the encoded contents on a new line.

echo 'text content' | openssl base64

You can also encode multiple lines using below command

echo 'text content' | openssl base64 && echo 'another text content' | openssl base64


Similarly you can decode using '-d' flag as shown below

echo 'dGV4dCBjb250ZW50Cg==' | openssl base64 -d

Using the OpenSSL package, you can also encode or decode a specific file, as shown below:

encode file
openssl base64 -in 'file.txt' -out 'encodedFile.txt'

decode file
openssl base64 -d -in 'encodedFile.txt' -out 'file.txt'

No comments:

Post a Comment