Algorithm/Steps :
- Take plain text from file named input.txt
- Take Key as input from key.txt
- Add same number of characters as plain text from key
- Do encryption and save Encrypted text in output.txt
- Hence decrypt the encrypted message and save decrypted text in input.txt
Code :
Encryption
#include<stdio.h>
#include<string.h>
int
main()
{
char plain[50],ci[50],key[50];
FILE *key1 =
fopen("key.txt","r");
FILE *plain1 =
fopen("input.txt","r");
FILE *cipher =
fopen("output.txt","w");
fscanf(plain1, "%s",
plain);
printf("\n%s",plain);
fscanf(key1, "%s", key);
printf("\n%s",key);
int len = strlen(key);
int len1 = strlen(plain);
int j=0,i=len;
while(i != len1)
{
l1:
key[len] = key[j];
if(j==(len-1))
{
j=0;
goto l1;
}
len++;
i++;
j++;
}
//printf("\nKey is:
%s",key);
i=0;j=0;
while(plain[i] != '\0')
{
ci[j] = (plain[i] +
key[i]) % 26 + 'A';
//printf("%c",ci[j]);
i++;
j++;
}
ci[j] = '\0';
printf("\nEncryted text is:
%s",ci);
fprintf(cipher,"%s",ci);
return 0;
}
Decryption
#include<stdio.h>
#include<string.h>
int
main()
{
char plain[50],ci[50],key[50];
FILE *key1 = fopen("key.txt","r");
FILE *cipher =
fopen("output.txt","r");
fscanf(cipher, "%s", ci);
printf("\n%s",ci);
fscanf(key1, "%s", key);
int len = strlen(key);
int len1 = strlen(ci);
int j=0,i=len;
while(i != len1)
{
l1:
key[len] = key[j];
if(j==(len-1))
{
j=0;
goto l1;
}
len++;
i++;
j++;
}
printf("\n%s",key);
i=0;j=0;
while(ci[i] != '\0')
{
plain[j] = (((ci[i] -
key[i])+26) % 26) + 'A';
i++;
j++;
}
plain[j] = '\0';
printf("\nDecrypted text is:
%s",plain);
return 0;
}