[C#] Problem z przetwarzaniem danych

0

Witam.
Napisałem sobie program do szyfrowania danych, mam klasę która je reprezentuje i szyfrowanie odbywa sie w metodzie:
[code]public static void WriteEncrypt(Data msg, string patch)
{
FileStream fs = new FileStream(patch, FileMode.OpenOrCreate, FileAccess.Write);
DESCryptoServiceProvider crypt = new DESCryptoServiceProvider();
crypt.Key = new byte[] { 71, 72, 83, 84, 85, 96, 97, 98 };
crypt.IV = new byte[] { 71, 72, 83, 84, 85, 96, 97, 98 };
CryptoStream cs = new CryptoStream(fs, crypt.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.WriteLine(msg);
sw.Close();
cs.Close();
fs.Close();
}[/code]
Metoda deszyfrująca wygląda tak:
[code]public static List<string> ReadEncrypt(string patch)
{
FileStream fs = new FileStream(patch, FileMode.Open, FileAccess.ReadWrite);
DESCryptoServiceProvider crypt = new DESCryptoServiceProvider();
crypt.Key = new byte[] { 71, 72, 83, 84, 85, 96, 97, 98 };
crypt.IV = new byte[] { 71, 72, 83, 84, 85, 96, 97, 98 };
CryptoStream cs = new CryptoStream(fs, crypt.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
string msg = null;
List<string> list = new List<string>();
while ((msg = sr.ReadLine()) != null)
list.Add(msg);
sr.Close();
cs.Close();
fs.Close();
return list;
}[/code]
Potem elementy tej listy są wysyłane do metody:
[code]public static Data ToDane(string msg)
{
int index1 = msg.IndexOf("^");
int index2 = msg.IndexOf("^", (index1 + 1));

        string[] dane = new string[3];
        dane[0] = msg.Substring(0, (index1-1));
        dane[1] = msg.Substring(index1 + 1, (index2 - index1) - 1);
        dane[2] = msg.Substring(index2 + 1, (msg.Length - index2) - 1);

        Data d = new Data();
        d.sLogin = dane[0];
        d.sPassword = dane[1];
        d.sInfo = dane[2];

        return d;
    }[/code] 

I wszystko w tym miejscu jest ok, ale gdy dodam jeszcze jeden obiekt do szyfrowania to po deszyfrowaniu jest tylko ostatnio dodany, gdy zmienię w metodzie WriteEncrypt konstruktor strumienia na:
[code]FileStream fs = new FileStream(patch, FileMode.Appened, FileAccess.Write);[/code]
to w metodzie ToDane w lini
[code]dane[0] = msg.Substring(0, (index1-1));[/code]
wypluwa błąd, że długość jest zerowa. Pomóżcie mi jak zrobić, by w jednym pliku można było szyfrowac więcej niż tylko jeden obiekt.

ps. Przeciążona metoda ToString() klasy Data wygląda tak:
[code] public override string ToString()
{
string msg = sLogin + "" + sPassword + "" + sInfo;
return msg;
}[/code]

ps2.Cały projekt jest tutaj: http://rapidshare.com/files/132547186/Szyfr.rar

0

Ścieżka w angielskim to "path", a nie "patch".

0
Rev.pl napisał(a)

Ścieżka w angielskim to "path", a nie "patch".

To akurat nie ma wpływu na przebieg działania programu :-P :-P

1 użytkowników online, w tym zalogowanych: 0, gości: 1