如何在 C# 中解密用 GO 语言加密的 RSA 加密字符串。解码 OAEP 填充时出错

How to decrypt RSA encrypted string in c# while it was encrypted in GO language. Error occurred while decoding OAEP padding

我在 c# 上有一个应用程序 运行,在 go 中有另一个服务器应用程序。我需要使用 rsa 实现安全通信。
我正在做的是在我的 C# 应用程序中初始化 RSA 提供程序并生成 public 密钥以提取模数和指数。然后连接模数(十六进制)和指数(int)并将此字符串转换为 Base64 字符串,将其发送到 GO 端点。

这是 C# 代码片段

public string ConvertToPublicKey()
    {
        CspParameters rsaParameters = GetCspParameters();
        RSACryptoServiceProvider provider = newRSACryptoServiceProvider(rsaParameters);
        string paramsXml = RsaProvider.ToXmlString(false);

        XDocument xDocument = XDocument.Parse(paramsXml);
        string modulus = xDocument.Descendants().FirstOrDefault(x => x.Name == "Modulus")?.Value ?? string.Empty;
        string exponent = xDocument.Descendants().FirstOrDefault(x => x.Name == "Exponent")?.Value ?? string.Empty;
        byte[] base64BytesOfModulus = Convert.FromBase64String(modulus);
        string hexaDecimalofModulus = BitConverter.ToString(base64BytesOfModulus).Replace("-", string.Empty);
        byte[] base64BytesOfExponent = Convert.FromBase64String(exponent);
        string hexadecimalOfExponent = BitConverter.ToString(base64BytesOfExponent).Replace("-", string.Empty);
        int intOfExponent = Convert.ToInt32(hexadecimalOfExponent, 16);
        byte[] publicKey = Encoding.UTF8.GetBytes($"{hexaDecimalofModulus};{intOfExponent}");
        return Convert.ToBase64String(publicKey);
    }

    private static CspParameters GetCspParameters()
    {
        const string containerName = "KeyContainer";
        return new CspParameters
        {
            KeyContainerName = containerName,
            Flags = CspProviderFlags.UseMachineKeyStore
        };
    }

在 Go 端点,我正确收到 public 密钥和模数。然后我使用 public 密钥对消息进行加密,并在将加密消息 byte[] 转换为 base64.

后将其发送回 c# 应用程序作为响应

这是 GO 代码片段

func GetLicenseInfo(responseWriter http.ResponseWriter,request*http.Request) 
    {
    encryptionKey := request.Header.Get("Authorization")
    var decodedStringBytes, errors = b64.StdEncoding.DecodeString(encryptionKey)
    if errors == nil {
        var decodedString = string(decodedStringBytes)
        result := strings.Split(decodedString, ";")
        modulus := new(big.Int)
        modulus.SetString(result[0], 16)
        exponent, exponentErrors := strconv.Atoi(result[1])
        if exponentErrors == nil {
            var someInfo = utils.GetInfo()
            var InfoInJson = ToJson(someInfo)
            publicKey := &rsa.PublicKey{N: modulus, E: exponent}
            var encryptedMessage, err = rsa.EncryptOAEP(sha256.New(),rand.Reader, publicKey,[]byte(InfoInJson), []byte(""))
            var response = b64.StdEncoding.EncodeToString(encryptedMessage)
            if err == nil {
                json.NewEncoder(responseWriter).Encode(response)
            }
        }

    }
}

func ToJson(model InfoModel) string {
    InfoInJson, errors := json.Marshal(model)
    if errors != nil {
        panic("An error occurred while serializing the response")
    }
    return string(InfoInJson)
}

当我收到 Base64 字符串中的响应时,我将其转换为 Byte[] 并尝试使用 RSACryptoServiceProvider 的相同实例对其进行解密,然后它抛出以下异常

Error occurred while decoding OAEP padding.

有什么帮助吗?

更新 例如,这是我在 GO

中收到的 base64 字符串

QUQ2NDlFRTlCQTA3Q0IxNEI1MTNDMzczQzBBMjNBOEQyMDI5MkVGQTBFMjgyNUIyMEEyMzM1MEE3OTUyNjgyQ0Y3MEFBQjJBMTZGMzQyNTM4MkU2RDZBRjU5M0IxRTI2MTE0OEIyQkFFRTY3MUVDMTQ1NDk1NjBDRkNEQUNCQzI3RUUxNDRFODZDQUI4RDBDOUY2OENBNTUwNUMxQjZGQkVBQjQ0MTlBMjg3RDhBRjgxRDUyREY3MEM0RDZDQTA5MkREMzk5Q0NEODU5Q0FGQzAzQ0JEQ0JBQzgwOTg3NDY0NThBMkY4NEREOTc1QjU5QTJBMUNBNzQxQTBDNkQ2RDs2NTUzNw==

这是我的 GO 应用发回的内容

QuWpWdEPSJR+l9UJTkh+heJJ/NpPwhz/hVVu1VdKYdz37YGWWdKTj7Fc5lZ3A8p1WjtC4F+yieZCz0tEatCqTpRmm9g6Oioyjbtr9qGTxO/PE+GA33YyBe6nmMRe674SPePx/fg6l3nnfSZ4/+iLCV4bNgyNqFHCaXc7H4Snms8=

更新 2 我已经更新了代码片段并包含了数据类型,这里是对从 GO 端点接收到的内容进行解密的部分

public byte[] Decrypt(byte[] encryptedData, RSAParameters rsaParameters)
    {
        RsaProvider.ImportParameters(rsaParameters);
        return RsaProvider.Decrypt(encryptedData, true);
    }

我收到一个 base64 字符串,然后我使用这个

转换为 byte[]
byte[] b = Convert.FromBase64String(responseString); byte[] decryptedBytes=crypto.Decrypt(b, crypto.RsaProvider.ExportParameters(false));

crypto 是 class 的实例,它包含解密逻辑、RSACryptoServiceProvider 的实例以及上面给出的返回 public 密钥

的方法 (ConvertToPublicKey)

你有

var encryptedMessage, err = rsa.EncryptOAEP(sha256.New(), ...

我将继续假设是带有 SHA-2-256 的 OAEP。

在 C# 中你有

return RsaProvider.Decrypt(encryptedData, true);

这是带有 SHA-1 的 OAEP。

您需要放弃 RSACryptoServiceProvider。如果你切换到 RSACng,你可以用

解密它
using (RSA rsa = new RSACng())
{
    rsa.ImportParameters(rsaParameters);
    return rsa.Decrypt(encryptedData, RSAEncryptionPadding.OaepSHA256);
}

当我在这里时:

ConvertToPublicKey 中导出 XML,解析 XML,并将其转换为字节数组。为什么不直接调用 ExportParameters(false) 并直接提取模数和指数字节数组?

在解决了@bartonjs 的评论后,我也将响应 response 从 go 更改为

var encryptedMessage, err = rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, []byte(licenseInformationJson), []byte(""))
if err == nil {
    responseWriter.Write([]byte(encryptedMessage))
}

注意 []byte(encryptedMessage)go 发送到 c# 的字节流略有变化,因为 encryptedMessage[]unint8。所以你需要将 encryptedMessage 类型转换为 []byte 以便在 c# 中正确映射值。