简体   繁体   English

如何在Go中将字符串加密为ASCII装甲文件

[英]How to Encrypt a String to an ASCII armored file in go

I'm at the moment realy struggeling in finding the error in my code - the task is to encrypt a string into a pgp ASCII armored file - a simple thing one could think. 目前,我确实在努力寻找代码中的错误-任务是将字符串加密为pgp ASCII装甲文件-这是一件容易想到的事情。

I use the following function, inspired by this gist : 受此要旨启发,我使用以下功能:

// pgp encryption using the pgp RSA certificate
// massive thx to https://gist.github.com/jyap808/8250124
func encToFile(secretString string, filename string) (string, error) {
  log.Println("Public Keyring: ", publicKeyring)

  encryptionType := "PGP MESSAGE"

  // Read in public key
  keyringFileBuffer, _ := os.Open(publicKeyring)
  defer keyringFileBuffer.Close() 
  entityList, err := openpgp.ReadArmoredKeyRing(keyringFileBuffer) 
  check(err)

  encbuf := bytes.NewBuffer(nil)
  w, err := armor.Encode(encbuf, encryptionType, nil) // the encoder somehow makes this into ASCII armor
  check(err)

  plaintext, err := openpgp.Encrypt(w, entityList, nil, nil, nil)
  check(err)

  message := []byte(secretString)
  _, err = plaintext.Write(message)

  plaintext.Close()
  w.Close()

  // Output encrypted/encoded string
  log.Println("Writing Encrypted Secred to: ", filename)
  // we write the file into a file

  err = ioutil.WriteFile(filename, encbuf.Bytes(), 0644)
  check(err)

  log.Println("File:\n", encbuf.String())


  return encbuf.String(), nil
}

However, the guys on the other end get this error message: 但是,另一端的人收到此错误消息:

gpg: encrypted with RSA key, ID 5BE299DC
gpg: decryption failed: No secret key

Hints and suggestion would be very welcome! 提示和建议将非常受欢迎!

However, the guys on the other end get this error message: 但是,另一端的人收到此错误消息:

 gpg: encrypted with RSA key, ID 5BE299DC gpg: decryption failed: No secret key 

If you encrypted for the right key, I don't think you did anything wrong. 如果您加密了正确的密钥,我认为您没有做错任何事情。 Looking at that key on the key servers, you encrypted to the newest (and only) encryption subkey. 在密钥服务器上查看该密钥,您已加密到最新的(也是唯一的)加密子密钥。

If the "guy on the other end" gets an error message indicating that he would not hold the secret key, then either 如果“另一端的人”收到一条错误消息,指示他将不持有该秘密密钥,则可以选择

  • you use the wrong key for encryption, 您使用错误的密钥进行加密,
  • "the other guy" gave you the wrong key or “另一个人”给了您错误的密钥,或者
  • "the other guy" messed up himself. “另一个家伙”搞砸了自己。

You can verify what's going wrong by passing the encrypted contents to gpg --list-packets or pgpdump , which list the OpenPGP packets contained in the message and are very helpful at debugging OpenPGP issues. 您可以通过将加密的内容传递给gpg --list-packetspgpdump来验证问题所在,该列表列出了消息中包含的OpenPGP数据包,对于调试OpenPGP问题非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM