Home
Logo - Encrypteasy

Encryption code

This is the code that EncryptEasy is using to interface with the PGP encryption protocol.

                                
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) {
    entityList, err := openpgp.ReadArmoredKeyRing(strings.NewReader(publicKey))
    if err != nil {
        return "", nil, err
    }
    firstSubKey := entityList[0]
    for k, _ := range firstSubKey.Identities {
        fingerPrint := firstSubKey.PrimaryKey.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
    }
    privOutput, err := key.ArmorPrivate(&config)
    if err != nil {
        fmt.Printf("Something went wrong: %v", err)
        return "", "", err
    }
    return privOutput, publicOutput, nil
}
func EncryptMessage(publicKey, message string) (string, error) {
    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()

    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)
    }
    read, err := ioutil.ReadAll(messageReader.UnverifiedBody)
    if err != nil {
        return "", fmt.Errorf("Error reading unverified body: %v", err)
    }
    return string(read), nil
}