XOR Encryption

In cryptography, XOR Encryption, also known as XOR Cipher, is a encryption algorithm. With this algorithm, a string of text can be encrypted by applying the bitwise XOR operator to every character using a given key. To decrypt the output, merely reapplying the XOR function with the key will remove the cipher.



									Public Shared Function XORCipher(data As String, key As String) As String
	Dim dataLen As Integer = data.Length
	Dim keyLen As Integer = key.Length
	Dim output As Char() = New Char(dataLen - 1) {}

	For i As Integer = 0 To dataLen - 1
		output(i) = ChrW(Asc(data(i)) Xor Asc(key(i Mod keyLen)))
	Next

	Return New String(output)
End Function
								


Example

									Dim text As String = "The quick brown fox jumps over the lazy dog."
Dim key As String = "secret"
Dim cipherText As String = XORCipher(text, key)
Dim plainText As String = XORCipher(cipherText, key)
								


Output

									cipherText: "'\r\u0006R\u0014\u0001\u001a\u0006\bR\a\u0006\u001c\u0012\rR\u0003\u001b\vE\t\a\b\u0004\0E\f\u0004\0\u0006S\u0011\v\u0017E\u0018\u0012\u001f\u001aR\u0001\u001b\u0014K"
plainText: "The quick brown fox jumps over the lazy dog."