Cześć chcę napisać program do szyfrowania i odszyfrowywania plików (np. .jpg, .doc itp.) algorytmem RSA, problem pojawia się podczas odszyrowywania występuję błąd "Złe dane" podczas odszyfrowywania pliku.
Kod programu:
Metoda szyfrująca:
System.IO.FileStream fin = System.IO.File.Open(inf, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
System.IO.FileStream fout = System.IO.File.Open(outf, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Write);
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAKeyInfo);
int licznik = 0;
byte[] buffer = new byte[RSAKeyInfo.Modulus.Length-48];
byte[] encrypt_buffer;
int len = 0;
long size_fin = fin.Length;
while(licznik<size_fin)
{
len=fin.Read(buffer, 0, buffer.Length);
encrypt_buffer= RSA.Encrypt(buffer, DoOAEPPadding);
fout.Write(encrypt_buffer, 0, encrypt_buffer.Length);
licznik += len;
}
fin.Close();
fin.Dispose();
fout.Close();
fout.Dispose();
Metoda deszyfrująca:
System.IO.FileStream fin = System.IO.File.Open(inf, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
string outName = outf.Remove(outf.Length - 4, 4);
System.IO.FileStream fout = System.IO.File.Open(outName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Write);
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAKeyInfo);
int licznik = 0;
byte[] decrypt_buffer;
int len = 0;
long size_fin = fin.Length;
byte[] buffer = new byte[RSAKeyInfo.Modulus.Length-48];
while (licznik < size_fin)
{
len = fin.Read(buffer, 0, buffer.Length);
decrypt_buffer = RSA.Decrypt(buffer, DoOAEPPadding);
fout.Write(decrypt_buffer, 0, decrypt_buffer.Length);
licznik += len;
}
fin.Close();
fin.Dispose();
fout.Close();
fout.Dispose();
Mógłby ktoś pomóc w czym tkwi błąd i jak go naprawić?