Encryption-Decryption using -XOR operator

This C program demonstrates basic example on encryption and decryption of data using "exclusive OR operator" -XOR . You can use it to secure your data and other security stuff. The C example has been explained with comments and input/output screenshots.

You can compile this program on bOtskOOl Free Online C/C++ Compiler

 

//Encryption-Decryption

#include <iostream.h>

using namespace std;

//declare funtion encrypt_decrypt that takes used data and the key

unsigned int encrypt_decrypt(unsigned int value,unsigned int key);

//main funtion

int main() 

{ unsigned int user_data,user_key,user_en;    

  cout<<"Enter data for encryption or decryption : "<<endl;

  cin>>user_data;

  cout<<user_data;

  cout<<"\nEnter a key value for encryption or decryption: "<<endl;

  cin>>user_key;

  cout<<user_key;

  //funtion call: Pass user data and key into function for encryption or decryption  

  user_en=encrypt_decrypt(user_data,user_key);

  cout<<"\n Your data has been encrypted or decrypted and its value is : "<<endl;

  cout  <<user_en<< endl; 

}

//funtion details

  unsigned int encrypt_decrypt(unsigned int value,unsigned int key) {

//perform bitwise XOR data and key, then return the value when function is called 

  return (value^key);

}

Output: 

Data-encryption

encryption

Data-decryption using same key

decryption

 

The above OUTPUT was generated by bOtskOOl Online Compiler try>>

Download Source Code

Terms of Agreement:  

By using this code, you agree to the following terms-  
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.  
2) You MAY NOT redistribute this code (for example to a web site) without written permission from us. Failure to do so is a violation of copyright laws.  
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which may have placed in the code or code's description.

 >>Kindly post your doubts and suggestions on our discussion forum.

Tags: