简体   繁体   English

由于指数大小有限,如何在C#中创建非对称键?

[英]How to create asymmetric keys in C# because of limited exponent size?

I am creating a little software to encrypt and decrypt data using asymmetric keys. 我正在创建一个小的软件,用于使用非对称密钥加密和解密数据。

The problem is, I am coding in C# and even if I use : 问题是,即使我使用C#进行编码,也可以:

 BigInteger.Pow(BigIntenger myNumber, int myExponent);

The exponent is an "int" and my value is to big for an int. 指数是一个“整数”,而我的价值对于一个整数来说意义重大。

Just to quickly explain and to be sure I am not doing any mistake, you have to use big numbers to make it more difficult to decrypt without having the private key. 只是为了快速解释并确保我没有做任何错误,您必须使用大数字以使其更难于在没有私钥的情况下解密。

So I have 所以我有

  • N = P * Q N = P * Q
  • P and Q are both prime numbers. P和Q都是素数。
  • M = (P-1)+(Q-1) M =(P-1)+(Q-1)
  • C is a prime number with M C是带有M的质数
  • Then find U with : C×U+M×V=1 然后用以下公式找到U:C×U + M×V = 1

Public key (N,C). 公钥(N,C)。

Private key (U,N). 私钥(U,N)。

For example you want to encrypt : "Bonjour !" 例如,您要加密:“ Bonjour!” to UTF8. 至UTF8。

You will have : 您将拥有 :

B⇔66 / o⇔111 / n⇔110 / j⇔106 / o⇔111 / u⇔117 / r⇔114 / (espace)⇔32 / !⇔33 B⇔66/o⇔111/n⇔110/j⇔106/o⇔111/u⇔117/r⇔114/(空格)⇔32/!⇔33

Then raise each numbers to the power of C and modulo N. 然后将每个数字提高到C的幂和取N的模。

Ex : valueOfB = (66^C)%N. 例如:valueOfB =(66 ^ C)%N。

Now your message is encrypted. 现在,您的消息已加密。

If you want to decrypt it, you have to multiply each value by exponent U and modulo N. 如果要解密,则必须将每个值乘以指数U和取模N。

Ex : (valueOfB^U)%N 例:(valueOfB ^ U)%N

So I can do this only if I use small number because I will have a small U value that fit with a "int" but it's not secure, how can I do this with a big U like BigInteger and not int ? 因此,仅当我使用较小的数字时,我才能执行此操作,因为我将拥有一个较小的U值并适合“ int”,但它并不安全,如何使用像BigInteger而不是int这样的大U来执行此操作?

BigInteger.Pow of a BigInteger would be a massively complicated number. BigInteger.Pow的功率将是一个非常复杂的数字。

Binary multiplication has the property that (roughly speaking) multiplying an n -bit number by an m -bit number produces an approximately (n+m) -bit answer. 二进制乘法的性质是(大致而言)将n位数字乘以m位数字可产生大约(n+m)位的答案。

10 * 4096 = 0b1010 * 0b1_0000_0000_0000  (4 bits, 13 bits)
40960 = 0b1010_0000_0000_0000 (16 bits)

16 * 4096 = 0b1_0000 * 0b1_0000_0000_0000  (5 bits, 13 bits)
65536 = 0b1_0000_0000_0000_0000 (17 bits)

15 * 4095 = 0b1111 * 0b1111_1111_1111 (4 bits, 12 bits)
61425 = 0b1110_1111_1111_0001 (16 bits)

Since exponentiation is repeated multiplication, and multiplication is repeated addition, we can see that raising a 1024-bit number to the power of a 512-bit number would produce an answer in the realm of 1024*512 bits (524288 bits, 65536 bytes). 由于乘幂是重复乘法,而乘法是重复加法,所以我们可以看到将1024位数字提高到512位数字的幂将在1024 * 512位(524288位,65536字节)的范围内产生答案。

But you're then going to follow it up with a modulus operation, bringing it back down the realm of a 1024-bit number. 但是您将随后进行模运算,将其放回到1024位数字的范围内。 That's pretty wasteful. 那真是浪费。

Thankfully, algorithms exist for doing efficient modular exponentiation . 幸运的是,存在用于进行有效的模幂运算的算法。 Double thankfully for you, .NET went ahead and wrote that for you. 双重感谢您,.NET继续为您编写了该代码。

What you're looking for is 您正在寻找的是

valueOfB = BigInteger.ModPow(66, U, N);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM