Click here to Skip to main content
15,881,413 members
Articles / Desktop Programming / Windows Forms

Securing Data in .NET

Rate me:
Please Sign up or sign in to vote.
4.81/5 (29 votes)
28 Oct 2007CPOL10 min read 93.1K   5.9K   108  
Need to learn further on cryptology? This article takes an overview of common cryptosystems, along with a step by step description of the Data Encryption Standard and the Advanced Encryption Standard.
//Copyright (c), October 2007, Some Rights Reserved 
//By Murat Firat

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace SecuringData
{
    public partial class DigitalSigDemo : Form
    {
        enum DigSigAlgorithms { RSA, DSA };

        private RSACryptoServiceProvider rsaAlg = new RSACryptoServiceProvider();
        private DSACryptoServiceProvider dsaAlg = new DSACryptoServiceProvider();

        public DigitalSigDemo()
        {
            InitializeComponent();
            InitializeComboBox();
        }

        private void InitializeComboBox()
        {
            comboBoxAlgorithm.DataSource = Enum.GetNames(typeof(DigSigAlgorithms));
            comboBoxAlgorithm.SelectedIndex = 0;
        }

        //sign message
        private void buttonSign_Click(object sender, EventArgs e)
        {
            byte[] MessageToBeSigned = Encoding.ASCII.GetBytes(textBoxSign.Text);
            byte[] signature = null;
            string publicParameters = null;
            string priavteParameters = null;

            if (comboBoxAlgorithm.SelectedIndex == 0)
            {
                signature = rsaAlg.SignData(MessageToBeSigned, "SHA1");
                publicParameters = rsaAlg.ToXmlString(false);
                priavteParameters = rsaAlg.ToXmlString(true);
            }
            else
            {
                signature = dsaAlg.SignData(MessageToBeSigned);
                publicParameters = dsaAlg.ToXmlString(false);
                priavteParameters = dsaAlg.ToXmlString(true);
            }

            textBoxVerify.Text = textBoxSign.Text;
            textBoxSignature.Text = Convert.ToBase64String(signature);
            textBoxPublicKey.Text = publicParameters;
            textBoxPrivateKey.Text = priavteParameters;
        }

        //verify message
        private void buttonVerify_Click(object sender, EventArgs e)
        {
            byte[] MessageToBeVerified = Encoding.ASCII.GetBytes(textBoxVerify.Text);
            byte[] signature = Convert.FromBase64String(textBoxSignature.Text);
            string publicParameters = textBoxPublicKey.Text;
            string priavteParameters = textBoxPrivateKey.Text;

            bool isVerified ;

            if (comboBoxAlgorithm.SelectedIndex == 0)
            {
                isVerified = rsaAlg.VerifyData(MessageToBeVerified, "SHA1", signature);
            }
            else
            {
                isVerified = dsaAlg.VerifyData(MessageToBeVerified, signature);
            }

            labelIsVerified.Text = isVerified ? "Message is Verified" : "Message is NOT Verified";
        }

    }
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Turkey Turkey
Has BS degree on computer science, working as software engineer in istanbul.

Comments and Discussions