There are many algorithms available for string encryption and decryption, but often they become complicated due to the need to ensure that the ASCII value of an encrypted character stays within the limits of a single byte (0-255). An elegant approach is to use the XOR function. The simple subroutine below will always give results falling within the 0-255 limit. It also has the advantage that a second call will reverse the encryption automatically.
Function Encrypt(sPassWord As String, sText As String) As String
Dim I As Integer
Dim iPChar As Integer
Dim iCChar As Integer
Do While Len(sPassWord) < Len(sText)
sPassWord = sPassWord & sPassWord
Loop
For I = 1 To Len(sText)
iPChar = Asc(Mid$(sPassWord, I, 1))
Char = Asc(Mid$(sText, I, 1))
d$(sText, I, 1) = Chr$(iPChar Xor iCChar)
Next I
Encrypt = sText
End Function
One thing you should be aware of is that the encrypted string may contain Chr$(0) values. While this will not be a problem with Visual Basic strings, other Windows objects treat Chr$(0) as an end-of-string character. Use of the encrypted string with a Text Box or in API calls may produce unexpected results.
|