VOOZH about

URL: https://www.geeksforgeeks.org/dsa/xor-cipher/

⇱ XOR Cipher - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

XOR Cipher

Last Updated : 23 Jul, 2025

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. 


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

Comment
Article Tags: