Go语言 如何使用RSA私钥解密加密邮件

2lpgd968  于 2022-12-07  发布在  Go
关注(0)|答案(1)|浏览(279)

我点击了一个需要我的公钥的API,然后那个API将返回一个包含向量和令牌的响应,但是它们是加密的形式(我认为他们使用了我的公钥)。为了解密它,我需要使用我的私钥。私钥和公钥都是DER编码。我基本上想实现类似于https://www.devglan.com/online-tools/rsa-encryption-decryption x1c 0d1x的东西
我使用的键(再次他们是哑!)公共:MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbI1SofN/ZPsprBlPAW/c4GDKHo6Idv3J/P5LEEUpAsbVVpq6wY7Tf6lfBSTcirq48oMD34p3z5sX1jYy15OXNxNKBpoOdv8BYQdnK/LfFcZ5720yUNka0xiHUol/Zi0c0PWB6HkAAkT0sbeGLbCuX6yr6SbJBFaI2ofIKk/mGswIDAQAB
私有:MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJsjVKh839k+ymsGU8Bb9zgYMoejoh2/cn8/ksQRSkCxtVWmrrBjtN/qV8FJNyKurjygwPfinfPmxfWNjLXk5c3E0oGmg52/wFhB2cr8t8VxnnvbTJQ2RrTGIdSiX9mLRzQ9YHoeQACRPSxt4YtsK5frKvpJskEVojah8gqT+YazAgMBAAECgYA5KaeD/Z8tmSlUjhxGAJzzGldkCzMs6uHxaSdW/1fwxooeaTBs5hA7gUmcerHKIbsps8HmlKXGvP2sIN/8Fb9mo1yHaAN2g52+cvpr2QmeeUOwfuZTM/gllT4rUIB7sMC5Uxl7sW55bGhRxqdfHJskow4rPga+irjuYSy5GIZSwQJBANDtfF+VWPKrR8MHYvaGoLLkrA1cFn8g9x1eGA3mgWPNYoH5nMJfe0IBaXo69jlrnBAjK+oxnPSyh/AZzsEehasCQQC+F15dVSjH8tJSXz8RCDpMdp+P9J/eoi7+V0IgwKAaSJCZfYWXeckAAIpvwit7fkj3n83nj0PPm9Xwh22CYGsZAkAAvIWOtLEOken71v/TTKAuYT32AhgWNKCKWvWMvv4/ws6RFLcnvDxr/CNKCgEqLKXtLKKP/cLG3gY6o2ymI5xnAkA8l4JSycRYB8SW0RlvOmoq2Vz/LACRnKzSEcpYWk4uUpcU9ffRUtaJE6MNAJ2Pna9wjxW5C+eXk5rcMq7rcgRxAkAjizE3JbwUu3MX5TDmHfGCvIZ38jnfSUjFrYBLKaNJegOD4zcxRZXfO7iKDa+PtFMRe7ZtnU7WuCM0yUT2Qa0j
加密邮件:BUy5nR/PuJNCFXeUxQd4oxkCvMo2JiLwH5VkzLz1UdM4M0VXDv4Ba1OaNHbttqETQENy1VfW2V4v9Pw5HmDIcS1sdGN69ceEHJbned46rK1EVtMVQZGP9ha54AXie5v/1TdKYWJ+AFt0FxVj4kfFXGXEzF19Q8LB2AOJCAEJK6s=让我们转到编码部分:我正在从env文件中检索我的私钥和公钥:

public := goDotEnvVariable("publicKey")
private := goDotEnvVariable("privateKey")

现在,在点击API并获得结果后,我试图使用rsa.DecryptOAEP函数解密消息,但它一直给我[] crypto/rsa: decryption error。在包本身内部调试后,我在这里得到错误:

k := priv.Size() // private key size
if len(ciphertext) > k || //ciphertext is the encrypted text and hash.Size()*2+2 is the hash size that Im using
    k < hash.Size()*2+2 {
    return nil, ErrDecryption
}

if语句的结果:k = 128 hash.Size()*2+2=66 cipher = 172起初我遇到了一个将私钥(在我的例子中是字符串,因为它来自env文件)转换为rsa私钥的问题。以下是我解决这个问题的方法:

