Simple Encryption


Sometimes you need to encrypt a string just to make it hard to grab a bit of information. This routine is very simple, but it does do a good job in scrambling a string.
Public Function Encrypt(InString As String, KeyValue As Integer) As String
'Encrypts or decrypts a string.  This encryption is a simple XOR based on
'a random seed number.  Passing the encrypted string will decrypt it, passing
'the unencrypted string will decrypt it.  Returns a blank string on error.
'Instring= the string to work with
'KeyValue=the seed value

    Dim X As Integer
    Dim singlechar As String
    Dim charnum As Integer
    Dim randomint As Integer
    Dim OutString As String
    Dim i As Integer
    
    On Error GoTo EncryptError
    
    OutString = ""
    X = Rnd(-KeyValue)
    For i = 1 To Len(InString)
        singlechar = Mid$(InString, i, 1)
        charnum = Asc(singlechar)
        randomint = Int(256 * Rnd)
        charnum = charnum Xor randomint
        singlechar = Chr$(charnum)
        OutString = OutString + singlechar
    Next i
    Encrypt = OutString
    Exit Function
    
EncryptError:
    'Encryption error; return a blank string
    Encrypt = ""
    Exit Function
End Function