我正在開發一個項目。加密工作正常,但當涉及到解密我的程序正在拋出「壞數據異常」。我該如何解決這個問題?RSA解密期間的數據異常異常
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
byte[] cipher;
byte[] plain;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private static string Encrypt(byte[] plain)
{
byte[] encrypted;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
StreamReader StRe = new StreamReader("D:\\PjesaVetemPublike.xml");
string VetemPublikeXML = StRe.ReadToEnd();
rsa.FromXmlString(VetemPublikeXML);
StRe.Close();
encrypted = rsa.Encrypt(plain, true);
return Encoding.UTF8.GetString(encrypted);
}
private static string Decrypt(byte[] encrypted)
{
byte[] decrypted;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
StreamReader StRe = new
StreamReader("D:\\PjesaPublikeDhePrivate.xml");
string PublikeDhePrivate = StRe.ReadToEnd();
rsa.FromXmlString(PublikeDhePrivate);
StRe.Close();
decrypted = rsa.Decrypt(encrypted, false); //THE ERROR APPEARS RIGHT HERE
return Encoding.UTF8.GetString(decrypted);
}
private void button1_Click(object sender, EventArgs e)
{
plain = Encoding.UTF8.GetBytes(txtPlain.Text);
txtCipher.Text = Encrypt(plain);
}
private void button2_Click(object sender, EventArgs e)
{
txtDekriptuar.Text = Decrypt(cipher);
}
}
}
這可能是一個壞鑰匙的跡象。檢查你的密鑰 – mjw