微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将JWK转换为RSA密钥值对的正确方法是什么

如何解决将JWK转换为RSA密钥值对的正确方法是什么

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

enter image description here

以下代码成功将JSON读入JSONWebKey

 JsonWebKey jwk = ReadResource();

我们希望将其转换为RSA密钥值对,因此我们期望使用以下代码将JWK转换为RSA KeyPair。

            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密钥对的正确方法是什么。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。