RSA非对称加密解密的问题,不能获得明文!!!!

大麦芽糖 2012-09-03 10:13:57
我用大数算法解密后得不到明文,请教大虾们!!!!

环境:VS2010,NET4.0
引用:using System.Security.Cryptography;
using System.Numerics;


public partial class FormRsaEncrypt : Form
{
public FormRsaEncrypt()
{
InitializeComponent();
this.Text = "RSA 加密解密";

textBoxEncrypt.ReadOnly = true;
textBoxDecrypt.ReadOnly = true;

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
this.txbPrivateKey.Text = RSA.ToXmlString(true);
this.txbPublicKey.Text = RSA.ToXmlString(false);
RSA.Clear();
}

private void buttonOK_Click(object sender, EventArgs e)
{
//int TextLength = 128;
byte[] encryptedData;
byte[] decryptedData;

try
{
RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider();
RSA1.FromXmlString(this.txbPrivateKey.Text);
RSAParameters RSAKeyInfo = RSA1.ExportParameters(true);

//byte[] dataToEncrypt = GenerateBytes(TextLength);
byte[] dataToEncrypt = Encoding.UTF8.GetBytes(this.textBoxInput.Text);

encryptedData = RSAEncrypt(dataToEncrypt, RSAKeyInfo.Exponent, RSAKeyInfo.Modulus);
int x = encryptedData.Length;
this.textBoxEncrypt.Text = Convert.ToBase64String(encryptedData);

decryptedData = RSADecrypt(encryptedData, RSAKeyInfo.D, RSAKeyInfo.Modulus);
int y = decryptedData.Length;
this.textBoxDecrypt.Text = Convert.ToBase64String(decryptedData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

//***********************************************************************
// RSA Encrypt
//***********************************************************************
static public byte[] RSAEncrypt(byte[] dataToEncrypt, byte[] Exponent, byte[] Modulus)
{
BigInteger original = new BigInteger(dataToEncrypt);
BigInteger e = new BigInteger(Exponent);
BigInteger n = new BigInteger(Modulus);

BigInteger encrypted = BigInteger.ModPow(original, e, n);
return encrypted.ToByteArray();
}

//***********************************************************************
// RSA Decrypt
//***********************************************************************
static public byte[] RSADecrypt(byte[] encryptedData, byte[] D, byte[] Modulus)
{
BigInteger encrypted = new BigInteger(encryptedData);
BigInteger d = new BigInteger(D);
BigInteger n = new BigInteger(Modulus);

BigInteger decrypted = BigInteger.ModPow(encrypted, d, n);

return decrypted.ToByteArray();
}
}
...全文
228 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqtt789632147 2012-09-04
  • 打赏
  • 举报
回复

msdn上不是有现成了的么?为什么还要自己写大数算法?


//Pass the data to ENCRYPT, the public key information
//(using RSACryptoServiceProvider.ExportParameters(false),
//and a boolean flag specifying no OAEP padding.
encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);

//Pass the data to DECRYPT, the private key information
//(using RSACryptoServiceProvider.ExportParameters(true),
//and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
大麦芽糖 2012-09-04
  • 打赏
  • 举报
回复
再定!!!
大麦芽糖 2012-09-03
  • 打赏
  • 举报
回复
就差这一点了,帮帮忙啊!!!

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