duanjiaopi8218
2019-04-04 18:30
浏览 270
已采纳

用golang代替load_pem_private_key()

I have this code in python

privateKey = appAuth["privateKey"]
passphrase = appAuth["passphrase"]

from cryptography.hazmat.primitives.serialization import load_pem_private_key
key = load_pem_private_key(
  data=privateKey.encode('utf8'),
  password=passphrase.encode('utf8'),
  backend=default_backend(),)

I want to replicate this in golang. Basically I have this:-

"appAuth": {
  "publicKeyID": "2qwqckds",
  "privateKey": "-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0AeQwwDgQIkkci4MFCUtsCAggA
MBQGCCqGSIb3DQMHBAjyW3BUgbqg9ASCBMjEpz/mhgRXFKkFpSL4SAq7YLxYzO7H
YfbisbcsrN4C1wAiXpT4FMBuGnHzRrm/slu+DnTdbsKrg2SxiyW3Zy5FtBfYGPHF
Yim4It2lPywMHHS62b2qXdIicW4pfst/eqeisLpl0bBTR7UYbxr6vpsVFKrGkjEI
8+VtADGgm6ORH975NBZiLrCEO2aLOfeXlQrdWHcNEB46emvoasBKRZ0bUTxUqKN9
egGPprpfEb7yp0UoJZayYpxcZOSEuM42jXiGELQ9qM7xeADlTP5rbw2d6r/Hbr67
vyDk2fydvRra4XlbDgeKfiD71OaetoyF8o07Zo4VJegdZnHYW7BW2kXuD5uCiNYI
WN3l9TpX8i6FU1i++NidH80t5cHAHlhBc2v0if5g8TlmRDarOo6pX8d2KLV98V7F
7iWmS9vHtyZZvIgaWDchQ+fVQ2ZS6KCRGnGipxkmGyDXnPcx60YPiN9NxCmKs0ji
8e7xtM+QXzYPWF/rUQh/YmISYGoktOj2XMxXmpNXdpcHFIWEbuW0LuWhV++4WrnO
ly9Vc3PwnHi5KE+IzgoOtgPkxDkr664fd5H0DD3RD1ytWuMW0rtKMr8jx/vRUkzM
+yzGo0UMe4XW0M0Xcrpq+o+0NBwa05xGA6vlrerMuLcsYG3MexdBkuVMammpmxdM
tQ+Ch65ikggn9Er2LbwJyddw7bUaIox0hXPUaiAyICY77D7m4SEsBfMFjy420UoR
umz1+ss4edJ0ago0FumG3QaUJzTGUfCHd/johD3AkYAQsFvwiCkMcL2wQEcYDx4x
x8OCdM1rTn61sQOHHYH4fN4V8TAEbhG57kSzz/GLBlIu84m7zew+NN3pTTw5oU+V
VHFyBj1eW9ywtING1oEnbKXIdzSs4dWF7zwlzDUfbS5GY6Crm7vhre6pnZY0zs3O
+MzcGe0AHPwRPOtR4gUiuW5tbBhLM9Nd8xFdS29QUnydKDpSbhySJgCPcYkTtbSG
sQZMAK7d/Dv178Qta5a+oO0XMh7I4qZ6tuBlGH06QEUh2NJrfzp42hUS0zd9XIZ5
StD/R9aTSLTr0ljfzrgs/brGj9S0eSHuxV/4teTYapmZoO+G3G7SfViSRn+IdDdK
OLa5sLeOu/KCdH0mIyVDupmJKt0adAza3S4Wp719HZVrgOOJc4Ni9GXPiPR5Qxv5
G+/oUIrBxvBpK4gB6Fzr1iRz51FmvukURdeHVDJfbNyEvFWyQ6w5ZXrSbWnD73WB
k7/J4N2gLKYVYagI/J9GZ4q/cfM5w+JztdqeBXOKa7fyEBE3iEv5hKs/C7lvxuQs
o0yrqTok0GnrlSGq4R0ve/t6eQ4nyWLM1yMxhl/JMGw7QFq7bhfeAhwVcQB7f1eT
/krRppgbBwkGHaES47LkoBV9AyZlRIymFaF128SePSSa1YgIaivxghcopdqC3HD1
/NSFA9zvxXv54Neqv9sO2Mc2PHaiOq3RPQbP4zPV7CD8bBSUwztLNAJJC99OeNah
1nyvS69JvysuM7Mxgv3e8VK4+hQ+I1uMwL8UlfCJZ+ejHwpLX26kc7NfKP/NWzm7
6JCsEbbtoR7OWnVgXf0lkIlJPcs86HPoX1DzlzVUQsAtKLpnhxLBBfDH5NwVtolM
n74=
-----END ENCRYPTED PRIVATE KEY-----
",
  "passphrase": "2788518b2900d77fafc71dbf4f764927"
}

In golang, I have tried this so far:-

block, _ := pem.Decode([]byte(privateKey)) \\ privateKey is a string
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)

But it doesn't work. Please Help.

图片转代码服务由CSDN问答提供 功能建议

我在python中有此代码

  privateKey = appAuth [  “ privateKey”] 
passphrase = appAuth [“ passphrase”] 
 
来自cryptography.hazmat.primitives.serialization import load_pem_private_key 
key = load_pem_private_key(
 data = privateKey.encode('utf8'),
 password = passphrase。  encode('utf8'),
 backend = default_backend(),)
   
 
 

