The employee management system stores bank account information for the purpose of dumping the salaries of employees directly into their bank accounts. at present this information is being stored as plain text. In this solution, you’ll insert private key encryption and decryption functions to your security library, and you’ll use these functions to encrypt the BankAccount field.

1. Open the identical EncryptDatabaseField program we used when encrypting the password field. The project is situated at CH01_Encryption\ EncryptDatabaseField\Start\EncryptDatabaseField.sl n. We will be changing the program to encrypt the BankAccount field.

2. affix the following code to the end of SecurityLibrary.db:

3. Namespace PrivateKey
Module PrivateKey
Function Encrypt(ByVal strPlainText As String, _
ByVal strKey24 As String) As String
Dim crp As New TripleDESCryptoServiceProvider()
Dim uEncode As New UnicodeEncoding()
Dim aEncode As New ASCIIEncoding()
’Store plaintext as a byte array
Dim bytPlainText() As Byte = uEncode.GetBytes(strPlainText)
’Create a memory stream for holding encrypted text
Dim stmCipherText As New MemoryStream()
’Private key
Dim slt(0) As Byte
Dim pdb As New PasswordDeriveBytes(strKey24, slt)
Dim bytDerivedKey() As Byte = pdb.GetBytes(24)
crp.Key = bytDerivedKey
’Initialization vector is the encryption seed
crp.IV = pdb.GetBytes(8)
’Create a crypto-writer to encrypt a bytearray
’into a stream
Dim csEncrypted As New CryptoStream(stmCipherText, _
crp.CreateEncryptor(), CryptoStreamMode.Write)
csEncrypted.Write(bytPlainText, 0, bytPlainText.Length)
csEncrypted.FlushFinalBlock()
’Return result as a Base64 encoded string
Return Convert.ToBase64String(stmCipherText.ToArray())
End Function
Function Decrypt(ByVal strCipherText As String, _
ByVal strKey24 As String) As String
Dim crp As New TripleDESCryptoServiceProvider()
Dim uEncode As New UnicodeEncoding()
Dim aEncode As New ASCIIEncoding()
’Store cipher text as a byte array
Dim bytCipherText() As Byte = _
Convert.FromBase64String(strCipherText)
Dim stmPlainText As New MemoryStream()
Dim stmCipherText As New MemoryStream(bytCipherText)
’Private key
Dim slt(0) As Byte
Dim pdb As New PasswordDeriveBytes(strKey24, slt)
Dim bytDerivedKey() As Byte = pdb.GetBytes(24)
crp.Key = bytDerivedKey


’Initialization vector
crp.IV = pdb.GetBytes(8)
’Create a crypto stream decoder to decode
’a cipher text stream into a plain text stream
Dim csDecrypted As New CryptoStream(stmCipherText, _
crp.CreateDecryptor(), CryptoStreamMode.Read)
Dim sw As New StreamWriter(stmPlainText)
Dim sr As New StreamReader(csDecrypted)
sw.Write(sr.ReadToEnd)
’Clean up afterwards
sw.Flush()
csDecrypted.Clear()
crp.Clear()
Return uEncode.GetString(stmPlainText.ToArray())
End Function
End Module
End Namespace

we can use these two functions in our code to encrypt and decrypt messages. The key is cited as strKey24 because it should be 24 characters long.

4. Open the MainModule.vb file, and in Sub Main(), change the line
EncryptField("Password", "PasswordHash")
to read
EncryptField("BankAccount", "BankAccountEncrypted")

5. In Sub EncryptField(), find the line that reads
strCipherText = HashCreateHash(strPlainText)
and modify it to the following:
strCipherText = PrivateKey.Encrypt(strPlainText, _ "111222333444555666777888")

6. Now press F5 to run the program. The BankAccountEncrypted field will currently contain the bank account information encrypted with the key 111222333444555666777888, and we must see output similar to what is shown here: