Witam. Jestem jeszcze ciągle na etapie szkolenia się. Próbuję właśnie sił w szyfrowaniu plików. Kod, który niżej wkleję teoretycznie działa. Napisałem go posługując się docsami z microsoftu, ale jest tu jednak coś nie tak. Po zaszyfrowaniu i odszyfrowaniu tekstu, wynik jest obcinany. Nie odzyskuję już pierwotnej wersji pliku testowego. Czy możecie mi wyjaśnić dlaczego? Oto kod:
using System;
using System.IO;
using System.Security.Cryptography;
namespace Encryptor
{
class FileOperations
{
byte[] tmp1;
public byte[] Tmp1 { get => tmp1; set => tmp1 = value; }
public void Encode(string e)
{
Tmp1 = File.ReadAllBytes(e);
RijndaelManaged RMCrypto = new RijndaelManaged();
byte[] Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
using (FileStream fs = File.Create(e))
{
CryptoStream cryptoStream = new CryptoStream(fs, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
BinaryWriter streamWriter = new BinaryWriter(cryptoStream);
foreach (var item in Tmp1)
streamWriter.Write(item);
}
}
public void Decode(string e)
{
Tmp1 = File.ReadAllBytes(e);
RijndaelManaged RMCrypto = new RijndaelManaged();
byte[] Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
using (FileStream fs = File.Create(e))
{
CryptoStream cryptoStream = new CryptoStream(fs, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Write);
BinaryWriter streamWriter = new BinaryWriter(cryptoStream);
foreach (var item in Tmp1)
streamWriter.Write(item);
}
}
}
class Program
{
static void Main(string[] args)
{
FileOperations fo = new FileOperations();
Console.WriteLine("(k)odujemy czy (d)ekodujemy?");
char c = char.Parse(Console.ReadLine());
foreach (var item in args)
{
if (c == 'k')
fo.Encode(item);
else
fo.Decode(item);
}
}
}
}