XOR Encryption is an encryption method used to encrypt data and is hard to crack by brute-force method, i.e generating random encryption keys to match with the correct one.
Below is a simple implementation in C++. The concept of implementation is to first define XOR – encryption key and then to perform XOR operation of the characters in the String with this key which you want to encrypt. To decrypt the encrypted characters we have to perform XOR operation again with the defined key. Here we are encrypting the entire String.
C++
#include<bits/stdc++.h>
void encryptDecrypt( char inpString[])
{
char xorKey = 'P' ;
int len = strlen (inpString);
for ( int i = 0; i < len; i++)
{
inpString[i] = inpString[i] ^ xorKey;
printf ( "%c" ,inpString[i]);
}
}
int main()
{
char sampleString[] = "GeeksforGeeks" ;
printf ( "Encrypted String: " );
encryptDecrypt(sampleString);
printf ( "\n" );
printf ( "Decrypted String: " );
encryptDecrypt(sampleString);
return 0;
}
|
Java
class XOREncryption
{
static String encryptDecrypt(String inputString)
{
char xorKey = 'P' ;
String outputString = "" ;
int len = inputString.length();
for ( int i = 0 ; i < len; i++)
{
outputString = outputString +
Character.toString(( char ) (inputString.charAt(i) ^ xorKey));
}
System.out.println(outputString);
return outputString;
}
public static void main(String[] args)
{
String sampleString = "GeeksforGeeks" ;
System.out.println( "Encrypted String" );
String encryptedString = encryptDecrypt(sampleString);
System.out.println( "Decrypted String" );
encryptDecrypt(encryptedString);
}
}
|
Python3
def encryptDecrypt(inpString):
xorKey = 'P' ;
length = len (inpString);
for i in range (length):
inpString = (inpString[:i] +
chr ( ord (inpString[i]) ^ ord (xorKey)) +
inpString[i + 1 :]);
print (inpString[i], end = "");
return inpString;
if __name__ = = '__main__' :
sampleString = "GeeksforGeeks" ;
print ( "Encrypted String: " , end = "");
sampleString = encryptDecrypt(sampleString);
print ( "\n" );
print ( "Decrypted String: " , end = "");
encryptDecrypt(sampleString);
|
C#
using System;
public class XOREncryption
{
static String encryptDecrypt(String inputString)
{
char xorKey = 'P' ;
String outputString = "" ;
int len = inputString.Length;
for ( int i = 0; i < len; i++)
{
outputString = outputString +
char .ToString(( char ) (inputString[i] ^ xorKey));
}
Console.WriteLine(outputString);
return outputString;
}
public static void Main(String[] args)
{
String sampleString = "GeeksforGeeks" ;
Console.WriteLine( "Encrypted String" );
String encryptedString = encryptDecrypt(sampleString);
Console.WriteLine( "Decrypted String" );
encryptDecrypt(encryptedString);
}
}
|
Javascript
function encryptDecrypt(inpString)
{
inpString = inpString.split( "" );
let xorKey = 'P' ;
let len = inpString.length;
for (let i = 0; i < len; i++)
{
inpString[i] = (String.fromCharCode((inpString[i].charCodeAt(0)) ^ xorKey.charCodeAt(0)));
process.stdout.write(inpString[i]);
}
return inpString.join( "" );
}
let sampleString = "GeeksforGeeks" ;
process.stdout.write( "Encrypted String: " );
sampleString = encryptDecrypt(sampleString);
process.stdout.write( "\n" );
process.stdout.write( "Decrypted String: " );
encryptDecrypt(sampleString);
|
Output
Encrypted String: 55;#6?"55;#
Decrypted String: GeeksforGeeks
Time Complexity : O(N) , here N is length of given string.
Space Complexity : O(1),since no extra space used.
The basic idea behind XOR – encryption is, if you don’t know the XOR-encryption key before decrypting the encrypted data, it is impossible to decrypt the data. For example, if you XOR two unknown variables you cannot tell what the output of those variables is. Consider the operation A XOR B, and this returns true. Now if the value of one of the variable is known we can tell the value of another variable. If A is True then B should be False or if A is False then B should be true according to the properties of the boolean XOR operation. Without knowing one of the value we can not decrypt the data and this idea is used in XOR – encryption.
Related Articles:
Vigenère Cipher
Caesar Cipher
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!