Skip to content

Instantly share code, notes, and snippets.

@amlwwalker
Created July 25, 2021 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amlwwalker/9ca0c940938fd45c26e31d80eb846dca to your computer and use it in GitHub Desktop.
Save amlwwalker/9ca0c940938fd45c26e31d80eb846dca to your computer and use it in GitHub Desktop.
Encrypting and decrypting with Go
package encryption
import (
"bytes"
"crypto"
"fmt"
"github.com/alokmenghrajani/gpgeez"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
"gorm.io/gorm"
"io/ioutil"
"log"
"strings"
)
type Encryption struct {
gorm.Model
KeyName string `gorm:"unique"`
PublicKey string
PrivateKey string
FingerPrint []byte
Selected bool
}
// SaneDefaultConfig is a more secure default config than the defaults.
func ParanoidDefaultConfig() *packet.Config {
return &packet.Config{
DefaultHash: crypto.SHA384,
DefaultCipher: packet.CipherAES256,
DefaultCompressionAlgo: packet.CompressionZLIB,
CompressionConfig: &packet.CompressionConfig{-1},
}
}
func ReadPGP(publicKey string) (string, []byte, error) {
log.Printf("public key %s\r\n", publicKey)
//keyReader := bytes.NewReader(armoredKey.Bytes())
entityList, err := openpgp.ReadArmoredKeyRing(strings.NewReader(publicKey))
if err != nil {
return "", nil, err
}
firstSubKey := entityList[0]
//uid := ""
for k, _ := range firstSubKey.Identities {
fingerPrint := firstSubKey.PrimaryKey.Fingerprint[:]
fmt.Println("key primary key fingerprint", fingerPrint)
return k, fingerPrint, nil
}
return "", nil, nil
}
// NewEntity creates a new entity. It doesn't provide an option for comments.
func NewEntity(name, email string) (string, string, error) {
config := gpgeez.Config{}
key, err := gpgeez.CreateKey(name, "", email, &config)
if err != nil {
fmt.Printf("Something went wrong: %v", err)
return "", "", err
}
publicOutput, err := key.Armor()
if err != nil {
fmt.Printf("Something went wrong: %v", err)
return "", "", err
}
fmt.Printf("%s\n", publicOutput)
privOutput, err := key.ArmorPrivate(&config)
if err != nil {
fmt.Printf("Something went wrong: %v", err)
return "", "", err
}
fmt.Printf("%s\n", privOutput)
return privOutput, publicOutput, nil
}
func EncryptMessage(publicKey, message string) (string, error) {
fmt.Printf("found key %+v", publicKey)
pubEl, err := openpgp.ReadArmoredKeyRing(strings.NewReader(publicKey))
if err != nil {
return "", fmt.Errorf("Error Reading Public Keyring: %v", err)
}
for _, v := range pubEl {
fmt.Printf("key by id %+v\r\n", v.Identities)
}
buf := new(bytes.Buffer)
encoderWriter, err := armor.Encode(buf, "PGP Message", make(map[string]string))
if err != nil {
return "", fmt.Errorf("Error creating OpenPGP armor: %v", err)
}
encryptorWriter, err := openpgp.Encrypt(encoderWriter, pubEl, nil, nil, nil)
if err != nil {
return "", fmt.Errorf("Error Encrypting Message: %v", err)
}
_, err = encryptorWriter.Write([]byte(message))
if err != nil {
return "", fmt.Errorf("Error writing data to encoder: %v", err)
}
encryptorWriter.Close()
encoderWriter.Close()
fmt.Println(string(buf.Bytes()))
return string(buf.Bytes()), nil
}
func DecryptMessage(privateKey, privateKeyPassword, message string) (string, error) {
el, err := openpgp.ReadArmoredKeyRing(strings.NewReader(privateKey))
if err != nil {
return "", fmt.Errorf("Error Reading Keyring: %v", err)
}
entity := el[0]
err = entity.PrivateKey.Decrypt([]byte(privateKeyPassword))
if err != nil {
return "", fmt.Errorf("Error decrypting PrivateKey: %v", err)
}
for _, subkey := range entity.Subkeys {
err = subkey.PrivateKey.Decrypt([]byte(privateKeyPassword))
if err != nil {
return "", fmt.Errorf("Error decrypting PrivateKey: %v", err)
}
}
block, err := armor.Decode(strings.NewReader(message))
if err != nil {
return "", fmt.Errorf("Error decoding: %v", err)
}
messageReader, err := openpgp.ReadMessage(block.Body, el, nil, nil)
if err != nil {
return "", fmt.Errorf("Error reading message: %v", err)
}
fmt.Printf("%+v\r\n", messageReader)
read, err := ioutil.ReadAll(messageReader.UnverifiedBody)
if err != nil {
return "", fmt.Errorf("Error reading unverified body: %v", err)
}
return string(read), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment