Add Powerful Encryption To Your Visual Basic Programs
I thought I would show you an easy way to add powerful encryption to your visual basic programs. Up till now, you either had to write the chipers in a fast language like C or deal with the CryptoAPI. Either way you went, it was very complex with uneven documentation. But lucky for the visual basic programmer, Microsoft released the CAPICOM component, which is a wrapper around the CryptoAPI. This is just a few examples of how to use it.
Once you have that downloaded, extract the CAPICOM.DLL file into your system directory (system32 on xp). Then open up your command prompt (dos on some machines) and change directories to c:\windows\system32 (c:\windows\system\ on some platforms). Then type this into the command prompt and hit enter:
regsvr32.exe CAPICOM.DLL
You should see a message box telling you the install was successful.
Using CAPICOM:
Before we can use CAPICOM from our visual basic program, we must first add a reference to it. You can do so by going to “Project” in your menu and then “References”. Tick the CAPICOM type library and hit ok. Now you can call the CAPICOM functions from within visual basic.
Add the following to your form:
Make this text box big:
Text Box – Name=txtMessage, text=nothing, Multiline=true
Text Box – Name=txtPassword, text=nothing, passwordchar=*
Command Button – Name=cmdDecrypt, text = Decrypt
Command Button – Name = cmdEncrypt, text = Encrypt
Then paste the following code into your forum.
Code:
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)
encryptdata.Algorithm = CAPICOM_ENCRYPTION_ALGORITHM_AES
'You could also use:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'encryptdata.Algorithm =CAPICOM_ENCRYPTION_ALGORITHM_3DES
'encryptdata.Algorithm = CAPICOM_ENCRYPTION_ALGORITHM_DES
'encryptdata.algorithm = CAPICOM_ENCRYPTION_ALGORITHM_RC2
'encryptdata.Algorithm = CAPICOM_ENCRYPTION_ALGORITHM_RC4
'Next we set our key size, which is a no brainer to put to max.
encryptdata.Algorithm.KeyLength = CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM
'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
I've also added the project (For those of u with vb 6)
Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde