如何 RSA 模数 &如果两者都是十进制格式,则指数在 C# 中有效 [英] How to RSA Modulus & Exponent works in C# if both are in decimal format

查看:58
本文介绍了如何 RSA 模数 &如果两者都是十进制格式,则指数在 C# 中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 RSA 加密,并在 C# 中有我的公钥.但我的公钥模数和指数采用十进制格式,如下所示:

I'm working on RSA encryption and have my public key in C#. But i have my public key modulus and exponent in decimal format as below :

模数:18597082174523508716390621410767314599038866539779750637065684697259605002694360104971398651747704217448206242771805831180528356170981586469477958663193117845356353634469679095227815268434823260637917891539622982485837392495877800705071553435850492058570460745900129552907596604479063007676795998193064078987369363544131073880694736862904482385332020513837955197528182597410203652025183467149166026077910473816908590029574674997850683021938033561647681780168764842253700974777073181357779101690539999736174329578178742236883520017849893817175274405622018571899733008344137833140207194792223664500885734080606246950229

modulus: 18597082174523508716390621410767314599038866539779750637065684697259605002694360104971398651747704217448206242771805831180528356170981586469477958663193117845356353634469679095227815268434823260637917891539622982485837392495877800705071553435850492058570460745900129552907596604479063007676795998193064078987369363544131073880694736862904482385332020513837955197528182597410203652025183467149166026077910473816908590029574674997850683021938033561647681780168764842253700974777073181357779101690539999736174329578178742236883520017849893817175274405622018571899733008344137833140207194792223664500885734080606246950229

公共指数:65537

我如何在 c# 中使用上面的 RSA 加密?

How can i use above for RSA encryption in c#?

推荐答案

你可以试试:

public static class BigIntegerExtensions
{
    public static byte[] ToByteArrayBigEndianUnsigned(this BigInteger bi, int minSize = 0)
    {
        byte[] bytes = bi.ToByteArray();

        int length;

        if (bytes[bytes.Length - 1] != 0)
        {
            if (minSize == 0 || minSize <= bytes.Length)
            {
                Array.Reverse(bytes);
                return bytes;
            }

            length = bytes.Length;
        }
        else
        {
            length = bytes.Length - 1;
        }

        var bytes2 = new byte[minSize == 0 ? length : Math.Max(minSize, length)];

        for (int i = 0, j = bytes2.Length - 1; i < length && j >= 0; i++, j--)
        {
            bytes2[j] = bytes[i];
        }

        return bytes2;
    }
}

var bi1 = BigInteger.Parse("18597082174523508716390621410767314599038866539779750637065684697259605002694360104971398651747704217448206242771805831180528356170981586469477958663193117845356353634469679095227815268434823260637917891539622982485837392495877800705071553435850492058570460745900129552907596604479063007676795998193064078987369363544131073880694736862904482385332020513837955197528182597410203652025183467149166026077910473816908590029574674997850683021938033561647681780168764842253700974777073181357779101690539999736174329578178742236883520017849893817175274405622018571899733008344137833140207194792223664500885734080606246950229");
var bi2 = BigInteger.Parse("65537");
var bytes1 = bi1.ToByteArrayBigEndianUnsigned();
var bytes2 = bi2.ToByteArrayBigEndianUnsigned();

var pars = new RSAParameters();
pars.Modulus = bytes1;
pars.Exponent = bytes2;

BigInteger.ToByteArray() 如何导出到 byte[] 以及 RsaParameters 如何期望其参数有两个不同之处:BigInteger 是有符号的和小端的,所以一些 Modulus 附加了一个 0 使它们成为正数(对于 1024 位,密钥是 129 字节长而不是 128 字节),最高有效数字是最后一个,RsaParameters 期望它的参数是无符号的(所以 1024 位的密钥必须是 128 字节长)和大端(所以最重要的数字是第一个).

There are two differences about how BigInteger.ToByteArray() exports to byte[] and how RsaParameters expects its parameters: BigInteger is signed and little endian, so some Modulus have a 0 appended to make them positive (and for 1024 bits keys are 129 bytes long instead of 128) and the most significant digit is the last one, RsaParameters expects its parameters to be unsigned (so a 1024 bit key must be 128 bytes long) and big endian (so the most significant digit is the first one).

请注意,从 BigInteger 导入私钥更加复杂:-)

Note that importing the private key from BigInteger is even more complex :-)

注2:ToByteArrayBigEndianUnsigned()中的minSize用于导入InvariantQ.Length <的私钥.指数.长度/2.忽略它.

Note 2: the minSize in ToByteArrayBigEndianUnsigned() is for importing private keys with InvariantQ.Length < Exponent.Length / 2. Ignore it.

这篇关于如何 RSA 模数 &amp;如果两者都是十进制格式,则指数在 C# 中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