sEnc, err := b64.StdEncoding.DecodeString(private) //using this package "encoding/base64"

然后我把它转换成这样:

block := &pem.Block{
    Type:  "RSA PRIVATE KEY",
    Bytes: sEnc,
    }
    parseResult, err := x509.ParsePKCS8PrivateKey(block.Bytes)

我做错了什么?

gev0vcfq

gev0vcfq1#

如果在 Select Cipher Type 字段中选择了 RSA,devglan网站将应用PKCS#1 v1.5作为填充,因此必须使用rsa.DecryptPKCS1v15()。私钥是Base64编码的PKCS#8 DER密钥,可以使用x509.ParsePKCS8PrivateKey()导入。
所有人一起:

import (
    "crypto/rand"
    "crypto/rsa"
    "crypto/x509"
    b64 "encoding/base64"
    "fmt"
)

func main() {

    // Base64 decode ciphertext
    ciphertext, _ := b64.StdEncoding.DecodeString("BUy5nR/PuJNCFXeUxQd4oxkCvMo2JiLwH5VkzLz1UdM4M0VXDv4Ba1OaNHbttqETQENy1VfW2V4v9Pw5HmDIcS1sdGN69ceEHJbned46rK1EVtMVQZGP9ha54AXie5v/1TdKYWJ+AFt0FxVj4kfFXGXEzF19Q8LB2AOJCAEJK6s=")

    // Import PKCS#8 key
    pkcs8DerKey, _ := b64.StdEncoding.DecodeString("MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJsjVKh839k+ymsGU8Bb9zgYMoejoh2/cn8/ksQRSkCxtVWmrrBjtN/qV8FJNyKurjygwPfinfPmxfWNjLXk5c3E0oGmg52/wFhB2cr8t8VxnnvbTJQ2RrTGIdSiX9mLRzQ9YHoeQACRPSxt4YtsK5frKvpJskEVojah8gqT+YazAgMBAAECgYA5KaeD/Z8tmSlUjhxGAJzzGldkCzMs6uHxaSdW/1fwxooeaTBs5hA7gUmcerHKIbsps8HmlKXGvP2sIN/8Fb9mo1yHaAN2g52+cvpr2QmeeUOwfuZTM/gllT4rUIB7sMC5Uxl7sW55bGhRxqdfHJskow4rPga+irjuYSy5GIZSwQJBANDtfF+VWPKrR8MHYvaGoLLkrA1cFn8g9x1eGA3mgWPNYoH5nMJfe0IBaXo69jlrnBAjK+oxnPSyh/AZzsEehasCQQC+F15dVSjH8tJSXz8RCDpMdp+P9J/eoi7+V0IgwKAaSJCZfYWXeckAAIpvwit7fkj3n83nj0PPm9Xwh22CYGsZAkAAvIWOtLEOken71v/TTKAuYT32AhgWNKCKWvWMvv4/ws6RFLcnvDxr/CNKCgEqLKXtLKKP/cLG3gY6o2ymI5xnAkA8l4JSycRYB8SW0RlvOmoq2Vz/LACRnKzSEcpYWk4uUpcU9ffRUtaJE6MNAJ2Pna9wjxW5C+eXk5rcMq7rcgRxAkAjizE3JbwUu3MX5TDmHfGCvIZ38jnfSUjFrYBLKaNJegOD4zcxRZXfO7iKDa+PtFMRe7ZtnU7WuCM0yUT2Qa0j")
    key, _ := x509.ParsePKCS8PrivateKey(pkcs8DerKey)
    var privateKey *rsa.PrivateKey
    privateKey, _ = key.(*rsa.PrivateKey)

    // Decrypt (using RSA with PKCS#1 v1.5 padding)
    rng := rand.Reader
    plaintext, _ := rsa.DecryptPKCS1v15(rng, privateKey, ciphertext)

    fmt.Println(string(plaintext)) // {"token":"312ade4b52e7bb4cadf59b4c7c83cb41","vector":"2b8db4fdb11f361d","id":"63876cf63ec7a641db8f1def"}
}

相关问题