1

假设我们有以下 JWK 作为项目中的嵌入式资源

在此处输入图像描述

以下代码成功将 JSON 读入 JSONWebKey

 JsonWebKey jwk = ReadResource();

我们希望将其转换为 RSA 密钥值对,因此我们有以下代码可以将 JWK 转换为 RSA 密钥对。

            UnicodeEncoding ByteConverter = new UnicodeEncoding();

            RSAParameters publicKey = new RSAParameters();
            RSAParameters privateKey = new RSAParameters();

            JsonWebKey jwk = ReadResource();

           // PUBLIC KEY

            publicKey.Exponent = ByteConverter.GetBytes(jwk.E);
            publicKey.Modulus = ByteConverter.GetBytes(jwk.N);

            // PRIVATE KEY

            privateKey.Exponent = ByteConverter.GetBytes(jwk.E);
            privateKey.Modulus = ByteConverter.GetBytes(jwk.N);
            privateKey.D = ByteConverter.GetBytes(jwk.D);
            privateKey.DP = ByteConverter.GetBytes(jwk.DP);
            privateKey.DQ = ByteConverter.GetBytes(jwk.DQ);
            privateKey.P = ByteConverter.GetBytes(jwk.P);
            privateKey.Q = ByteConverter.GetBytes(jwk.Q);
            privateKey.InverseQ = ByteConverter.GetBytes(jwk.QI);

但是当我们尝试如下签署数据时

            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(2048);
            RSAalg.ImportParameters(privateKey);
            return RSAalg.SignData(DataToSign, SHA256.Create());

它抛出 CryptographicException @ RSAalg.ImportParameters(privateKey);

使用 C#.net 核心将 JWK 转换为 RSA 密钥对的正确方法是什么。

4

0 回答 0