我用大数算法解密后得不到明文,请教大虾们!!!!环境: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();
}
}
引用: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();
}
}
解决方案 »
- C#遍历FTP的中文名文件出现乱码
- 高分求救我一命 dev gridView rowcellclick事件怎么触发
- 怎么通过形状数据中的数据找到该形状 c# visio
- 求教,如何遍历选定的文件夹底下的图片
- 求教Document.Activate这句话是什么意思???
- 各位兄弟帮忙看一下,这段代码错在那里?总提示匹配不对!很简单,谢谢
- 跪求各位大侠--如何将rdf文件转换成Pdf文件
- WinForm下的 DataGrid 中加一列 CheckBox的问题!急请帮忙!!
- 大婶进,一个关于AppDomain.Load的问题
- 写什么代码都没反应了
- usb口模拟串口
- Form1 点键钮刷新Form2 方法 两个form 都不消失,求解
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);