尝试执行 go-pgp 时出现了文件结束错误。

huangapple go评论111阅读模式
英文:

EOF while attempting go-pgp

问题

这个错误是由于在加密过程中发生了EOF(文件结束)错误引起的。根据代码,我注意到在encrypt.go文件中的TestPublicKey常量是一个空字符串。这可能是导致错误的原因之一。

请确保TestPublicKey常量包含有效的公钥字符串。你可以从你的PGP密钥对中获取公钥,并将其复制到TestPublicKey常量中。

如果问题仍然存在,请检查你的代码逻辑,确保所有的依赖项都正确导入,并且加密过程中的输入数据正确。

英文:

I need to pgp encrypt a string in Go and I read that this library was good: "github.com/jchavannes/go-pgp/pgp"

main.go

package main

import "fmt"

func main() {
	encrypted := encrypt("4007400000000007", "123")
	fmt.Println(encrypted)
}

encrypt.go

package main

import (
	"fmt"

	"github.com/jchavannes/go-pgp/pgp"
)

func encrypt(ccNum string, cvv string) string {
	sourceString := `{"cardNumber":"` + ccNum + `","cvv":"` + cvv + `"}`
	fmt.Println(sourceString)
	pubEntity, err := pgp.GetEntity([]byte(TestPublicKey), []byte{})
	if err != nil {
		panic(err.Error())
	}

	encrypted, err := pgp.Encrypt(pubEntity, []byte(sourceString))
	if err != nil {
		panic(err.Error())
	}

	return string(encrypted)
}

const TestPrivateKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
xcZYBFkNK+ABEADUpjJ/kz3j+iz9qnzUb6ONw+WHSLp8umnd1z06SBVkWFjYReqf
oPCOq67XDseK71ZSevrIt7EdTLAzl0xN8kB+8iedAGM5OCakDe3R8L83OGy1Em26
PbrrYs3TYKGDXW65TsGYCoETROGgU2zPvuBDU1RvVvd9vAlWHQis43BOWaaakCEc
00V3sdNcfV+lz7fNUXEgtmTCCr9NWX4gO3YeenIIxep4WD27VwscW5Q2B1cnxcFL
+TZzE2oVjtXljGSO94XsekuNU47zwJZNGyU6SSlSZ+KVXuSdkRRfNYHlgDWg5b8C
xVmdVUfsx3bmNlOlXoyETj83xvRlLxn3PYIgOz6OlYGba5oDogK2QLXGTXK1o9OE
kgoghmCNQqxocvb1h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HSLp8umnd1z06SBVkWFjYReqf
oPCOq67XDseK71ZSevrIt7EdTLAzl0xN8kB+8iedAGM5OCakDe3R8L83OGy1Em26
PbrrYs3TYKGDXW65TsGYCoETROGgU2zPvuBDU1RvVvd9vAlWHQis43BOWaaakCEc
00V3sdNcfV+lz7fNUXEgtmTCCr9NWX4gO3YeenIIxep4WD27VwscW5Q2B1cnxcFL
+TZzE2oVjtXljGSO94XsekuNU47zwJZNGyU6SSlSZ+KVXuSdkRRfNYHlgDWg5b8C
xVmdVUfsx3bmNlOlXoyETj83xvRlLxn3PYIgOz6OlYGba5oDogK2QLXGTXK1o9OE
kgoghmCNQqxocvb1hQXT8cEynIbAdc6/JknYaoic6ka1iTTz3uN8FEPw5gRlidcQ
3wkbmqIS0LJs3JmVbD7/BxMY1dwqMyvulfnLiTsWSPvk41o7dHf077t23V9w78Jg
h4Xq4HRvt37PtuO6eWW3c5aUIWmvvDqMbMEqp2y23noYoVNqEpVoHolDdoCSurv/
XxbNBnj46XwaIs6OlrO2htV0al2/WVTNnSLxCyoHXoJEDXyaOyNKn1jM/FczgYQJ
069uC804ohOfjLmbtUEYE7Hjeo5utPm2ryjnakgV5AStKgL0SyFZUwN/DwARAQAB
=gO1a
-----END PGP PUBLIC KEY BLOCK-----`

When I run the code I get:

{"cardNumber":"4007400000000007","cvv":"123"}
panic: EOF

goroutine 1 [running]:
main.encrypt({0x561363?, 0x60?}, {0x55f3af?, 0x0?})
        /home/peter/Documents/go-projects/circle-pgp-driver/encrypt.go:14 +0x196
main.main()
        /home/peter/Documents/go-projects/circle-pgp-driver/main.go:6 +0x31

Any idea what causes this panic?

答案1

得分: 1

遇到类似问题,通过在密钥字符串的-----BEGIN PGP PUBLIC KEY BLOCK-----之后插入一个新行来解决,对于你的情况看起来也是类似的。

像这样:

const TestPublicKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----

xsFNBFkNK+ABEADUpjJ/kz3j+iz9qnzUb6ONw+WHSLp8umnd1z06SBVkWFjYReqf
....`
英文:

Had faced similar issue, was solved by putting a new line after -----BEGIN PGP PUBLIC KEY BLOCK----- in the key string, looks similar for your case.

like

const TestPublicKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----

xsFNBFkNK+ABEADUpjJ/kz3j+iz9qnzUb6ONw+WHSLp8umnd1z06SBVkWFjYReqf
....

huangapple
  • 本文由 发表于 2022年9月6日 21:13:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/73622677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定