Tuesday, June 9, 2009

Using Powerful Encryption in Visual Basic

From time to time, visual basic programmers need the ability to secure information. When security is needed in software, many visual basic programmers create childishly simple algorithms that can be easily broken; however, programmers have another option to secure information. Visual basic programmers can add powerful encryption to their programs when they plug their software into the CAPICOM library that is provided in the platform SDK by Microsoft.

Programmers must install the platform software development kit from the Microsoft web site before they can use CAPICOM library. Since Microsoft frequently changes its URLs, programmers should just do a search for platform SDK on the Microsoft web site. Since the platform SDK comes with an easy to use setup, vb programmers should have no trouble installing the kit.

After the platform software development kit has been installed, programmers can simply plug the library into their software. In visual basic, programmers should go to the menu and select project->references and select the COM tab. In the list of components, programmers should select “CAPICOM Type Library” and hit ok; now, programmers should be able to call CAPICOM functions from their software project.

After the CAPICOM library has been incorporated into the software project, programmers can encrypt and decrypt information with ease. For example, programmers can experiment with the following code.

Test Form Setup:
Programmers should add two text boxes and two command buttons to a standard form. The first text box should be large because it is going to hold the message.

Text Box 1. Name=txtMessage, text=nothing, Multiline=true
Text Box 2. Name=txtPassword, text=nothing, passwordchar=*
Command Button 1. Name=cmdDecrypt, text = Decrypt
Command Button 2. Name = cmdEncrypt, text = Encrypt

After programmers have added the above components to their form, they can paste the following code into the form's module.

Private Sub cmdDecrypt_Click()
'Make sure we have entered all the information:
If txtMessage.Text <> "" And txtPassword <> "" Then

'This is our encryption object
Dim DecryptData As New EncryptedData

'We send the object the password to use to unlock the encryption:
DecryptData.SetSecret (txtPassword.Text)

'Send the ciphertext to the object and tell it to decrypt it.
DecryptData.Decrypt (txtMessage.Text)

'Get our plaintext from the object
txtMessage.Text = DecryptData.Content
End If
End Sub

Private Sub cmdEncrypt_Click()
'Make sure we have entered all the information:
If txtMessage.Text <> "" And txtPassword <> "" Then

'This is our encryption object
Dim encryptdata As New EncryptedData

'Before we can start encrypting, we got to define the
'algorithm, keysize, and the password (Used to generate key)
'Algorithm to use: AES (Advanced Encryption Standard)

'You could also use:
'encryptdata.algorithm = CAPICOM_ENCRYPTION_ALGORITHM_RC2

'Next we set our key size, which is a no brainer to put to max.

'Next we set our secret password to unlock the message.
encryptdata.SetSecret (txtPassword.Text)

'We send the object the plaintext scramble:
encryptdata.Content = txtMessage.Text

'Return message as base64
txtMessage.Text = encryptdata.Encrypt(CAPICOM_ENCODE_BASE64)
End If
End Sub

No comments:

Post a Comment