英文:
GO - How to convert public key from string to PEM format
问题
我正在尝试使用jwt库进行jwt验证。我通过调用另一个应用程序的REST端点来获取公钥,该端点以字符串格式返回公钥。
现在,当我尝试以相同的字符串格式发送该公钥时,我收到"Invalid key format"的错误。请问如何将字符串格式的密钥转换为有效的PEM格式?谢谢。
func (test *TESTStrategy) doJWTValidation(token string, key string, logger *util.Logger) (TESTResponse, error) {
parsedToken, jwtErr := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
return decodedJWT.ParsedPubKey, nil
})
下面是当将密钥作为字符串传递给jwt.Parse()调用时收到的错误。
公钥:
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsFWkb/eSl6I3DRVhaonW3DFy8EnL0yaPiDzCcOLuYfBjN9zZIR1wXmnMJFle1K89qHGg42wgweVTIwA1XFTfoUKSziwsjF6FscZX5H56ZYyS/wWiO3rWWynlfbSZt+ga71+ndsu+A0Dy7Nn7ZgP8kRsu4UM5vE7QQTRERNiUKpzScN1cgZUFUqSddQmkwTEN8hH1mFX1Mum54NGqWIlmQxQDrOyogmMXIaaakhabcmuIPMULVVDVwUJC9sSDsc/j05qcZn3kkiEBRyiYB6ZLY2W7WfiV+dB7/icPONsYSD0FxHWEGNnbqtiGoNf9WZWtaP+o8WMR9sB3GKGVnbLvbQIDAQAB
英文:
I am trying to use jwt library to do jwt validation. And i am getting public key from another application by calling its REST endpoint which is returning public key in string format.
So now when trying to send that public key in same string format, i am getting "Invalid key format". Any help on how to convert string formatted key to a valid PEM format would be great.
func (test *TESTStrategy) doJWTValidation(token string, key string, logger *util.Logger) (TESTResponse, error) {
parsedToken, jwtErr := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
return decodedJWT.ParsedPubKey, nil
})
Below is the error what i am getting when passing key as a string to jwt.Pasrse() call.
Public Key:
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsFWkb/eSl6I3DRVhaonW3DFy8EnL0yaPiDzCcOLuYfBjN9zZIR1wXmnMJFle1K89qHGg42wgweVTIwA1XFTfoUKSziwsjF6FscZX5H56ZYyS/wWiO3rWWynlfbSZt+ga71+ndsu+A0Dy7Nn7ZgP8kRsu4UM5vE7QQTRERNiUKpzScN1cgZUFUqSddQmkwTEN8hH1mFX1Mum54NGqWIlmQxQDrOyogmMXIaaakhabcmuIPMULVVDVwUJC9sSDsc/j05qcZn3kkiEBRyiYB6ZLY2W7WfiV+dB7/icPONsYSD0FxHWEGNnbqtiGoNf9WZWtaP+o8WMR9sB3GKGVnbLvbQIDAQAB
答案1
得分: 3
这是一个PEM编码的密钥,只是缺少了BEGIN和END头部。该密钥是简单的Base64编码,你可以按照以下步骤解码并解析为RSA密钥:
base64Data := []byte(`MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsFWkb/eSl6I3DRVhaonW3DFy8EnL0yaPiDzCcOLuYfBjN9zZIR1wXmnMJFle1K89qHGg42wgweVTIwA1XFTfoUKSziwsjF6FscZX5H56ZYyS/wWiO3rWWynlfbSZt+ga71+ndsu+A0Dy7Nn7ZgP8kRsu4UM5vE7QQTRERNiUKpzScN1cgZUFUqSddQmkwTEN8hH1mFX1Mum54NGqWIlmQxQDrOyogmMXIaaakhabcmuIPMULVVDVwUJC9sSDsc/j05qcZn3kkiEBRyiYB6ZLY2W7WfiV+dB7/icPONsYSD0FxHWEGNnbqtiGoNf9WZWtaP+o8WMR9sB3GKGVnbLvbQIDAQAB`)
d := make([]byte, base64.StdEncoding.DecodedLen(len(base64Data)))
n, err := base64.StdEncoding.Decode(d, base64Data)
if err != nil {
// 处理错误
}
d = d[:n]
key, err := x509.ParsePKIXPublicKey(d)
if err != nil {
// 处理错误
}
fmt.Println(key)
如果你需要将密钥以PEM编码形式输出,只需添加适当的头部和尾部,例如 -----BEGIN PUBLIC KEY-----
和 -----END PUBLIC KEY-----
。注意,BEGIN头部必须单独位于一行,并以新行 ("\n"
) 结束。END头部也必须在新行之前。
英文:
That's a PEM encoded key, it's just missing the BEGIN & END headers. The key is simple Base64 encoded, you can decode and unmarshal into a RSA key as follows:
base64Data := []byte(`MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsFWkb/eSl6I3DRVhaonW3DFy8EnL0yaPiDzCcOLuYfBjN9zZIR1wXmnMJFle1K89qHGg42wgweVTIwA1XFTfoUKSziwsjF6FscZX5H56ZYyS/wWiO3rWWynlfbSZt+ga71+ndsu+A0Dy7Nn7ZgP8kRsu4UM5vE7QQTRERNiUKpzScN1cgZUFUqSddQmkwTEN8hH1mFX1Mum54NGqWIlmQxQDrOyogmMXIaaakhabcmuIPMULVVDVwUJC9sSDsc/j05qcZn3kkiEBRyiYB6ZLY2W7WfiV+dB7/icPONsYSD0FxHWEGNnbqtiGoNf9WZWtaP+o8WMR9sB3GKGVnbLvbQIDAQAB`)
d := make([]byte, base64.StdEncoding.DecodedLen(len(base64Data)))
n, err := base64.StdEncoding.Decode(d, base64Data)
if err != nil {
// Handle error
}
d = d[:n]
key,err:=x509.ParsePKIXPublicKey(d)
if err != nil {
// Handle error
}
fmt.Println(key)
If you need the key in PEM encoded form, simple add the appropriate header and footer, e.g. -----BEGIN PUBLIC KEY-----
& -----END PUBLIC KEY-----
. Note that the BEGIN header must start on its own line and end in a new line ("\n"
). The END header must also be proceeded by a new line.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论