我想在golang中复制它。 基本上我有这个:-

 “ appAuth”:{
“ publicKeyID”:“ 2qwqckds”,
“ privateKey”:“ ----- BEGIN加密私有密钥----- 
MIIFDjBABgkqhkiG9w0BBQ0wMzAbBggqqkikki99w0Ae  
MBQGCCqGSIb3DQMHBAjyW3BUgbqg9ASCBMjEpz / mhgRXFKkFpSL4SAq7YLxYzO7H 
YfbisbcsrN4C1wAiXpT4FMBuGnHzRrm / SLU + DnTdbsKrg2SxiyW3Zy5FtBfYGPHF 
Yim4It2lPywMHHS62b2qXdIicW4pfst / eqeisLpl0bBTR7UYbxr6vpsVFKrGkjEI 
8 + VtADGgm6ORH975NBZiLrCEO2aLOfeXlQrdWHcNEB46emvoasBKRZ0bUTxUqKN9 
egGPprpfEb7yp0UoJZayYpxcZOSEuM42jXiGELQ9qM7xeADlTP5rbw2d6r / Hbr67 
vyDk2fydvRra4XlbDgeKfiD  71OaetoyF8o07Zo4VJegdZnHYW7BW2kXuD5uCiNYI 
WN3l9TpX8i6FU1i ++ NidH80t5cHAHlhBc2v0if5g8TlmRDarOo6pX8d2KLV98V7F 
7iWmS9vHtyZZvIgaWDchQ + fVQ2ZS6KCRGnGipxkmGyDXnPcx60YPiN9NxCmKs0ji 
8e7xtM + QXzYPWF / rUQh / YmISYGoktOj2XMxXmpNXdpcHFIWEbuW0LuWhV ++ 4WrnO 
ly9Vc3PwnHi5KE + IzgoOtgPkxDkr664fd5H0DD3RD1ytWuMW0rtKMr8jx / vRUkzM \ N + yzGo0UMe4XW0M0Xcrpq + O + 0NBwa05xGA6vlrerMuLcsYG3MexdBkuVMammpmxdM \ NTQ + Ch65ikggn9Er2LbwJyddw7bUaIox0hXPUaiAyICY77D7m4SEsBfMFjy420UoR 
umz1 + ss4edJ0ago0FumG3QaUJzTGUfCHd / johD3AkYAQsFvwiCkMcL2wQEcYDx4x 
x8OCdM1rTn61sQOHHYH4fN4V8TAEbhG57kSzz / GLBlIu84m7zew +  NN3pTTw5oU + V 
VHFyBj1eW9ywtING1oEnbKXIdzSs4dWF7zwlzDUfbS5GY6Crm7vhre6pnZY0zs3O \ N + MzcGe0AHPwRPOtR4gUiuW5tbBhLM9Nd8xFdS29QUnydKDpSbhySJgCPcYkTtbSG 
sQZMAK7d / Dv178Qta5a + oO0XMh7I4qZ6tuBlGH06QEUh2NJrfzp42hUS0zd9XIZ5 \ NSTD / R9aTSLTr0ljfzrgs / brGj9S0eSHuxV / 4teTYapmZoO + G3G7SfViSRn + IdDdK 
OLa5sLeOu / KCdH0mIyVDupmJKt0adAza3S4Wp719HZVrgOOJc4Ni9GXPiPR5Qxv5 \ NG + / oUIrBxvBpK4gB6Fzr1iRz51FmvukURdeHVDJfbNyEvFWyQ6w5ZXrSbWnD73WB 
k7 / J4N2gLKYVYagI / J9GZ4q / cfM5w + JZT  dqeBXOKa7fyEBE3iEv5hKs / C7lvxuQs 
o0yrqTok0GnrlSGq4R0ve / t6eQ4nyWLM1yMxhl / JMGw7QFq7bhfeAhwVcQB7f1eT \ N / krRppgbBwkGHaES47LkoBV9AyZlRIymFaF128SePSSa1YgIaivxghcopdqC3HD1 \ N / NSFA9zvxXv54Neqv9s​​O2Mc2PHaiOq3RPQbP4zPV7CD8bBSUwztLNAJJC99OeNah 
1nyvS69JvysuM7Mxgv3e8VK4 + HQ + + I1uMwL8UlfCJZ ejHwpLX26kc7NfKP / NWzm7 
6JCsEbbtoR7OWnVgXf0lkIlJPcs86HPoX1DzlzVUQsAtKLpnhxLBBfDH5NwVtolM 
n74 = 
 ----- END加密的私有密钥----- 
  “,
” passphrase“:” 2788518b2900d77fafc71dbf4f764927“ 
} 
   
 
 

到目前为止,在golang中,我已经尝试过:-

  block,_:= pem.Decode([] byte(privateKey))\\ privateKey是一个字符串
key,错误:= x509.ParsePKCS1PrivateKey(block.Bytes)
   
 
 

但是它不起作用。 请帮助。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhoutuo0883 2019-04-05 19:01
    已采纳

    To decrypt encrypted pkcs8 key (an example of such key is mentioned in the question) in go there is an awesome library https://github.com/youmark/pkcs8/.

    import "github.com/youmark/pkcs8"
    block, _ := pem.Decode([]byte(config.BoxAppSetting.AppAuth.PrivateKey))
    decryptedPrivateKey, err := pkcs8.ParsePKCS8PrivateKey(block.Bytes, []byte(config.BoxAppSetting.AppAuth.PassPhrase))
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题