Basic Encryption with the XOR command

Level:
Level2

Basic is right! This is not some brain-bending algorithm, in fact, you could say that it's eXtraORdinarily easy! How's that for subliminal messaging? Yes, we'll be using the XOR operator.

If you would like to follow along, download the Visual Basic 6 Source Code for this tutorial.

For those of you not familiar with XOR in Visual Basic 6, I'll give you a little lesson. XOR is supposed to mean "exclusive OR". It is so called because it only returns True if one and only one of the expressions it is passed is True. Wait, that sounds confusing :) An illustration is in order!

True Xor True = False
True Xor False = True
False Xor True = True
False Xor False = False

See? Now, the logical operations of XOR (above) are less useful to us than the bitwise operations. Observe:

1 Xor 1 = 0
1 Xor 0 = 1
0 Xor 1 = 1
0 Xor 0 = 0

This is pretty much the same as the logical operation, but watch, in VB6 we can perform it on a whole bunch of bits (say... a BYTE!) at a time:

11111111
XOR
01010101 (Key)
Result:
10101010

Here comes the magic.. if we XOR again, using the value I've marked Key and the result, we obtain the original!

10101010 (Old Result)
XOR
01010101 (Key)
Result:
11111111 (!!!)

This works in all situations when the key is held constant: XOR once, you have an encrypted result. XOR again with the same key, and you get the original value. Sounds cool, but how can we apply it practically? When storing data in a binary file, it is a simple matter to XOR each byte that is read or written in order to encrypt/decrypt it. To encrypt data under other circumstances can be a little trickier. You need to coerce that data into byte form, and then back again. We'll do it with strings here:

Dim strResult As String
Dim strChar1 As String * 1
Dim strChar2 As String * 1
   
   strChar1 = "E"
   strChar2 = "z"
   strResult = Chr(Asc(strChar1) Xor Asc(strChar2))

This code will XOR "E" with "z" by converting them to their ASCII values! Once converted to ASCII, they can be treated like bytes and XOR'ed. We can then convert back to string format using the Chr function. This method can be adapted using a For loop to encrypt complex strings using equally complex key values. I hope this visual basic tutorial was helpfull to you. Click here to download sample source code for an example of string encryption .

This tutorail is released under the GNU Free Documentation License 1.2. The original can be found here.

 

If you enjoyed this post, subscribe for updates (it's free)

coding for a string as opposed to a single character

I tried this but it worked for just the single character "e" or "z" but how do I get it to work for "Easy" for instance, I need for it to accept the entire string as a word and process the entire word.... which perhaps means breaking it up into individual bytes...(?)

cheers

Thanks!

Sir thank you so much for the help with the encryption! really helped me alot

Some code

Here is the code that code(encript)/decode your string:

Dim yourstring, encriptwith, stringpart As String
yourstring = *YOUR TEXT HERE*
encriptwith = *KEY HERE*
For i = 1 To Len(yourstring)
stringpart = Chr(Asc(encriptwith) Xor Asc(Mid(yourstring, i, 1)))
Mid(yourstring, i, 1) = stringpart
Next i

This doesn't seem to work for me??

So I coded up an encryption like this:

Public Sub passwordcrypt(ByRef strText As String, ByVal intTextLen As Integer)
Dim strPassword As String = ""
Dim intPassLen As Integer = 0
Dim blnKey As Boolean = True
Dim intStrPass As Integer = 0

'gets the password
While blnKey = True 'loops to get input
strPassword = InputBox("Please enter a password (5 to 30 characters) contianing numbers, letters, and special characters for the encryption. This password will also be required to decrypt.", "Password Required")
intPassLen = Len(strPassword)
If intPassLen <= 30 And intPassLen >= 5 Then 'checks the bounds
blnKey = False 'exits the loop if bounds check out
Else
MessageBox.Show("!!Password to short or long, please try again!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error) 'input out of bounds
End If
End While

While intStrText < intTextLen
If intStrPass = intPassLen Then
intStrPass = 0
End If
Mid$(strText, intStrText + 1, 1) = Chr(Asc(strText.Substring(intStrText, 1)) Xor Asc(strPassword.Substring(intStrPass, 1)))
intStrPass += 1
intStrText += 1
End While

intStrText = 0 'resets for main function
End Sub

'but the resulting text is not always the same, once encrypted and decrypted. What is going on??

hmmm

i'm using this xor enryption for a basic savety program (class project, so isn't important to be a 128bit encryption system or sth related), and the point is that i'm saving/reading in/from a file, sth like this:

Open "version.dat" For Input As #1
Line Input #1, tmp
Close #1
Do Until i = 9
password = password & Chr(Asc(Mid(txtKey.text, i, 1)) Xor Asc(Mid(XORtext, i, 1)))
i = i + 1
Loop

i is declared as 1, XORtext is a 8 bytes text that means 8 chars, and the txtkey.text allows 8 chars as min and max of course, the problem started when i encrypted different words, for example the string "accessok" works fine the file has 8 squares, but when writing "hackthis" i don't remember which letter from this string xored with the variable XORtext which is "flatland" made a "tabulation space" in ASCII which i guess is the problem, because checking the "accessok" it's fine but with "hackthis" became an error at the debugger, so i guess is just the word and not other problem, that is my deduction when using 2 possibilities and one is going wrong, if anybody knows a solution please reply ;) coz i'm out of ideas :P

For those who don't

For those who don't understand:

In the 'E' with 'z' example, he is saying that he is encrypting the letter 'E' with key 'z' ie it Takes the ASCII value of E into binary notation - 01000101 and encryts it with 'z' - 01111010
therefore:
01000101
XOR
01111010
=
00111111 as the encrypted byte.

Question

How would you de-code it?

Answer

Use the same code that was code it

Hence XOR

strDecoded = Chr(Asc(strResult) Xor Asc(strChar2))

The use of vb6 Xor in the Xor Sample

I am having trouble gettting the same results every time and think that the information shonn below as part of the sample might be the answer.

1.
Dim strResult As String
2.
Dim strChar1 As String * 1
3.
Dim strChar2 As String * 1
4.

5.
strChar1 = "E"
6.
strChar2 = "z"
7.
strResult = Chr(Asc(strChar1) Xor Asc(strChar2))

I don't understand this part of the explanation and wonder if there is a more complete expiation of this part as it pertains to encryption.

Thanks for your help.i

d00d! dat was great..thanks

d00d! dat was great..thanks