[英]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-packets
或pgpdump
来验证问题所在,该列表列出了消息中包含的OpenPGP数据包,对于调试OpenPGP问题非常有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.