Help: Encrypting and Decrypting with golang.org/x/crypto/openpgp

Hi,

i have been trying to decrypt and encrypt ASCII armor PGP Messages that i receive and send to a API.

I Wrote a function for decrypting messages and another one for encrypting messages. The Decrypting function Works and i can decrypt the API’s messages. But My encrypt function Seems to be broken as Neither the API or my decrypt function can decrypt the Messages encrypted by my encrypt function. Decrypting the encrypted messages from my encrypt function always result in the following error: “Error reading message: openpgp: unsupported feature: unknown SymmetricallyEncrypted version”

Here is only the problematic encrypt function:

func encryptMessage(publicKey, privateKey, privateKeyPassword, message string) (string, error) {

	pubEl, err := openpgp.ReadArmoredKeyRing(strings.NewReader(publicKey))
	if err != nil {
		return "", fmt.Errorf("Error Reading Public Keyring: %v", err)
	}

	privEl, err := openpgp.ReadArmoredKeyRing(strings.NewReader(privateKey))
	if err != nil {
		return "", fmt.Errorf("Error Reading Private Keyring: %v", err)
	}

	privateEntity := privEl[0]
	err = privateEntity.PrivateKey.Decrypt([]byte(privateKeyPassword))
	if err != nil {
		return "", fmt.Errorf("Error decrypting PrivateKey: %v", err)
	}
	for _, subkey := range privateEntity.Subkeys {
		err = subkey.PrivateKey.Decrypt([]byte(privateKeyPassword))
		if err != nil {
			return "", fmt.Errorf("Error decrypting PrivateKey: %v", err)
		}
	}

	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)
	}

	messageReader := bytes.NewReader([]byte(message))
	_, err = io.Copy(encoderWriter, messageReader)
	if err != nil {
		return "", fmt.Errorf("Error writing data to encoder: %v", err)
	}

	encryptorWriter.Close()
	encoderWriter.Close()

	return string(buf.Bytes()), nil
}

And Here I have uploaded a example to the Go Play Ground : https://play.golang.org/p/JhhZ4xWWYvn

Any help is Appreciated.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.