You can use a simple random XOR shift to do the 'encrypting'... as you probably know XOR of XOR gives original, so you can use the same for encoding/decoding provided the shift key is right. If you supply the file (binary) to this function, then it'll return encoded with the given key.
If you pass the encoded file, it'll return the original file.
[courier new]
Public Function EncodeDecode(Data As String, Key As Long) As String
On Error Resume Next
Dim I As Long, X As Single
Dim CharNum As Integer, RandInteger As Integer
Dim SingleChar As String * 1
Dim ReturnData As String
X = Rnd(-Key)
For I = 1 To Len(Data)
CharNum = Asc(Mid$(Data, I, 1))
RandInteger = Int(256 * Rnd)
CharNum = CharNum Xor RandInteger
SingleChar = Chr(CharNum)
ReturnData = ReturnData & SingleChar
Next I
EncodeDecode = ReturnData
End Function
[/courier new]
But how do you make a key out of password ? Convert it to a number that vastly varies according to the password...
[courier new]
Public Function ConvertPassKey(Key as String) As Long
Dim I As Integer, Num As Long
Key = LCase(Key)
Num = 1
For I = 1 To Len(Key)
Num = Num + (I * Asc(Mid(Key, I, 1)))
Next I
ConvertPassKey = Num
End Function
[/courier new]
Be VERY, VERY WARY of the data you pass to the function. A slight shifting of even a single character will ruin the entire decoding process, and you'll get junk data!!!
I suggest you do multiple "parity" checks before actually give it up for decoding....
Also, encoding/decoding takes some time... that's why i prefer my tried and tested method "mangle da header!"
Enjoy !
Enter your message below
Sign in or Join us (it's free).