【发布时间】:2013-03-20 02:57:50
【问题描述】:
一段时间以来,我在 CodeProject 上看过一篇文章,其中解释了如何使用 RSA 提供程序进行加密和解密:
虽然 2009 年的旧版本有问题,但 2012 年的新版本(支持 System.Numerics.BigInteger)似乎更可靠。不过,这个版本缺少的是一种使用 public 密钥加密 和使用 private 密钥解密 的方法。
所以,我自己尝试过,但解密时会出现垃圾。我不熟悉 RSA 提供商,所以我在这里一无所知。很难找到更多关于这应该如何工作的信息。
有人知道这有什么问题吗? 以下是使用公钥加密:
// Add 4 byte padding to the data, and convert to BigInteger struct
BigInteger numData = GetBig( AddPadding( data ) );
RSAParameters rsaParams = rsa.ExportParameters( false );
//BigInteger D = GetBig( rsaParams.D ); //only for private key
BigInteger Exponent = GetBig( rsaParams.Exponent );
BigInteger Modulus = GetBig( rsaParams.Modulus );
BigInteger encData = BigInteger.ModPow( numData, Exponent, Modulus );
return encData.ToByteArray();
执行此操作时是否使用提供商提供的大“D”?可能不是,因为它是没有“D”的公钥。
然后是对应的(使用 PRIVATE 密钥解密):
BigInteger numEncData = new BigInteger( cipherData );
RSAParameters rsaParams = rsa.ExportParameters( true );
BigInteger D = GetBig( rsaParams.D );
//BigInteger Exponent = GetBig( rsaParams.Exponent );
BigInteger Modulus = GetBig( rsaParams.Modulus );
BigInteger decData = BigInteger.ModPow( numEncData, D, Modulus );
byte[] data = decData.ToByteArray();
byte[] result = new byte[ data.Length - 1 ];
Array.Copy( data, result, result.Length );
result = RemovePadding( result );
Array.Reverse( result );
return result;
这里需要“D”还是指数?
显然,我需要加密货币在私人-公共-公共-私人两种方式下工作。 非常感谢任何帮助!
【问题讨论】:
标签: c# .net cryptography rsa encryption-asymmetric