Click here to Skip to main content
15,886,833 members
Articles / Programming Languages / Visual Basic

Encrypting a dataset using AES, including compression

Rate me:
Please Sign up or sign in to vote.
4.98/5 (49 votes)
21 Aug 2015CPOL7 min read 125.6K   4.8K   127  
Article describes how to encrypt a dataset using AES. Optionally the dataset is compressed before the encryption.
''' <summary>
''' This window handles all the data input for the dataset
''' </summary>
Class MainWindow
   Private Sub btnCreate_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
      Me.OpenTheDataSet(True)
   End Sub

   Private Sub btnOpen_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
      Me.OpenTheDataSet(False)
   End Sub

   Private Sub btnSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
      EncryptDataSetVB.DataHandler.SaveChanges()
   End Sub

   Private Sub btnCancel_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
      EncryptDataSetVB.DataHandler.CancelChanges()
   End Sub

   Private Sub Window_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs)
      e.Cancel = Not Me.CheckPendingChanges()
   End Sub

   ''' <summary>
   ''' Checks if there are pending changes and if they shall be ignored.
   ''' </summary>
   ''' <returns>True if changes are ignored or there are none</returns>
   Private Function CheckPendingChanges() As Boolean
      Dim result As System.Windows.MessageBoxResult = MessageBoxResult.Yes

      If Not EncryptDataSetVB.DataHandler.TheDataSet Is Nothing Then
         If EncryptDataSetVB.DataHandler.TheDataSet.HasChanges() Then
            result = System.Windows.MessageBox.Show(
               "All the unsaved changes will be lost. Do you want to continue?",
               EncryptDataSetVB.DataHandler.AppTitle,
               MessageBoxButton.YesNo,
               MessageBoxImage.Question,
               MessageBoxResult.No)
         End If
      End If

      Return result = MessageBoxResult.Yes
   End Function

   ''' <summary>
   ''' Opens or creates the dataset
   ''' </summary>
   ''' <param name="create">Create a new dataset</param>
   ''' <returns>True if successful</returns>
   Private Function OpenTheDataSet(create As Boolean) As Boolean
      Dim authentication As Authentication

      ' Check if there are pending changes
      If (Not Me.CheckPendingChanges()) Then
         Return False
      End If

      ' Get authentication information
      authentication = New Authentication()
      authentication.Owner = Me
      authentication.CreateDataset = create
      authentication.UseCompression = Me.chkCompress.IsChecked
      If Not authentication.ShowDialog() Then
         Return False
      End If

      If (create) Then
         ' Create the dataset
         If (Not EncryptDataSetVB.DataHandler.CreateDataSet( _
            authentication.FileName, _
            authentication.UserName, _
            authentication.Password, _
            Me.chkCompress.IsChecked)) Then
            Return False
         End If
      Else
         ' Open the existing dataset
         If (Not EncryptDataSetVB.DataHandler.OpenDataSet( _
            authentication.FileName, _
            authentication.UserName, _
            authentication.Password, _
            Me.chkCompress.IsChecked)) Then
            Return False
         End If
      End If

      ' Show the data
      Me.gbData.Header = String.Format("Current dataset: {0}", EncryptDataSetVB.DataHandler.FileName)
      Me.dgContacts.ItemsSource = EncryptDataSetVB.DataHandler.TheDataSet.Contacts
      Me.btnSave.IsEnabled = True
      Me.btnCancel.IsEnabled = True

      Return True
   End Function

   Private Sub Button_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)

   End Sub
End Class

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
Architect
Europe Europe
Biography provided

Comments and Discussions