openpgp: invalid data: tag byte does not have MSB set

Code that tries to decrypt message:

func decrypt() {
    privfile, err := os.Open(privkeyf)
    if err != nil {
        fmt.Printf("Error: %sn", err)
        os.Exit(1)
    }
    defer privfile.Close()

    keyring, err := openpgp.ReadKeyRing(privfile)
    if err != nil {
        fmt.Printf("Error reading keyring: %sn", err)
        os.Exit(1)
    }

    file, err := os.Open(filef)
    if err != nil {
        fmt.Printf("Error: %sn")
        os.Exit(1)
    }
    defer file.Close()

    md, err := openpgp.ReadMessage(file, keyring, nil, nil)
    if err != nil {
        fmt.Printf("Error decrypting: %sn")
        os.Exit(1)
    }

    fmt.Println("Message Details")
    fmt.Printf("Was encrypted: %bnWas signed: %bnContents: nn%snn", md.IsEncrypted, md.IsSigned, md.UnverifiedBody)
}

Code that creates public and private key:

func createKeypair() {
    // We need to ask for a keyname, name, comment and email
    var keyname, name, comment, email string
    fmt.Print("Enter name for key: ")
    fmt.Scanln(&keyname)
    fmt.Print("Enter your name: ")
    fmt.Scanln(&name)
    fmt.Print("Enter a comment: ")
    fmt.Scanln(&comment)
    fmt.Print("Enter your email: ")
    fmt.Scanln(&email)
    // We will create entity and serialize to buffer
    var buffer bytes.Buffer
    entity, err := openpgp.NewEntity(name, comment, email, nil)
    if err != nil {
        fmt.Printf("Error: %sn")
        os.Exit(1)
    }

    entity.Serialize(&buffer)
    pubkey := base64.StdEncoding.EncodeToString([]byte(buffer.String()))
    buffer.Truncate(0)

    entity.SerializePrivate(&buffer, nil)
    privkey := base64.StdEncoding.EncodeToString([]byte(buffer.String()))

    pubfile, err := os.Create(keyname + ".pub")
    if err != nil {
        fmt.Printf("Error: %sn", err)
        os.Exit(1)
    }
    defer pubfile.Close()

    privfile, err := os.Create(keyname + ".key")
    if err != nil {
        fmt.Printf("Error: %sn", err)
        os.Exit(1)
    }
    defer privfile.Close()

    _, err = pubfile.WriteString(pubkey + "n")
    if err != nil {
        fmt.Printf("Error: %sn", err)
        os.Exit(1)
    }

    _, err = privfile.WriteString(privkey + "n")
    if err != nil {
        fmt.Printf("Error: %sn", err)
        os.Exit(1)
    }
}

Error i’m getting when executing the program:

Error reading keyring: openpgp: invalid data: tag byte does not have MSB set

I’ve tried both openpgp.ReadKeyRing and openpgp.ReadArmoredKeyRing but both return the same error, and being the noob I am when it comes to pgp I have no clue what the error means.

Am I doing something wrong, if so what? Pointers to how to solve this, or a solution would be greatly appreciated.

NOTE: Please ignore the fact that I’m trying to print an io.Reader as a string, rather than reading from it.


Source: linux

Leave a Reply