As you might know, encryption is hiding data by masking them with some other data. There are many exncryption algorithms used for encrypting any confidential data. XOR, SHA, MD5, RSA and DSA etc. are few of them. Today i am going to explain what is XOR encryption and how it works and also implementation of XOR in C programming Language.
About XOR Encryption Method:
In Cryptography, the XOR cipher is a type of additivecipher, and an encryption algorithm. Operation of XOR Encryption Algorithm is based on following four principles.
Example: If A=1 and B=0 then A XOR B = 1
If A=1 and B=1 then A XOR B = 0
How XOR Encryption Works?
XOR Encryption works by repeating XOR operation on each character of the given string and the key string. So to decrypt the encrypted data, just pass the decrypted text as the string input and the same key used for the encryption as the key.
Example:(From Wikipedia)
In 8-bit ASCII, the string 'Wiki' can be represented as '01010111 01101001 01101011 01101001', where each 8-bit word represents the letters W, i, k and i respectively.
The XOR operation is carried out in the follwing manner using the key '11110011'.
01010111 XOR 11110011 = 10100100
01101001 XOR 11110011 = 10011010
01101011 XOR 11110011 = 10011000
01101001 XOR 11110011 = 10011010
So the encrypted output will be ascii text equivalent of '10100100 10011010 10011000 10011010'
This is the simplest working example of the XOR encryption. There are other implementations too, which stands different because of their iteration steps.
Note:- XOR encryption can be made very strong(Even unbreakable) by using true random numbers. But normally XOR encryption is considered to be very weak. This is because encrypted data can be subjected to plaintext attacks to get the key and there by leading to the possibility of decryption of confidential data. So no standards recommend XOR Encryption for data protection.
Implementation of XOR Encryption In C
As i have already explained the basic technique of XOR Encryption, i am not going to explain much about the program and it's working wherever it is not necessary to do so. Also remember that while using the below specified code to encrypt textual data, the key should have atleast the length of the string to be encrypted. Otherwisse there might be some data lose after decrypting again.
Here goes a simple C Code:-
#include <stdio.h>
#include <string.h>
int main()
{
int i;
char string[100]="";
char key[100]="!@#$%^&*()_"; //Default key.
printf("\n Enter the string to be ciphered: ");
scanf("%s",&string); //Read the string to be encrypted and store it in 'string' variable
printf("\n Enter the key: ");
scanf("%s",&key); //Read the string to be used as key and store it in 'key' variable
printf("\n Ciphered text : ");
for(i=0; i<strlen(string); i++)
{
string[i]=string[i]^key[i]; //Perform the XOR operation on i'th caharcter of the string and i'th character of the key.
}
printf("%s\n",string); //print the ciphered text to the screen.
return 0;
}
About XOR Encryption Method:
In Cryptography, the XOR cipher is a type of additivecipher, and an encryption algorithm. Operation of XOR Encryption Algorithm is based on following four principles.
- A XOR 0 = A
- A XOR A = 0
- (A XOR B) XOR C = A XOR (B XOR C)
- (B XOR A) XOR A = B XOR 0 = B
Example: If A=1 and B=0 then A XOR B = 1
If A=1 and B=1 then A XOR B = 0
How XOR Encryption Works?
XOR Encryption works by repeating XOR operation on each character of the given string and the key string. So to decrypt the encrypted data, just pass the decrypted text as the string input and the same key used for the encryption as the key.
Example:(From Wikipedia)
In 8-bit ASCII, the string 'Wiki' can be represented as '01010111 01101001 01101011 01101001', where each 8-bit word represents the letters W, i, k and i respectively.
The XOR operation is carried out in the follwing manner using the key '11110011'.
01010111 XOR 11110011 = 10100100
01101001 XOR 11110011 = 10011010
01101011 XOR 11110011 = 10011000
01101001 XOR 11110011 = 10011010
So the encrypted output will be ascii text equivalent of '10100100 10011010 10011000 10011010'
This is the simplest working example of the XOR encryption. There are other implementations too, which stands different because of their iteration steps.
Note:- XOR encryption can be made very strong(Even unbreakable) by using true random numbers. But normally XOR encryption is considered to be very weak. This is because encrypted data can be subjected to plaintext attacks to get the key and there by leading to the possibility of decryption of confidential data. So no standards recommend XOR Encryption for data protection.
Implementation of XOR Encryption In C
As i have already explained the basic technique of XOR Encryption, i am not going to explain much about the program and it's working wherever it is not necessary to do so. Also remember that while using the below specified code to encrypt textual data, the key should have atleast the length of the string to be encrypted. Otherwisse there might be some data lose after decrypting again.
Here goes a simple C Code:-
#include <stdio.h>
#include <string.h>
int main()
{
int i;
char string[100]="";
char key[100]="!@#$%^&*()_"; //Default key.
printf("\n Enter the string to be ciphered: ");
scanf("%s",&string); //Read the string to be encrypted and store it in 'string' variable
printf("\n Enter the key: ");
scanf("%s",&key); //Read the string to be used as key and store it in 'key' variable
printf("\n Ciphered text : ");
for(i=0; i<strlen(string); i++)
{
string[i]=string[i]^key[i]; //Perform the XOR operation on i'th caharcter of the string and i'th character of the key.
}
printf("%s\n",string); //print the ciphered text to the screen.
return 0;
}
No comments:
Post a Comment