Browse DevX
Sign up for e-mail newsletters from DevX


Keeping Secrets: A Guide to VB .NET Cryptography : Page 3

Learn how to use the .NET encryption and key-generating classes in your own VB programming to maintain secret files on your hard drive or encrypt files before sending them over the Internet.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The following code generates the password in the example program:

Public Class Form1 Inherits System.Windows.Forms.Form   'create an 8-byte long array to hold the key Private TheKey(7) As Byte   'Stuff some random values into the vector: Private Vector() As Byte = {&H12, &H44, &H16, &HEE, &H88, &H15, &HDD, &H41}

First, it defines two variables to hold the key and the initialization vector (which I'll explain shortly). You need only provide random values to the vector, though you could hash a password or some other variable to get values for the vector. In any case, the following procedure creates the key out of a password the user types in:

Sub CreateKey(ByVal strKey As String)   ' Byte array to hold key Dim arrByte(7) As Byte   Dim AscEncod As New ASCIIEncoding() Dim i As Integer = 0 AscEncod.GetBytes(strKey, i, strKey.Length, arrByte, i)     'Get the hash value of the password Dim hashSha As New SHA1CryptoServiceProvider()   Dim arrHash() As Byte = hashSha.ComputeHash(arrByte)     'put the hash value into the key For i = 0 To 7 TheKey(i) = arrHash(i) Next i   End Sub

The user's password (strKey) is passed to this procedure, and then the password is separated into individual ASCII values held in a byte array. This byte array is fed to the ComputeHash method of the SHA1CryptoServiceProvider class, which returns a hash value. You put the hash into your TheKey array for use later in the encryption (or decryption) procedures.

Note that SHA1CryptoServiceProvider can actually provide 160 bits, but you're using only 64 in the example program. If you wish, you could employ 64 of the unused bits for the initialization vector. The user should then enter a 16-byte key though.

Just what does that initialization vector do? It is a byte array with eight elements, just like the key. The vector, however, doesn't work like the key. The vector is designed to eliminate a problem typical of block ciphers like DES, where the text is broken into eight-byte groups, which are each then manipulated as units. DES uses the patterns of data from each previous block to further distort the next block. Change a single character in the first block of the plaintext and you change the characters that follow it in all the blocks to come. Thus you've eliminated the problem of creating duplicate blocks down the chain of connected blocks.

For example, even if in a fit of passion you sent a message filled with nothing but "Melanie! Melanie! Melanie! Melanie," the combination of the key and the impact of previous blocks on subsequent blocks would prevent duplication in the ciphertext. However, if you think about it, the very first part of the message is vulnerable to repetition if the same key is used over and over for multiple messages in which the greeting is also repeated (more on that shortly). For that reason, initialization vectors are used to simulate previous-block chaining.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date