Go lang base64 Encoding/Decoding Example

by Anish

Posted on Sunday November 4 , 2018

This sample chapter extracted from the book, Go Lang Cryptography for Developers . The Book theme isCryptography is for EveryOne. Learn from Crypto Principle to Applied Cryptography With Practical Example


URL Encoding/Decoding

URLs can only be sent over the Internet using the ASCII character set, Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.

  • URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.
  • URLs cannot contain spaces.
  • URL encoding normally replaces a space with a plus (+) sign or with %20
  • In go lang the function URLEncoding is the alternate base64 encoding defined in RFC 4648. It is typically used in URLs and file names.

Reference ASCII table

Base64 Encoding/Decoding

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. Each Base64 digit represents exactly 6 bits of data. Three 8-bit bytes (i.e., a total of 24 bits) can therefore be represented by four 6-bit Base64 digits.

The Base64 index table:

Go provides built-in support for base64 encoding/decoding.

import (   
 "encoding/base64")

Go supports both standard and URL-compatible base64

For URL compatible encoding use the below function,

EncodeToString returns the base64 encoding of src.

func (enc *Encoding) EncodeToString(src []byte) string

For URL compatible Decoding use the below function

DecodeString returns the bytes represented by the base64 string s.

func (enc *Encoding) DecodeString(s string) ([]byte, error)

The complete example using URL-compatible base64

package main  
  
import (  
   "fmt"  
 "encoding/base64")  
  
func main() {  
  
   msg := "https://8gwifi.org"  
  // Base64 Encoding  
  encmess := base64.URLEncoding.EncodeToString([]byte(msg))  
  fmt.Println("Base64 Encoded Message", encmess)  
  
   // Base64 Decoding  
   decode, err := base64.URLEncoding.DecodeString(encmess)  
   
   if err != nil {  
      fmt.Println("Falied to Perfrom URL Encoding", encmess)  
      return  
  }   
   fmt.Println("Base64 Decoded Message", string(decode))  
  
}

The output

Base64 Encoded Message aHR0cHM6Ly84Z3dpZmkub3Jn
Base64 Decoded Message https://8gwifi.org

By Using StdEncoding method for base64 encode/decode.

package main  
  
import (  
   "fmt"  
 "encoding/base64")  
  
func main() {  
  
   msg := "https://8gwifi.org"  
  // Base64 Encoding  
  encmess := base64.StdEncoding.EncodeToString([]byte(msg))  
   fmt.Println("Base64 Encoded Message", encmess)  
  
   // Base64 Decoding  
  decode, err := base64.StdEncoding.DecodeString(encmess)  
  
   if err != nil {  
      fmt.Println("Falied to Perfrom URL Encoding", encmess)  
      return  
  }  
   fmt.Println("Base64 Decoded Message", string(decode))  
     
  
}

The output

Base64 Encoded Message aHR0cHM6Ly84Z3dpZmkub3Jn
Base64 Decoded Message https://8gwifi.org

Thanku for reading !!! Give a Share for Support

Asking for donation sound bad to me, so i'm raising fund from by offering all my eight book for just $9

Referefce 8gwifi.org

Online Terminals
Online Terminals
python Cryptography Topics
Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Kubernetes for DevOps

Hello Dockerfile

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers

Here