-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar.c
44 lines (39 loc) · 990 Bytes
/
caesar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int encrypt(char c, int key)
{ int x=c;
key%=26;
if(x<=90 && x>=65)
{ if(x+key<90)
x+=key;
else x = 64 + ((x+key)%90);}
if(x<=122 && x>=97)
{ if(x+key<=122)
x+=key;
else x = 96 + ((x+key)%122);}
return x;
}
int main(int argc, char * argv[])
{ int i,j,imax,key;
char plaintext[100],c;
if(argc ==2 && (atoi(argv[1])!=0))
{
int key = atoi(argv[1]);
printf("plaintext: ");
for(i=0;(c= getchar())!='\n';i++)
plaintext[i] = c;
imax = i;
plaintext[imax] = '\0';
int ciphertext[imax];
for(i=0;i<imax;i++)
{ ciphertext[i] = encrypt(plaintext[i],key); }
ciphertext[imax]='\0';
printf("ciphertext: ");
for(i=0;i<imax;i++)
putchar(ciphertext[i]);
putchar('\n');
}
else printf("Usage: ./caesar key");
return 0;
}