centralkeymanager_src.zip
Core
Cryptography
WrappedData.xsx
Properties
KeyManager
Properties
TestClient
Properties
Settings.settings
Web References
KeyManager
Reference.map
Service.disco
Service.wsdl
SessionKeyInfo.datasource
|
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
namespace TestClient
{
public partial class Form1 : Form
{
RijndaelManaged _sessionKey = null;
public Form1()
{
InitializeComponent();
}
private void buttonGetKey_Click(object sender, EventArgs e)
{
X509Certificate2 certificate = comboBoxCertificates.SelectedItem as X509Certificate2;
if (certificate == null)
throw new ApplicationException("Certificate not selected");
int keyId;
if (Int32.TryParse(textBoxKeyId.Text, out keyId) == false || keyId <= 0)
throw new ApplicationException("Invalid KeyId specified");
Stopwatch sw = new Stopwatch();
sw.Start();
KeyManager.Service svc = new TestClient.KeyManager.Service();
KeyManager.SessionKeyInfo keyInfo = svc.GetSessionKey(keyId, certificate.Export(X509ContentType.SerializedCert));
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream, Encoding.UTF8);
keyInfo.WrappedKey.WriteTo(writer);
writer.Flush();
stream.Position = 0;
Core.Cryptography.WrappedData wrappedData = new Core.Cryptography.WrappedData();
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(stream);
wrappedData.ReadXml(reader);
string clearKey = Core.Cryptography.CryptHelper.AsymmetricDecrypt(wrappedData);
_sessionKey = Core.Cryptography.CryptHelper.GetSessionKey(clearKey);
sw.Stop();
toolStripStatusLabel1.Text = string.Format("GetKey took {0} ms", sw.ElapsedMilliseconds);
buttonDecrypt.Enabled = true;
buttonEncrypt.Enabled = true;
}
private void buttonCreate_Click(object sender, EventArgs e)
{
X509Certificate2 certificate = comboBoxCertificates.SelectedItem as X509Certificate2;
if (certificate == null)
throw new ApplicationException("Certificate not selected");
Stopwatch sw = new Stopwatch();
sw.Start();
KeyManager.Service svc = new TestClient.KeyManager.Service();
KeyManager.SessionKeyInfo keyInfo = svc.CreateSessionKey(certificate.Export(X509ContentType.SerializedCert));
textBoxKeyId.Text = keyInfo.KeyId.ToString();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream, Encoding.UTF8);
keyInfo.WrappedKey.WriteTo(writer);
writer.Flush();
stream.Position = 0;
System.IO.StreamReader r = new System.IO.StreamReader(stream);
string xml = r.ReadToEnd();
Core.Cryptography.WrappedData wrappedData = new Core.Cryptography.WrappedData();
stream.Position = 0;
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(stream);
wrappedData.ReadXml(reader);
string clearKey = Core.Cryptography.CryptHelper.AsymmetricDecrypt(wrappedData);
_sessionKey = Core.Cryptography.CryptHelper.GetSessionKey(clearKey);
sw.Stop();
toolStripStatusLabel1.Text = string.Format( "Create Key took {0} ms", sw.ElapsedMilliseconds);
buttonDecrypt.Enabled = true;
buttonEncrypt.Enabled = true;
}
private void buttonEncrypt_Click(object sender, EventArgs e)
{
try
{
string clearText = textBoxOriginal.Text;
byte[] clearData = Encoding.Unicode.GetBytes(clearText);
Stopwatch sw = new Stopwatch();
sw.Start();
byte[] encryptedData = _sessionKey.CreateEncryptor().TransformFinalBlock(clearData, 0, clearData.Length);
string ecryptedText = Convert.ToBase64String(encryptedData);
textBoxEcrypted.Text = ecryptedText;
sw.Stop();
toolStripStatusLabel1.Text = string.Format("Encrypted Data in {0} ms", sw.ElapsedMilliseconds);
}
catch (System.Security.Cryptography.CryptographicException ex)
{
toolStripStatusLabel1.Text = ex.Message;
}
}
private void buttonDecrypt_Click(object sender, EventArgs e)
{
try
{
string encryptedString = textBoxEcrypted.Text;
byte[] encryptedData = Convert.FromBase64String(encryptedString);
Stopwatch sw = new Stopwatch();
sw.Start();
byte[] clearData = _sessionKey.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
string clearText = Encoding.Unicode.GetString(clearData);
textBoxOriginal.Text = clearText;
sw.Stop();
toolStripStatusLabel1.Text = string.Format("Decrypted Data in {0} ms", sw.ElapsedMilliseconds);
}
catch (System.Security.Cryptography.CryptographicException ex)
{
toolStripStatusLabel1.Text = ex.Message;
}
}
private void Form1_Load(object sender, EventArgs e)
{
// Get the list of available certificates
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
foreach (X509Certificate2 certificate in store.Certificates)
{
if (certificate.HasPrivateKey == true)
comboBoxCertificates.Items.Add(certificate);
}
toolStripStatusLabel1.Text = "Select a certificate to start";
}
private void comboBoxCertificates_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBoxCertificates.SelectedItem != null)
{
buttonCreate.Enabled = true;
buttonGetKey.Enabled = true;
toolStripStatusLabel1.Text = string.Empty;
}
else
{
buttonCreate.Enabled = false;
buttonGetKey.Enabled = false;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of use 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.