Golang : Tutorial on loading GOB and PEM files
Private and public key cryptography systems have enabled a vast array of security implementations such as encrypting email(PGP), ssh login and etc. In this tutorial, we will learn how to load the key and pem files created by previous tutorial on saving private and public keys
loadrsakey.go
package main
import (
"fmt"
"os"
"bufio"
"crypto/rsa"
"encoding/pem"
"encoding/gob"
)
func main () {
// Load Private Key
privatekeyfile, err := os.Open("private.key")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
decoder := gob.NewDecoder(privatekeyfile)
var privatekey rsa.PrivateKey
err = decoder.Decode(&privatekey)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
privatekeyfile.Close()
fmt.Printf("Private Key : \n%x\n", privatekey)
// Public Key
publickeyfile, err := os.Open("public.key")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
decoder = gob.NewDecoder(publickeyfile)
var publickey rsa.PublicKey
err = decoder.Decode(&publickey)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
publickeyfile.Close()
fmt.Printf("Public key : \n%x\n", publickey)
// Load PEM
pemfile, err := os.Open("private.pem")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// need to convert pemfile to []byte for decoding
pemfileinfo, _ := pemfile.Stat()
var size int64 = pemfileinfo.Size()
pembytes := make([]byte,size)
// read pemfile content into pembytes
buffer := bufio.NewReader(pemfile)
_, err = buffer.Read(pembytes)
// proper decoding now
data, _ := pem.Decode([]byte(pembytes))
pemfile.Close()
fmt.Printf("PEM Type :\n%s\n", data.Type)
fmt.Printf("PEM Headers :\n%s\n", data.Headers)
fmt.Printf("PEM Bytes :\n%x\n", string(data.Bytes))
}
Output :
Private Key :
{{bdfc7d7......b86ac5dac2 []}}
Public key :
{bdfc7d76..... 10001}
PEM Type :
RSA PRIVATE KEY
PEM Headers :
map[]
PEM Bytes :
3082025c02....ac5dac2
Reference :
https://www.socketloop.com/tutorials/golang-read-binary-file-into-memory
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.2k Golang : Web(Javascript) to server-side websocket example
+25k Golang : Convert long hexadecimal with strconv.ParseUint example
+18.7k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+8k Golang : Oanda bot with Telegram and RSI example
+15.8k Golang : Generate universally unique identifier(UUID) example
+4.9k Golang : Customize scanner.Scanner to treat dash as part of identifier
+4.3k Javascript : Detect when console is activated and do something about it
+44.4k Golang : Use wildcard patterns with filepath.Glob() example
+13.3k Golang : Strings comparison
+8k Golang : Emulate NumPy way of creating matrix example
+36.3k Golang : Converting a negative number to positive number
+18.5k Golang : Display list of time zones with GMT