none
Creating Encrypted Connection to DB - reusable RRS feed

  • Question

  • Hello ALL

     

    Once again I have dried my mind trying to figure this one out but I have been tasked with the following

     

    1.  Create encrypted connection to DB - reusable

    2. Create securitywrapper for DB

    3.  Create login module - password is nested via MD5

     

    below is the code I have generated so far....PLEASE HELP ME!

     

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using Encryption;

    using System.Text;

    using Microsoft.Win32;

     

    namespace EncryptionTestApp

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void label1_Click(object sender, EventArgs e)

    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)

    {

    }

    private void btnEncrypt_Click(object sender, EventArgs e)

    {

    try

    {

    // Create the encryptor object, specifying 3DES as the

    // encryption algorithm

    Encryptor enc = new Encryptor(EncryptionAlgorithm.TripleDes);

    // Get the connection string as a byte array

    byte[] plainText =

    Encoding.ASCII.GetBytes(txtConnectionString.Text);

    byte[] key = Encoding.ASCII.GetBytes(txtKey.Text);

    // Perform the encryption

    byte[] cipherText = enc.Encrypt(plainText, key);

    // Store the intialization vector, as this will be required

    // for decryption

    txtInitializationVector.Text = Encoding.ASCII.GetString(enc.IV);

    // Display the encrypted string

    txtEncryptedString.Text = Convert.ToBase64String(cipherText);

    }

    catch(Exception ex)

    {

    MessageBox.Show("Exception encrypting: " + ex.Message,

    "Encryption ");

    }

    try {

    }

    {

    // Set up the Decryptor object

    Decryptor dec = new Decryptor(EncryptionAlgorithm.TripleDes);

    // Set the Initialization Vector

    dec.IV = Encoding.ASCII.GetBytes(txtInitializationVector.Text);

    byte[] key = Encoding.ASCII.GetBytes(txtKey.Text);

    // Perform the decryption

    byte[] plainText = dec.Decrypt(Convert.FromBase64String(

    txtEncryptedString.Text),

    key);

    // Display the decrypted string.

    txtDecryptedString.Text = Encoding.ASCII.GetString(plainText);

    }try catch(Exception ex)

    {

    MessageBox.Show("Exception decrypting. " + ex.Message,

    "Encryption Test Harness");

    }

    }

    catch(Exception ex;

    {

    MessageBox.Show("Exception encrypting: " + ex.Message;

    private void btnWriteRegistryData_Click(object sender, EventArgs e)

    {

    // Create registry key and named values

    RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software",true);

    rk = rk.CreateSubKey("TestApplication");

    // Write encrypted string, initialization vector and key to the

    registry

    rk.SetValue("connectionString",txtEncryptedString.Text);

    rk.SetValue("initVector",

    Encoding.ASCII.GetBytes(txtInitializationVector.Text));

    rk.SetValue("key", Encoding.ASCII.GetBytes(

    txtKey.Text));

    MessageBox.Show("The data has been successfully written to the registry");

    }

    private void SetValue(string p,string p_2)

    {

    throw new Exception("The method or operation is not implemented.");

    }

    "Encryption ");

    }

    }

    Monday, July 23, 2007 10:49 PM