#region License
// CryptoManager - Logiciel de chiffrement de fichiers
// Copyright (C) 2008 G. Villard
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#endregion
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Windows.Forms;
namespace CryptoManager {
internal partial class MainForm : Form {
#region Variables
public const string FILE_EXT = ".cmcrypted";
public const string TABLE_FILE = "table.data";
private static readonly Func<string, bool> AskFunc = (string question) => { return MessageBox.Show(question, string.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes; };
#endregion
#region Procédure de lancement du cryptage
private void CryptageMain(WorkData wd) {
// WorkData wd = data as WorkData;
StringBuilder log = new StringBuilder();
if(wd == null)
return;
ICryptoObject co = CreateCryptoObject(wd, true);
if(co == null)
return;
if(!co.CanDecrypt) {
if(MessageBox.Show("certprivkey".GetResString(), "warn".GetResString(), MessageBoxButtons.YesNo) == DialogResult.No) {
co.Dispose();
GC.Collect();
return;
}
}
co.Init += new Action<int>(InitPB);
co.FileChange += new Action<string>(ChangerFichier);
co.TaskChange += new Action<string>(ChangerAction);
co.Increment += new Action(Increment);
co.Decrement += new Action(Decrement);
co.Alert += new Action<string>((string Message) => { MessageBox.Show(Message); });
co.Log += new Action<string>((string Message) => { log.AppendLine(Message); });
EventHandler handler = new EventHandler((object sender, EventArgs e) => { co.Cancel(); });
btnAnnuler.Click += handler;
Working();
co.Finished += new Action(() => {
btnAnnuler.Click -= handler;
co.Dispose();
co = null;
ChangerTache(string.Empty);
Waiting();
if(log.Length > 0) {
string f = DateTime.Now.ToString("yyyyMMddHHmmss") + "_cm.log";
try {
StreamWriter sw = new StreamWriter(f);
sw.Write(log.ToString());
sw.Flush();
sw.Close();
MessageBox.Show("logcreated".GetResString() + f);
} finally {
log.Clear();
}
}
GC.Collect();
});
co.Start();
}
#endregion
#region Procédure de lancement du decryptage
private void DecryptageMain(WorkData wd) {
// WorkData wd = data as WorkData;
StringBuilder log = new StringBuilder();
if(wd == null)
return;
ICryptoObject co = CreateCryptoObject(wd, false);
if(co == null)
return;
if(!co.CanDecrypt) {
MessageBox.Show("certcantdecrypt".GetResString(), "err".GetResString());
co.Dispose();
GC.Collect();
return;
}
co.Init += new Action<int>(InitPB);
co.FileChange += new Action<string>(ChangerFichier);
co.TaskChange += new Action<string>(ChangerAction);
co.Increment += new Action(Increment);
co.Decrement += new Action(Decrement);
co.Alert += new Action<string>((string Message) => { MessageBox.Show(Message); });
co.Log += new Action<string>((string Message) => { log.AppendLine(Message); });
EventHandler handler = new EventHandler((object sender, EventArgs e) => { co.Cancel(); });
btnAnnuler.Click += handler;
Working();
co.Finished += new Action(() => {
btnAnnuler.Click -= handler;
co.Dispose();
co = null;
ChangerTache(string.Empty);
Waiting();
if(log.Length > 0) {
string f = DateTime.Now.ToString("yyyyMMddHHmmss") + "_cm.log";
try {
StreamWriter sw = new StreamWriter(f);
sw.Write(log.ToString());
sw.Flush();
sw.Close();
MessageBox.Show("logcreated".GetResString() + f);
} finally {
log.Clear();
}
}
GC.Collect();
});
co.Start();
}
#endregion
#region Création d'un CrpytoObject
private static ICryptoObject CreateCryptoObject(WorkData wd, bool withPassCheck) {
switch(wd.Mode) {
case SecureDataMode.Password:
using (SecureString password = GetSecureString.Demand(withPassCheck)) {
if(password == null) {
return null;
}
return new CryptoPwd(wd, password.CreateKeyIV(), AskFunc);
}
case SecureDataMode.CertStore:
X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);
st.Open(OpenFlags.ReadOnly);
X509Certificate2Collection col = X509Certificate2UI.SelectFromCollection(st.Certificates, "certchoosetitle".GetResString(), "certchoosemsg".GetResString(), X509SelectionFlag.SingleSelection);
if(col == null)
return null;
if(col.Count == 0)
return null;
return new CryptoCert(wd, col[0], AskFunc);
case SecureDataMode.CertFile:
using(OpenFileDialog ofd = new OpenFileDialog()) {
if(ofd.ShowDialog() != DialogResult.OK)
return null;
try {
X509Certificate2 cert = new X509Certificate2();
cert.Import(ofd.FileName);
return new CryptoCert(wd, cert, AskFunc);
} catch(Exception ex) {
#if (DEBUG)
MessageBox.Show(ex.ToString());
#else
MessageBox.Show("certinvalidfile".GetResString());
#endif
return null;
}
}
}
return null;
}
#endregion
#region Cryptage du presse-papiers
private static void CrypterClipboard(object sender, EventArgs e) {
string data = null;
if(Clipboard.ContainsText(TextDataFormat.UnicodeText)) {
data = Clipboard.GetText(TextDataFormat.UnicodeText);
}
else if(Clipboard.ContainsText(TextDataFormat.Rtf)) {
data = Clipboard.GetText(TextDataFormat.Rtf);
}
else if(Clipboard.ContainsText()) {
data = Clipboard.GetText();
}
if(data == null) {
MessageBox.Show("errcbnotext".GetResString(), "err".GetResString(), MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
ICryptoObject cb = CreateCryptoObject(new WorkData() { Mode = SecureDataMode.Password }, true);
if(cb == null) {
MessageBox.Show("errcbgetico".GetResString(), "err".GetResString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Clipboard.SetText(data.Encrypt(cb));
cb.Dispose();
GC.Collect();
MessageBox.Show("cbcrypted".GetResString(), string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#endregion
#region Decryptage du presse-papiers
private static void DecrypterClipboard(object sender, EventArgs e) {
string data = null;
if(Clipboard.ContainsText()) {
data = Clipboard.GetText();
}
if(data == null) {
MessageBox.Show("errcbnotext".GetResString(), "err".GetResString(), MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
ICryptoObject cb = CreateCryptoObject(new WorkData() { Mode = SecureDataMode.Password }, false);
if(cb == null) {
MessageBox.Show("errcbgetico".GetResString(), "err".GetResString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string output = data.Decrypt(cb);
if(output == null) {
MessageBox.Show("pwdbad".GetResString(), "err".GetResString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
Clipboard.SetText(output);
cb.Dispose();
GC.Collect();
MessageBox.Show("cbdecrypted".GetResString(), string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#endregion
}
}