RSA - 加密与负指数 [英] RSA - Encryption with negative exponent

查看:128
本文介绍了RSA - 加密与负指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,大家好我想加密(实际签名)使用公共及私人指数和模量数据,这是在C#.NET,我不能用的RSACryptoServiceProvider ,因为它也需要两个素数和其他CRT的东西。

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.

我是什么做错了吗?或者可以从这个易于回收CRT?或者,也许是有没有更好的办法怎么办呢? 在不同的程序,我能与这我使用的数据,所以我有参考验证它。

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.

推荐答案

现在的问题是,你有一个负面的私人指数摆在首位。这取决于你如何得到这个破指数试:

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

  1. 添加 N
  2. Concating一个 00 字节数组,使之正确解析。
  1. Adding n to it
  2. Concating a 00 byte to the array, to make it parse correctly.

您也应该小心字节序的问题。 .NET的的BigInteger 使用小尾数,其他二进制格式可能使用大端。

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

尝试:

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它也可以恢复 P 问:(参数并从这些休息)时,你知道电子 D N


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.

这篇关于RSA - 加密与负指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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