Commit de420985 by raliis

Caesar cipher encryption/decryption

parent 51717909
Showing with 135 additions and 0 deletions
#include <stdio.h>
void encrypt ();
void decrypt ();
int main (void)
{
char valik;
printf ("Do you wish to encrypt or decrypt?\n");
do
{
printf ("Please enter a valid option(e/d):");
scanf ("%c", &valik);
}
while (valik != 'e' && valik != 'd');
if (valik == 'e')
{
encrypt ();
}
else if (valik == 'd')
{
decrypt ();
}
else
{
printf ("option does not exist");
}
return 0;
}
void encrypt ()
{
char ni[80] = "sisend.txt", no[80] = "tul.txt";
char c;
int key;
int caesar;
FILE *fi, *fo;
fi = fopen (ni, "r");
fo = fopen (no, "w");
printf ("Enter the encryption key to encrypt: ");
scanf ("%d", &key);
do
{
c = fgetc(fi);
if (feof(fi))
{
break;
}
if (c >= 65 && c <= 90)
{
if ((c + key) > 90)
{
caesar = c - (26 - key);
}
else
{
caesar = (int)c + key;
}
}
else if (c >= 97 && c <= 122)
{
if ((c + key) > 122)
{
caesar = c - (26 - key);
}
else
{
caesar = (int)c + key;
}
}
printf ("%c", caesar);
fprintf (fo, "%c", caesar);
}
while (1);
printf (" \n");
}
void decrypt ()
{
char ni[80] = "sisend.txt", no[80] = "tul.txt";
char c;
int key;
int caesar;
FILE *fi, *fo;
fi = fopen (ni, "r");
fo = fopen (no, "w");
printf("Enter the encryption key to decrypt: ");
scanf ("%d", &key);
do
{
c = fgetc(fi);
if (feof(fi))
{
break;
}
if (c >= 65 && c <= 90)
{
if ((c - key) < 65)
{
caesar = c + (26 - key);
}
else
{
caesar = (int)c - key;
}
}
else if (c >= 97 && c <= 122)
{
if ((c - key) < 97)
{
caesar = c + (26 - key);
}
else
{
caesar = (int)c - key;
}
}
printf ("%c", caesar);
fprintf (fo, "%c", caesar);
}
while (1);
printf (" \n");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment