Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

NETWORK SECURITY LAB

2. Writing program in C to Encrypt/Decrypt using XOR key

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
char *xorcipher(char* data, char* key,int datalen,int keylen)
{
char* output = (char*)malloc(sizeof(char)*datalen);
int i;
for(i=0;i<datalen;++i)
{
output[i] = data[i]^key[i%keylen];
}
return output;
}
void main()
{
char *text = "Lazyyy Me";
char *key = "GigaChad";
int datalen = strlen(text);
int keylen = strlen(key);
char *ciphertext = xorcipher(text,key,datalen,keylen);
int i;
for (i=0;i<datalen;i++)
{
printf("%c", ciphertext[i]);
}
char *plaintext = xorcipher(ciphertext,key,datalen,keylen);
printf("\n");
for (i=0;i<datalen; i++)
{
printf("%c", plaintext[i]);
}
}

You might also like