簡體   English   中英

如何在Go中將字符串加密為ASCII裝甲文件

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

目前,我確實在努力尋找代碼中的錯誤-任務是將字符串加密為pgp ASCII裝甲文件-這是一件容易想到的事情。

受此要旨啟發,我使用以下功能:

// 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

提示和建議將非常受歡迎!

但是,另一端的人收到此錯誤消息:

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

如果您加密了正確的密鑰,我認為您沒有做錯任何事情。 在密鑰服務器上查看該密鑰,您已加密到最新的(也是唯一的)加密子密鑰。

如果“另一端的人”收到一條錯誤消息,指示他將不持有該秘密密鑰,則可以選擇

  • 您使用錯誤的密鑰進行加密,
  • “另一個人”給了您錯誤的密鑰,或者
  • “另一個家伙”搞砸了自己。

您可以通過將加密的內容傳遞給gpg --list-packetspgpdump來驗證問題所在,該列表列出了消息中包含的OpenPGP數據包,對於調試OpenPGP問題非常有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM