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

用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.

  • 写回答

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))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题