Sometimes you want to secure your data with a way that you are the only one that will be able to view these data again. This can be accomplished using X509 Certificate. The X509 Certificate is signed with a private key that uniquely and positively identifies the holder of the certificate. The X509 Certificates can be used in Public Key Infrastructure PKI and SSO.
In this article we will create an application that queries and display installed certificates on your machine and encrypt and decrypt entered data with the private key of the certificate.
Using the Code:
To create an application that encrypts your data with X509 Certificate follow the following steps:
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
private void EncryptToolStripButton_Click(object sender, EventArgs e)
{
try
{
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certCollection = (X509Certificate2Collection)store.Certificates;
X509Certificate2Collection foundCollection = (X509Certificate2Collection)certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
X509Certificate2Collection selectedcollection = X509Certificate2UI.SelectFromCollection(foundCollection,
"Select a Certificate.", "Select a Certificate from the following list to get information on that certificate", X509SelectionFlag.SingleSelection);
if (selectedcollection.Count > 0)
{
X509Certificate2 cert = selectedcollection[0];
string certificateData = "Subject: " + cert.Subject + Environment.NewLine + "IssuerName: " + cert.Issuer
+ "\nSerialNumber: " + cert.SerialNumber + "\nFriendlyName:\n"+ cert.FriendlyName;
MessageBox.Show(certificateData, "Certificate Data",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if (cert.Verify())
{
MessageBox.Show(cert.Subject + " is a valid certificate.", cert.FriendlyName,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(cert.Subject + " is not a valid certificate.", cert.FriendlyName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)cert.PrivateKey;
byte[] cipherData = rsaEncryptor.Encrypt(Encoding.UTF8.GetBytes(PlainRichTextBox.Text), true);
CipherRichTextBox.Text = Convert.ToBase64String(cipherData);
}
}
catch (CryptographicException ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DecryptToolStripButton_Click(object sender, EventArgs e)
{
try
{
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certCollection = (X509Certificate2Collection)store.Certificates;
X509Certificate2Collection foundCollection = (X509Certificate2Collection)certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
X509Certificate2Collection selectedcollection = X509Certificate2UI.SelectFromCollection(foundCollection,
"Select a Certificate.", "Select a Certificate from the following list to get information on that certificate", X509SelectionFlag.SingleSelection);
if (selectedcollection.Count > 0)
{
X509Certificate2 cert = selectedcollection[0];
string certificateData = "Subject: " + cert.Subject + Environment.NewLine + "IssuerName: " + cert.Issuer
+ "\nSerialNumber: " + cert.SerialNumber + "\nFriendlyName:\n"+ cert.FriendlyName;
MessageBox.Show(certificateData, "Certificate Data",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if (cert.Verify())
{
MessageBox.Show(cert.Subject + " is a valid certificate.", cert.FriendlyName,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(cert.Subject + " is not a valid certificate.", cert.FriendlyName,
MessageBoxButtons.OK,MessageBoxIcon.Error);
}
RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)cert.PrivateKey;
byte[] plainData = rsaEncryptor.Decrypt(Convert.FromBase64String(CipherRichTextBox.Text), true);
PlainRichTextBox.Text = Encoding.UTF8.GetString(plainData);
}
}
catch (CryptographicException ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Note: In decryption process you should follow the reverse steps of the encryption process.
Now you have an application that Encrypt and Decrypt your data with X509 Certificates.
15 March 2022
17 February 2022
09 December 2019