P2Pprogrammer 2 programmer

Home > Tips > Encryption and Decryption > XOR Encryption

XOR Encryption

XOR Encryption, Vernam Cipher Logics, Sample Coding, Text and Database Encryptr by XOR Encryption

What is XOR Encryption?

The simplest stream cipher is called the Vernam cipher or XOR Encryption. A bit from a key is added, with the carry discarded, to a bit from the plaintext to yield one bit of cipher text (this operation is called exclusive -OR). One simple way to encrypt and decrypt data is using XOR Encryption method, The character in the data stream and those of a code key are XORed together to create and encrypted character. The process is exactly same for encryption as well as decryption.

The code key can be made up of any alphanumeric characters you want. It can be any long, much longer key used it is most tough to crack.

Vernam or XOR Ciphers turn out to be easy to break. An attack can mathematically combine ciphertext messages in pairs to factor out the key stream and produce a messages that is easier to crack. If the attacker solves one message, extracting its plaintext from ciphertext, the plaintext from ciphertext, that plaintext can be used to create the key string. Once the key string is recovered, the attacker can decipher all of the other messages protected with that key string.

Despite its weakness in simple applications, the XOR Encryption remains an important cipher, It is weak when we use repeating keys, but it can be very effective when the key stream varies continuously. this approaches is often uses in modern stream ciphers RC4 (Rivest Cipher #4), developed by Ron Rivest of RSA data security. It is used wisely in the field of data security for its easy to use, however it is very easy to crack for professional hacker.

XOR Encryption is not very secure and it can be crack very easily, however if want encrypt messages or documents so that the cannot be viewed in text editor, then it is simple way to accomplish.

Following code demonstrate how to use XOR Encryption algorithm to encrypt and decrypt a stream of text with Key Code, here we are creating a function named XORCipher which take two argument one is Input stream and one is Key code.

We XORed each bit of stream with the Key bit and concatenate the resulting bit to produce return string, it used same function to encryption and decryption, however in both case the key code should be same.

    Public Function XORCipher(ByVal strDataIn As String, _

    ByVal CodeKey As Integer) As String

        Dim i As Integer

        Dim j As Long

        Dim StrOut As String

        StrOut = ""

        For j = 1 To Len(strDataIn)

            i = Asc(Mid(strDataIn, j, 1)) Xor CodeKey

            StrOut = StrOut & Chr(i)


        XORCipher = StrOut

    End Function

For example if we call the function XORCipher("ATANU MAITY",160) then it returns cipher text "áôáîõ€íáéôù", same way if we call the function XORCipher("áôáîõ€íáéôù",160) then it returns plain text "ATANU MAITY".

For completing listing of the code and sample Visual Basic 6, VB.NET, C# .NET project for XOR Encryption can be found in Sample Code section.

Home > Tips > Encryption and Decryption > XOR Encryption