Implementing Encrypted SQL Server Database Columns with .NET (cont'd)
Encrypting and Storing a Record
To store the data in the database, you first generate the private IV for the record. This will be a unique IV for that record and will keep the encrypted values in the table unique. Then you will store the IV, the columns uniquely encrypted with that IV and the LastName using the shared IV for searching.
advertisement


Author's Note: The data access code used in this article uses the Microsoft Data Access Application Block. If you're not familiar with it, I strongly recommend going to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daab-rm.asp and downloading it. It comes complete with source code and provides a simpler and cleaner API for most data access tasks you'll want to do with .NET.

   Private void SaveButton_Click(
   object sender, System.EventArgs e)
   {
      object clientID = System.DBNull.Value;
   
      if(this.ClientIDField.Text!="") { 
         clientID=Convert.ToInt32(
         this.ClientIDField.Text); }
   
      byte[] PrivateVector = 
         SimpleAES.GenerateEncryptionVector();
      SimpleAES privateAES = new 
         SimpleAES(this.Key, PrivateVector);
   
      SqlHelper.ExecuteNonQuery(
         DatabaseConnectionString,
         "SetClient",
         new SqlParameter("@ClientID", 
            clientID),
         new SqlParameter(
            "@FirstNameCrypted", 
            privateAES.Encrypt(
            this.FirstNameField.Text)),
         new SqlParameter("@LastNameCrypted",
      privateAES.Encrypt(
      this.LastNameField.Text)),
         new SqlParameter("@LastNameShared", 
            lib.Encrypt(
            this.LastNameField.Text)),
         new SqlParameter("@Vector", 
            PrivateVector),
         new SqlParameter("@ShelterName", 
            privateAES.Encrypt(
            this.ShelterNameField.Text)));
   }  
Previous Page: Applying AES Encryption to Databases Next Page: Finding and Decrypting a Record


Page 1: IntroductionPage 4: Encrypting and Storing a Record
Page 2: Introducing AES EncryptionPage 5: Finding and Decrypting a Record
Page 3: Applying AES Encryption to Databases