|
//------------------------------------------------------------------------
// (c) 2002-2004 by Per Anderson. All rights reserved.
//------------------------------------------------------------------------
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Security.Cryptography;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Bullfrog.Compress.RSA;
namespace Bullfrog.KeyGen
{
public class KeyGenApp
{
protected const string _appname = "KeyGen";
public KeyGenApp()
{
}
protected KeyGenForm _ctrl = null;
protected KeyGenForm Ctrl
{
get
{
if (_ctrl == null)
_ctrl = new KeyGenForm();
return _ctrl;
}
}
protected void Prep()
{
// RSA XML keypair file
Ctrl.BtnOpen.Click += new EventHandler(BtnOpenXml_Click);
// Binary SNK or PUB file made with sn.exe
Ctrl.BtnOpenSnk.Click += new EventHandler(BtnOpenSnk_Click);
Ctrl.BtnOpenPub.Click += new EventHandler(BtnOpenPub_Click);
Ctrl.BtnCreate.Click += new EventHandler(BtnCreate_Click);
Ctrl.BtnSignIt.Click += new EventHandler(BtnSignIt_Click);
Ctrl.BtnVerifyIt.Click += new EventHandler(BtnVerifyIt_Click);
}
public void Go()
{
Prep();
Application.Run(Ctrl);
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
KeyGenApp app = new KeyGenApp();
app.Go();
}
protected Signer _signer = null;
protected Signer Signer
{
get
{
return _signer;
}
}
protected void Reset()
{
_signer = new Signer();
UpdateKeyUI(/*pubonly*/ false);
}
protected void ResetSnk(string xmlkey)
{
_signer = new Signer(xmlkey);
UpdateKeyUI(/*pubonly*/ false);
}
protected void ResetPub(string xmlkey)
{
_signer = new Signer(xmlkey);
UpdateKeyUI(/*pubonly*/ true);
}
protected void ResetSnk(byte[] snkbuf)
{
_signer = new Signer(snkbuf);
UpdateKeyUI(/*pubonly*/ false);
}
protected void ResetPub(byte[] pubbuf)
{
_signer = new Signer(pubbuf);
UpdateKeyUI(/*pubonly*/ true);
}
protected void UpdateKeyUI(bool pubonly)
{
Ctrl.TextBoxKeyPriv.Text = "";
Ctrl.TextBoxKeyPub.Text = "";
Ctrl.BtnSignIt.Enabled = false;
if (Signer == null) return;
Ctrl.TextBoxKeyPub.Text = Signer.PubKeyXml;
if (pubonly) return;
Ctrl.TextBoxKeyPriv.Text = Signer.PrivKeyXml;
Ctrl.BtnSignIt.Enabled = true;
}
protected void BtnOpenXml_Click(object sender, EventArgs ea)
{
StreamReader sr = null;
try
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() != DialogResult.OK) return;
sr = new StreamReader(dlg.FileName);
string buf = sr.ReadToEnd();
ResetSnk(buf);
}
catch(Exception e)
{
MessageBox.Show(Ctrl, e.Message, _appname, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
if (sr != null)
sr.Close();
}
}
protected void BtnSignIt_Click(object sender, EventArgs ea)
{
try
{
string plain = Ctrl.TextBoxPlain.Text;
if ((plain == null) || (plain.Length < 1)) goto fail;
if (Signer == null) goto fail;
string sig = Signer.SignToString(plain);
if ((sig == null) || (sig.Length < 1)) goto fail;
Ctrl.TextBoxSignature.Text = sig;
return;
fail:
MessageBox.Show(Ctrl, "Bad Params", _appname,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch(Exception e)
{
MessageBox.Show(Ctrl, e.Message, _appname,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
protected void BtnVerifyIt_Click(object sender, EventArgs ea)
{
try
{
if (Signer == null) goto fail;
bool result = Signer.VerifySignature(
Ctrl.TextBoxPlain.Text, Ctrl.TextBoxSignature.Text);
if (!result) goto fail;
MessageBox.Show(Ctrl, "Verified", _appname,
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
fail:
MessageBox.Show(Ctrl, "Failed", _appname,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(Exception e)
{
MessageBox.Show(Ctrl, e.Message, _appname, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
protected void BtnCreate_Click(object sender, EventArgs ea)
{
Reset();
}
protected void BtnOpenSnk_Click(object sender, EventArgs ea)
{
try
{
byte[] snkbuf = GetFileBytes();
if ((snkbuf == null) || (snkbuf.Length < 1))
{
MessageBox.Show(Ctrl, "Error opening file", _appname, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
ResetSnk(snkbuf);
}
catch(Exception e)
{
MessageBox.Show(Ctrl, e.Message, _appname, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
protected void BtnOpenPub_Click(object sender, EventArgs ea)
{
try
{
byte[] pubbuf = GetFileBytes();
if ((pubbuf == null) || (pubbuf.Length < 1))
{
MessageBox.Show(Ctrl, "Error opening file", _appname, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
ResetPub(pubbuf);
}
catch(Exception e)
{
MessageBox.Show(Ctrl, e.Message, _appname, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
protected byte[] GetFileBytes()
{
FileStream fs = null;
BinaryReader br = null;
try
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() != DialogResult.OK) return null;
string fname = dlg.FileName;
if ((fname == null) || (fname.Length < 1)) return null;
if (!File.Exists(fname)) return null;
// Get bytes
fs = new FileStream(fname, FileMode.Open, FileAccess.Read);
br = new BinaryReader(fs);
byte[] ret = br.ReadBytes((int) fs.Length);
return ret;
}
finally
{
if (fs != null) fs.Close();
if (br != null) br.Close();
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Per Anderson is the founder of Sunfrog Technologies LLC, http://sunfrog-tech.com .