Dim strEncrypted As String
'cmdEncrypt_aStrFile = Routine that shows encryption of a string
'cmdDecrypt_aStrFile = Shows decryption (recovery) of the same string
Private Sub cmdEncrypt_aStrFile()
'**************************************************************************
'* This routine shows the basic method of encryption. This example
*
'* takes plain text from an existing text box, but any text (or binary
*
'* data) contained in a string variable could be used.
*
'*
*
'* Notice that the encrypted result is passed to a string variable not
*
'* to a text box. Although the textbox, tbEncryptedText, used here, shows *
'* the encrypted result, this is for illustrative purposes only. Text
*
'* boxes can only show "printable" characters, and can't be used to
*
'* store binary data.
*
'*************************************************************************
Dim OrgString As String, strPassword As String
Dim MC As CSCypherLib
Set MC = New CSCypherLib
'Instantiate the class
OrgString = tbTexttoEncrypt.Text
'Get some text from text box
strPassword = RTrim$(tbEncryptPw.Text) 'Make sure we know the true length of the password!
strEncrypted = MC.CS_EncryptBC(OrgString, strPassword, -2) 'Encrypt and store in strEncrypted
tbEncryptedText.Text = strEncrypted 'Show result in text box
Set MC = Nothing
'Destroy cypher object
End Sub
Private Sub cmdDecrypt_aStrFile()
'**************************************************************************
'* This routine shows the method of decryption, it takes encrypted data
*
'* from the string variable (strEncrypted) which is used to contain the
*
'* cyphertext or encrypted binary data.
*
'**************************************************************************
Dim encString As String, strPassword As String
Dim MC As CSCypherLib
Set MC = New CSCypherLib
'Instantiate the class
encString = strEncrypted
'Read encrypted string from variable (not from text box)
If Len(encString) < 56 Then Exit Sub 'On this example do a crude check to see if we actually have _
something to decode
strPassword = RTrim$(tbDecryptPw.Text) 'Make sure we know the true length of the password!
tbDecryptedText.Text = MC.CS_DecryptBC(encString, strPassword, -1) 'As the original string was taken from a _
text box, we can decode direct to another Text box
Set MC = Nothing
'Clean up
End Sub
'Encrypt_File = Routine that demonstrates encryption of a file
'Decrypt_File = Shows decryption (recovery) of the same file
Private Sub Encrypt_File()
'*************************************************************************
'* This routine shows the basic method of file encryption. It takes an
*
'* existing .jpg graphics file ( Pooch.jpg ) found in the VBExamples
*
'* folder, encrypts it, and saves the result with a .cs$ extension.
*
'* For the purposes of demonstration the file name/path and password
*
'* are hard coded.
*
'*************************************************************************
Dim strCurrentFile As String, strFileData As String, strPassword As String
Dim MC As CSCypherLib
Set MC = New CSCypherLib
'Instantiate the class
strCurrentFile = App.Path & "\Pooch.jpg" 'Select
some arbitrary File
strPassword =
"apass$tr1ng"
'Set a password
On Error GoTo FileOrPathError
'Turn on error trap for file/path access
strFileData = String(FileLen(strCurrentFile), " ") 'Read the whole file
On Error GoTo 0
'Turn off trap
lngFilebuff = Freefile
Open strCurrentFile For Binary As lngFilebuff
Get lngFilebuff, , strFileData
'Get the file data
Close lngFilebuff
strEncrypted = MC.CS_EncryptBC(strFileData, strPassword, -2) 'Encrypt the File Data
strCurrentFile = Mid$(strCurrentFile, 1, Len(strCurrentFile) - 3) & "cs$"
'Change File Extension so that _
we don't overwrite the original
in this example
lngFilebuff = Freefile
Open strCurrentFile For Binary As lngFilebuff 'Write out the encoded file
Put lngFilebuff, , strEncrypted
Close
MsgBox " ENCRYPTED File Saved with the new File Extension [ cs$ ] ", vbOKOnly + vbInformation, "Encrypted File: SUCCESS"
Set MC = Nothing
'Destroy cypher object
Exit Sub
FileOrPathError:
MsgBox " Can't find the File or Path of the specified file ", vbOKOnly + vbExclamation, "Encrypted File: FAIL"
Set MC = Nothing
End Sub
Private Sub Decrypt_File()
'**************************************************************************
'* This routine shows the basic method of file decryption. It takes the
*
'* file Pooch.cs$ created as a result of running Encrypt_File, decrypts
*
'* it and restores the original file name ( Pooch.jpg ) by replacing the
*
'* the .cs$ extension with .jpg. You might delete Pooch.jpg to check
*
'* file restoration.
*
'**************************************************************************
Dim strCurrentFile As String, strFileData As String, strPassword As String
Dim MC As CSCypherLib
Set MC = New CSCypherLib
'Instantiate the class
strCurrentFile = App.Path & "\Pooch.cs$" 'Get the encrypted file
strPassword =
"apass$tr1ng"
'Use the original password
On Error GoTo FileOrPathError
'Turn on error trap for file/path access
strEncrypted = String(FileLen(strCurrentFile), " ") 'Read the whole file
On Error GoTo 0
'Turn off trap
lngFilebuff = Freefile
Open strCurrentFile For Binary As lngFilebuff
Get lngFilebuff, , strEncrypted
'Get the encrypted data
Close lngFilebuff
strFileData = MC.CS_DecryptBC(strEncrypted, strPassword, -1) 'Decrypt the File Data
strCurrentFile = Mid$(strCurrentFile, 1, Len(strCurrentFile) - 3) & "jpg"
'Restore the File Extension, This _
will overwrite the original file
lngFilebuff = Freefile
Open strCurrentFile For Binary As lngFilebuff 'Write out the decoded file
Put lngFilebuff, , strFileData
Close
'Close all open file buffers
MsgBox " The Original File has been Overwritten/Restored ", vbOKOnly + vbInformation, "Decrypted File: SUCCESS"
Set MC = Nothing
Exit Sub
FileOrPathError:
MsgBox " Can't find the Encrypted [ .cs$ ] File Specified ", vbOKOnly + vbExclamation, "Decrypted File: FAIL"
Set MC = Nothing
'Make sure we clean up in case of error
End Sub
|