【问题标题】:How to Encrypt a String to an ASCII armored file in gogo - 如何在go中将字符串加密为ASCII装甲文件
【发布时间】:2016-05-06 14:56:15
【问题描述】:

我现在真的很难在我的代码中找到错误 - 任务是将字符串加密到一个 pgp ASCII 装甲文件中 - 一件简单的事情可以想到。

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
}

但是,另一端的人收到此错误消息:

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

非常欢迎提示和建议!

【问题讨论】:

  • 如果您为正确的密钥加密,我认为您没有做错任何事情。查看密钥服务器上的该密钥,您已加密为最新的(也是唯一的)加密子密钥。如果“另一端的人”收到错误消息,他没有持有密钥,那么要么你使用了错误的密钥进行加密,要么对方给了你错误的密钥,要么对方搞砸了自己。
  • 我使用他们在他们的网站上发布的 ASCII 装甲密钥。我认为关键是正确的。但是,当我们使用脚本时,他们会尝试在命令行上使用 gpg 解密。
  • 没关系。 “其他人”实现正确地意识到您为(子)密钥5BE299DC 加密,但没有此密钥。只有我的评论中列出的原因之一会导致此消息。
  • 嗨 Jens,我们现在尝试使用我的联系人的个人密钥,它确实像一个魅力。看起来加密不仅对像我这样的网络人来说是困难的;) - 如果您将评论添加为答案,我很乐意将其标记为最后一个。

标签: go pgp openpgp


【解决方案1】:

但是,另一端的人收到此错误消息:

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

如果您为正确的密钥加密,我认为您没有做错任何事情。查看密钥服务器上的该密钥,您已加密为最新的(也是唯一的)加密子密钥。

如果“另一端的人”收到一条错误消息,表明他不会持有密钥,那么

  • 您使用了错误的密钥进行加密,
  • “另一个人”给了你错误的钥匙或
  • “另一个人”把自己搞砸了。

您可以通过将加密的内容传递给gpg --list-packetspgpdump 来验证出了什么问题,它们列出了消息中包含的 OpenPGP 数据包,并且对调试 OpenPGP 问题非常有帮助。

【讨论】:

    猜你喜欢
    • 2019-06-11
    • 2019-04-29
    • 2018-08-02
    • 2018-09-07
    • 2014-11-22
    • 2012-04-23
    • 2012-08-02
    • 2016-12-03
    • 2017-02-14
    相关资源
    最近更新 更多