{"id":1458,"date":"2023-12-31T00:00:00","date_gmt":"2023-12-31T05:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1458"},"modified":"2023-12-13T14:10:12","modified_gmt":"2023-12-13T19:10:12","slug":"bash-how-to-decode-base64","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/bash-how-to-decode-base64\/","title":{"rendered":"bash: How to Decode base64"},"content":{"rendered":"\n<p>Base64 encoding is a widely used technique for transforming binary data into a text-based format, and decoding Base64 strings is a common task in scripting and programming. In this guide, we&#8217;ll explore how to decode Base64-encoded strings in Bash, a scripting language prevalent in Unix-like systems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Decoding Base64 in Bash:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Using the <code>base64<\/code> Command:<\/h3>\n\n\n\n<p>The <code>base64<\/code> command-line tool is often available on Unix-like systems and provides a straightforward way to decode Base64 strings.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\r\n\r\n# Base64-encoded string\r\nencoded_string=\"SGVsbG8gd29ybGQhCg==\"\r\n\r\n# Decoding the Base64-encoded string\r\ndecoded_string=$(echo \"$encoded_string\" | base64 --decode)\r\n\r\n# Display the decoded string\r\necho \"Decoded String: $decoded_string\"\r<\/code><\/pre>\n\n\n\n<p>In this script:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Replace the <code>encoded_string<\/code> variable value with your actual Base64-encoded string.<\/li>\n\n\n\n<li>The <code>echo \"$encoded_string\" | base64 --decode<\/code> command decodes the Base64-encoded string.<\/li>\n\n\n\n<li>The decoded string is stored in the <code>decoded_string<\/code> variable.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. Using the <code>openssl<\/code> Command:<\/h3>\n\n\n\n<p>If the <code>base64<\/code> tool is not available, you can use the <code>openssl<\/code> command as an alternative:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\r\n\r\n# Base64-encoded string\r\nencoded_string=\"SGVsbG8gd29ybGQhCg==\"\r\n\r\n# Decoding the Base64-encoded string using openssl\r\ndecoded_string=$(echo \"$encoded_string\" | openssl base64 -d)\r\n\r\n# Display the decoded string\r\necho \"Decoded String: $decoded_string\"\r<\/code><\/pre>\n\n\n\n<p>This script achieves the same decoding operation using the <code>openssl<\/code> command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Usage:<\/h2>\n\n\n\n<p>Understanding how to decode Base64 strings in Bash is essential for various scenarios, including decoding authentication tokens, processing binary data, and handling encoded content in scripts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example Use Case: Decoding a JWT Token:<\/h3>\n\n\n\n<p>Consider a scenario where you have a Base64-encoded JSON Web Token (JWT) and need to extract information from it. You can use Bash to decode the token and retrieve the payload.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\r\n\r\n# Base64-encoded JWT token\r\njwt_token=\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\"\r\n\r\n# Decoding the JWT token payload\r\njwt_payload=$(echo \"$jwt_token\" | cut -d'.' -f2 | base64 --decode)\r\n\r\n# Display the decoded payload\r\necho \"Decoded JWT Payload: $jwt_payload\"\r<\/code><\/pre>\n\n\n\n<p>This script uses the <code>cut<\/code> command to extract the second part of the JWT (the payload), which is then Base64-decoded.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion:<\/h2>\n\n\n\n<p>Decoding Base64 strings in Bash is a fundamental skill for any script developer. Whether you&#8217;re working with authentication tokens, handling binary data, or parsing encoded content, understanding the decoding process is crucial.<\/p>\n\n\n\n<p>In this guide, we&#8217;ve explored two methods: using the <code>base64<\/code> command-line tool and the <code>openssl<\/code> command. Armed with this knowledge, you&#8217;re well-equipped to decode Base64 strings in various scenarios, enhancing your scripting capabilities.<\/p>\n\n\n\n<p>Happy scripting!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Base64 encoding is a widely used technique for transforming binary data into a text-based format, and decoding Base64 strings is a common task in scripting and programming. In this guide, we&#8217;ll explore how to decode Base64-encoded strings in Bash, a scripting language prevalent in Unix-like systems. Decoding Base64 in Bash: 1. Using the base64 Command: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,34],"tags":[94,474,35],"class_list":["post-1458","post","type-post","status-publish","format-standard","hentry","category-bash","category-linux","tag-bash","tag-decoding-base64","tag-linux"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1458"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1458\/revisions"}],"predecessor-version":[{"id":1459,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1458\/revisions\/1459"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}