Ado Examples and Best Practices

Team-Fly

We already talked about how to make your applications (especially your Web pages) more efficient by not using the concatenation operator (&). The following two tips discuss a number of other string-related operations that can save you precious CPU cycles—especially when your application is sharing time with Age of Empires or other more important code.

Using Visual Basic's String Manipulation Operators

There are two types of string operators built into Visual Basic's runtime library—one type handles Strings and the other Variants. For example, the Left$, Mid$, and Right$ operators all handle (just) strings, while the Left, Mid, and Right functions handle Variants, which can be strings. If you know that the target operand is a string, use the string-specific functions.

Encrypting Strings

If you are uncomfortable saving sensitive text to a database or to an easily accessed document or text file, you might consider encrypting the data. Although the FBI can crack this pretty quickly, your cousin from Cleveland can't. The trick here is the XOR operator in Visual Basic, which functions at the bit level to manipulate the data values passed to it.

The following example illustrates use of the XOR operator to convert individual bytes of a string to make them unrecognizable without the use of a separate text key. The magic here is the formula that selects a byte from the key to XOR against individual characters. This makes it even tougher for your cousin to crack the code, because he can't simply use hex FFFF as one would initially guess.

I don't profess to know how this little formula ((i Mod l) – l * ((i Mod l) = 0)) works, but it does. I checked out the MSDN article on data encryption for the formula, but it also failed to explain how it works. I suspect it permits you to use an encryption key of varying lengths to map to one of the characters in the password. That is, if you have a password of 10 characters, and an encryption key of 3 characters, the formula chooses one of the password characters to XOR against a selected byte of the encryption key. Make sense? Just don't forget the encryption key or you'll need the FBI to figure out your password.

The example takes a value from a TextBox control, encrypts it, and writes to a second TextBox. This is the value you should save to the database, Registry, or human-readable file. To retrieve the original value, simply pass in the encrypted text and the same password key. The same Encrypt routine repeats the process, but decrypts this time, which results in the original value. If you are working with NT, placing NT security on the right file or Registry key is the definitive way to prevent access—and the encrypting can be used to prevent files from being viewed.

Dim Char As Integer Dim strPasswordIn As String Dim strPasswordOut As String Private Sub cmdDecrypt_Click() strPasswordIn = txtDataOut.Text txtDecrypted.Text = Encrypt(strPasswordOut, txtPasswordKey) 'Encrypt the string. Debug.Print "in:{" & strPasswordOut, "} out:{" & txtDecrypted.Text & "}" End Sub Private Sub cmdEncrypt_Click() strPasswordIn = txtDataIn.Text strPasswordOut = Encrypt(strPasswordIn, txtPasswordKey) 'Encrypt the string. Debug.Print "in:{" & strPasswordIn & "} out:{" & strPasswordOut & "}" txtDataOut.Text = strPasswordOut End Sub Function Encrypt(secret As String, PassWord As String) As String Dim strDataOut As String ' secret$ = the string you wish to encrypt or decrypt. ' PassWord$ = the password with which to encrypt the string. l = Len(PassWord$) strDataOut = String(Len(secret), " ") For i = 1 To Len(secret$) Char = Asc(Mid$(PassWord$, (i Mod l) - l * ((i Mod l) = 0), 1)) Mid$(strDataOut, i, 1) = Chr$(Asc(Mid$(secret$, i, 1)) Xor Char) Next Encrypt = strDataOut End Function


Team-Fly

Категории