英文:
the function x509.ParsePKCS8PrivateKey return rsa.privateKey. But can't use in the encryptPKCS1v15 function
问题
const strPrivateKey = "30820b82020100300d06092a864886f70d010101050004820b6c30820b680201000282028100acfc585f43ca36ec2dddc518b5c7d1303b658faec58b634aff16ce4b7930b93a23517f8d9c8a260f4e2eb44b01da5b6588fefe63acb68c15677"
decoded, err := hex.DecodeString(strPrivateKey)
if err != nil {
return ""
}
privateKey, err := x509.ParsePKCS8PrivateKey(decoded)
if err != nil {
return ""
}
encypt, err := rsa.EncryptPKCS1v15(rand.Reader, &privateKey.PublicKey, data)
if err != nil {
fmt.Println(err)
return ""
}
privateKey.PublicKey未定义(类型any没有PublicKey字段或方法)
英文:
const strPrivateKey = "30820b82020100300d06092a864886f70d010101050004820b6c30820b680201000282028100acfc585f43ca36ec2dddc518b5c7d1303b658faec58b634aff16ce4b7930b93a23517f8d9c8a260f4e2eb44b01da5b6588fefe63acb68c15677"
decoded, err := hex.DecodeString(strPrivateKey)
if err != nil {
return ""
}
privateKey, err := x509.ParsePKCS8PrivateKey(decoded)
if err != nil {
return ""
}
encypt, err := rsa.EncryptPKCS1v15(rand.Reader, &privateKey.PublicKey, data)
if err != nil {
fmt.Println(err)
return ""
}
privateKey.PublicKey undefined (type any has no field or method PublicKey)
答案1
得分: 1
根据文档(https://pkg.go.dev/crypto/x509@go1.19.3#ParsePKCS8PrivateKey):
func ParsePKCS8PrivateKey(der []byte) (key any, err error)
...
它返回一个*rsa.PrivateKey
、一个*ecdsa.PrivateKey
或一个ed25519.PrivateKey
。将来可能支持更多类型。
你应该使用type assertion来检查密钥的类型:
switch privateKey := privateKey.(type) {
case *rsa.PrivateKey:
// ...
case *ecdsa.PrivateKey:
// ...
case ed25519.PrivateKey:
// ...
default:
panic("unknown key")
}
由于rsa.EncryptPKCS1v15
期望一个*rsa.PublicKey
,你的代码可以这样写:
if privateKey, ok := privateKey.(*rsa.PrivateKey); ok {
encypt, err := rsa.EncryptPKCS1v15(rand.Reader, &privateKey.PublicKey, data)
}
顺便说一下,提供的strPrivateKey
是无效的(encoding/hex: odd length hex string
)。你可以从https://github.com/golang/go/blob/1c05968c9a5d6432fc6f30196528f8f37287dd3d/src/crypto/x509/pkcs8_test.go#L52-L124获取一些有效的私钥。
英文:
According to the doc (https://pkg.go.dev/crypto/x509@go1.19.3#ParsePKCS8PrivateKey):
> func ParsePKCS8PrivateKey(der []byte) (key any, err error)
>
> ...
> It returns a *rsa.PrivateKey
, a *ecdsa.PrivateKey
, or a ed25519.PrivateKey
. More types might be supported in the future.
You should use type assertion to check the type of the key:
switch privateKey := privateKey.(type) {
case *rsa.PrivateKey:
// ...
case *ecdsa.PrivateKey:
// ...
case ed25519.PrivateKey:
// ...
default:
panic("unknown key")
}
Since rsa.EncryptPKCS1v15
expects a *rsa.PublicKey
, your code can be written like this:
if privateKey, ok := privateKey.(*rsa.PrivateKey); ok {
encypt, err := rsa.EncryptPKCS1v15(rand.Reader, &privateKey.PublicKey, data)
}
BTW, the provided strPrivateKey
is invalid (encoding/hex: odd length hex string
). You can get some valid private keys from https://github.com/golang/go/blob/1c05968c9a5d6432fc6f30196528f8f37287dd3d/src/crypto/x509/pkcs8_test.go#L52-L124
答案2
得分: 0
*正确答案。我解析了privateKey.(rsa.PrivateKey)
decodedString, err := hex.DecodeString(utility.StrPrivateKey)
if err != nil {
return err
}
pkcs8PrivateKey, err := x509.ParsePKCS8PrivateKey(decodedString)
if err != nil {
return err
}
privateKey := pkcs8PrivateKey.(*rsa.PrivateKey)
英文:
*correct answer. I resolved privateKey.(rsa.PrivateKey)
decodedString, err := hex.DecodeString(utility.StrPrivateKey)
if err != nil {
return err
}
pkcs8PrivateKey, err := x509.ParsePKCS8PrivateKey(decodedString)
if err != nil {
return err
}
privateKey := pkcs8PrivateKey.(*rsa.PrivateKey)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论