我已经看了CodeProject上的一篇文章,介绍了如何使用RSA提供程序进行加密和解密:
虽然2009年的旧版本存在缺陷,但新版2012年(带有System.Numerics.BigInteger支持)似乎更可靠。但是这个版本缺少一种使用公共密钥进行加密并使用私人密钥进行解密的方法。
因此,我自己尝试了一下,但在解密时得到了垃圾数据。我不熟悉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”。
然后是配对(使用私钥进行解密):
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”还是指数符号?
显然我需要加密工作双向私钥公钥和公钥私钥。 非常感谢任何帮助!