Shalvin Interests

Tuesday, January 11, 2011

Symmetric Encryption in .Net

using System.Security.Cryptography;
using System.IO;
using System.Diagnostics;


       Rfc2898DeriveBytes passwordKey;
        RijndaelManaged alg;
        FileStream inFile;
        ICryptoTransform encryptor;
        FileStream outFile;
        CryptoStream encryptStream;

        string inFileName;
        string outFileName;
        string password;
        byte[] saltValueBytes;
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            saltValueBytes = Encoding.ASCII.GetBytes("ShalvinPD");
        }
        private void btnEncrypt_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                EncPrepare();

                // Read the unencrypted file into fileData
                inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
                byte[] fileData = new byte[inFile.Length];
                inFile.Read(fileData, 0, (int)inFile.Length);


                encryptor = alg.CreateEncryptor();
                outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
                encryptStream = new CryptoStream(outFile, encryptor, CryptoStreamMode.Write);

                // Write the contents to the CryptoStream
                encryptStream.Write(fileData, 0, fileData.Length);

                // Close the file handles
                encryptStream.Close();
                inFile.Close();
                outFile.Close();
                MessageBox.Show("File encrypted successfully");
                Process.Start("notepad", txtDestination.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

      

        private void btnDecrypt_Click(object sender, RoutedEventArgs e)
        {
            EncPrepare();
 
            // Read the encrypted file into fileData
            ICryptoTransform decryptor = alg.CreateDecryptor();
            inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
            CryptoStream decryptStream = new CryptoStream(inFile, decryptor, CryptoStreamMode.Read);
            byte[] fileData = new byte[inFile.Length];
            decryptStream.Read(fileData, 0, (int)inFile.Length);

            // Write the contents of the unencrypted file
            outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
            outFile.Write(fileData, 0, fileData.Length);

            // Close the file handles
            decryptStream.Close();
            inFile.Close();
            outFile.Close();
            MessageBox.Show("File encrypted successfully");
        }

        private void EncPrepare()
        {
            inFileName = txtSource.Text;
            outFileName = txtDestination.Text;
            password = txtKey.Text;
            passwordKey = new Rfc2898DeriveBytes(password, saltValueBytes);


            alg = new RijndaelManaged();
            alg.Key = passwordKey.GetBytes(alg.KeySize / 8);
            alg.IV = passwordKey.GetBytes(alg.BlockSize / 8);

          
        }
    }

WPF Xaml


<Window x:Class="WpfEncryptionShalvin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Shalvin Encryption" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Label Content="Source File Name" Height="28" HorizontalAlignment="Left" Margin="62,42,0,0" Name="label1" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="221,42,0,0" Name="txtSource" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="221,135,0,0" Name="txtDestination" VerticalAlignment="Top" Width="120" />
        <Label Content="Destination File Name" Height="28" HorizontalAlignment="Left" Margin="62,130,0,0" Name="label2" VerticalAlignment="Top" />
        <Label Content="Key" Height="28" HorizontalAlignment="Left" Margin="62,88,0,0" Name="label3" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="221,88,0,0" Name="txtKey" VerticalAlignment="Top" Width="120" />
        <Button Content="Encrypt" Height="23" HorizontalAlignment="Left" Margin="62,202,0,0" Name="btnEncrypt" VerticalAlignment="Top" Width="75" Click="btnEncrypt_Click" />
        <Button Content="Decrypt" Height="23" HorizontalAlignment="Left" Margin="221,202,0,0" Name="btnDecrypt" VerticalAlignment="Top" Width="75" Click="btnDecrypt_Click" />
    </Grid>
</Window>


Hashing

using System.Security.Cryptography;

private void btnHash_Click(object sender, EventArgs e)
{
    HashAlgorithm hashA = new SHA1CryptoServiceProvider();
byte[] pwordData = Encoding.Default.GetBytes(txtPlainText.Text);
byte[] hash = hashA.ComputeHash(pwordData);
txtHashCode.Text = BitConverter.ToString(hash);
}

No comments:

Post a Comment