banr_bklrx.gificon_bw_about.gif (884 bytes)banr_homex_ico.gif (1035 bytes)banr_howbuyx_ico.gif (1114 bytes)banr_descfrx_ico.gif (1119 bytes)banr_hascfrx_ico.gif (1223 bytes)
crypt_tools4swdev.gif (2298 bytes)
Example Project: Convert ciphertext (encrypted data)
to hexadecimal format for storage or transmission

Download Example VB Project:
(DEScipher/ActiveX | TDEScipher/ActiveX)

(also see  notes  |   code )

Description: The encryption process produces binary data regardless of what format or data type is input. This is a natural and desirable result! For each byte of encrypted data all possible values are equally likely.

However, in some applications processing binary data is unacceptable. For example the "null" character (&H00 will occur once in every 256 bytes of encrypted data on average) is used as a string terminator in some applications. Applications where binary or non-printable string characters cause problems include transmission of ciphertext via e-mail, and storage or processing of ciphertext in   text fields in some databases.

scrnshot_hexciphertext.gif (3730 bytes)

This example VB project illustrates how to convert ciphertext data into hexadecimal string format. Since the hex "alphabet" contains only 16 characters (0-9, A-F), and native VB functions are available for processing hex strings this approach provides a simple and convenient solution for representing binary data. Note: those who agree with Friedman will be gratified to realize that this is not a "free lunch"... the cost is a doubling of the string length, and the resources required to store it. A more efficient approach would be conversion to Base64 - this is left as an exercise for the reader.

Code:

Private Sub Encrypt_HEX()
Dim count As Long
Dim Status As Integer
Dim strANSIInput As String
Dim bytCfrArray() As Byte
Dim hexVal As String
Dciph321.DESReset
'
' Convert the input string from Unicode to ANSI

strANSIInput = StrConv(txtInput.Text, vbFromUnicode)
count = LenB(strANSIInput)
'
' Use the DEScipher/ActiveX control to encrypt the ANSI input string

Status = Dciph321.EncryptString(strANSIInput, count)
'
' Map the encrypted string into a byte array

bytCfrArray = strANSIInput
'
' Use the "Hex" function to convert each element
' of the byte array into its hex equivalent;
' add a leading "0" if the hex value is a single character

For i = LBound(bytCfrArray) To UBound(bytCfrArray)
hexVal = hexVal & Right("0" + Hex(bytCfrArray(i)), 2)
Next
'
' Display the ciphertext in hex format on the form

txtHexCfr.Text = hexVal
'
' References:
' See http://support.microsoft.com/support/kb/articles/Q187/6/75.ASP
' and "DBCS String Manipulation Functions" in the VB Help File

End Sub

Private Sub Decrypt_HEX()
Dim count As Long
Dim Status As Integer
Dim strCfr As String
Dciph321.DESReset
'
' Use the "Chr" function to convert each pair of hex values
' in the ciphertext into its string character equivalent

For i = 1 To Len(txtHexCfr.Text) Step 2
strCfr = strCfr & Chr("&H" & Mid(txtHexCfr.Text, i, 2))
Next
strCfr = StrConv(strCfr, vbFromUnicode)
'
' Use the DEScipher/ActiveX control to decrypt the Unicode input string

Status = Dciph321.DecryptString(strCfr, LenB(strCfr))
'
' Display the result on the form - should be identical to the input string!

txtClear.Text = StrConv(strCfr, vbUnicode)
End Sub

Private Sub cmdAbout_Click()
MsgBox "Some applications require that the ciphertext (encrypted data)" & _
"be represented as ASCII characters. This example shows one technique " & _
"for doing that... the ciphertext is represented in hexadecimal format, " & _
"(characters 0-9, A-F). This example further illustrates how to decrypt " & _
"the hexadecimal ciphertext to recover the original input string."
End Sub

 

Notes:
1) This project DOES NOT contain a copy of the DEScipher/ActiveX control. It will not run unless DEScipher/ActiveX has been installed or registered on your system.
2) This project is not "production quality" code, but intended only to illustrate techniques that may be of use to VB developers using the DEScipher or TDEScipher controls in the MS VB development environment. You are free to use this sample project for any legal purpose provided you have a valid developer's license for DEScipher/ActiveX or TDEScipher/ActiveX. Bokler Software Corp. does not warrant, nor do we support this sample project.
3) See the Tech Support FAQ for more details and sample code.


Copyright ©, 1995-2000 Bokler Software Corp. All rights reserved. DEScipher, TDEScipher and HASHcipher are trademarks of Bokler Software Corp. The "animated_cipher.gif" is copyrighted by Bokler Software Corp. Microsoft, Windows, Visual Basic, Visual C++ and ActiveX are trademarks of Microsoft Corp.