Zielstellung: Benutzereingabe in Form - Passwort soll gespeichert werden.
Passwort wird verschlüsselt und dann in eine Textdatei geschrieben und bei Bedarf wieder entschlüsselt.
Ich weiss, ich habe einen Fehler in der Denkweise, aber ich komme irgendiwe nicht drauf!
class CCrypt
{
public byte[] Crypt(string StrInput, out byte[] ByKey, out byte[] ByIV)
{
try
{
RijndaelManaged RMRijndal = new RijndaelManaged();
ASCIIEncoding AEConvert = new ASCIIEncoding();
byte[] ByEncrypted;
byte[] ByToEncrypt;
RMRijndal.GenerateKey();
RMRijndal.GenerateIV();
ByKey = RMRijndal.Key;
ByIV = RMRijndal.IV;
ICryptoTransform ICTEncryptor = RMRijndal.CreateEncryptor(ByKey, ByIV);
MemoryStream MsEncrypt = new MemoryStream();
CryptoStream CsEncrpyt = new CryptoStream(MsEncrypt, ICTEncryptor, CryptoStreamMode.Write);
ByToEncrypt = AEConvert.GetBytes(StrInput);
CsEncrpyt.Write(ByToEncrypt, 0, ByToEncrypt.Length);
CsEncrpyt.FlushFinalBlock();
ByEncrypted = MsEncrypt.ToArray();
return ByEncrypted;
}
catch (CryptographicException e)
{
MessageBox.Show("A Cryptographic error occurred: {0}", e.Message);
ByKey = null;
ByIV = null;
return null;
}
}
class CDecrypt
{
public string Decrypt(byte[] ByInput, byte[] ByKey, byte[] ByIV)
{
try
{
RijndaelManaged RMRijndal = new RijndaelManaged();
ASCIIEncoding AEConvert = new ASCIIEncoding();
byte[] BySource;
ICryptoTransform ICDecryptor = RMRijndal.CreateDecryptor(ByKey, ByIV);
MemoryStream MsDecrypt = new MemoryStream(ByInput);
CryptoStream CsDecrypt = new CryptoStream(MsDecrypt, ICDecryptor, CryptoStreamMode.Read);
BySource = new byte[ByInput.Length];
CsDecrypt.Read(BySource, 0, BySource.Length);
string StrOutput = AEConvert.GetString(BySource);
return StrOutput;
}
catch (CryptographicException e)
{
MessageBox.Show("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
private void BtSave_Click(object sender, EventArgs e)
{
byte[] ByKey, ByIV;
CCrypt crypt = new CCrypt();
ASCIIEncoding AEEncoding = new ASCIIEncoding();
CSqlSettings settings = new CSqlSettings();
String[] AStrSettings = new string[6];
AStrSettings[0] = TbServername.Text;
AStrSettings[1] = TbUsername.Text;
AStrSettings[2] = AEEncoding.GetString(crypt.Crypt(TbPassword.Text, out ByKey, out ByIV));
AStrSettings[3] = CbDatabase.SelectedItem.ToString();
AStrSettings[4] = AEEncoding.GetString(ByKey);
AStrSettings[5] = AEEncoding.GetString(ByIV);
settings.WriteSetting(AStrSettings);
BtCancel.Text = "Close";
BtSave.Enabled = false;
}
Daniel