RSA - Encryption with negative exponent

廉价感情. 提交于 2019-12-11 13:17:28

问题


Well guys I am trying to encrypt (actually sign) data using Public and Private exponent and modulus, It is in C#.NET and I can't use RSACryptoServiceProvider because it needs also both prime numbers and other CRT stuff.

So I am trying to do following:

private Byte[] signData()
{
  BigInteger biPrivEx = new BigInteger(this.privEx); // Those are byte[]
  BigInteger biPubEx = new BigInteger(this.pubEx);
  BigInteger biMod = new BigInteger(this.mod);          

  BigInteger cyph = BigInteger.ModPow(new BigInteger(pkcs11), biPrivEx, biMod); // This raise exception

  return cyph.ToByteArray();;
}

But the problem is I am getting Out Of Range Exception because my private exponent is negative number.

What am I doing wrong? Or is possible to easily recovery CRT from this? Or maybe is there any better way how to do it? In different program I am able to this with data I am using, so I have got reference to verify it.


回答1:


The problem is that you got a negative private exponent in the first place. Depending on how you got this broken exponent try:

  1. Adding n to it
  2. Concating a 00 byte to the array, to make it parse correctly.

You should also be careful about endianness issues. .net's BigInteger uses little endian, other binary formats might use big endian.


Try:

BigInteger ParseBinaryLE(byte[] raw)
{
   return new BigInteger(raw.Concat(new byte[]{0}).ToArray());
}

BigInteger ParseBinaryBE(byte[] raw)
{
   return new BigInteger(raw.Reverse().Concat(new byte[]{0}).ToArray());
}

AFAIK it is also possible to recover P and Q (and from those the rest of the parameters) when you know e, d and n.



来源:https://stackoverflow.com/questions/9430343/rsa-encryption-with-negative-exponent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!