-
Notifications
You must be signed in to change notification settings - Fork 1
/
caesar_cipher.c
55 lines (51 loc) 路 1.32 KB
/
caesar_cipher.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
45
46
47
48
49
50
51
52
53
54
55
/*
Author - Ajay Badrinath
Will document this soon....
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* GENERATE_CHAR_SET(){
char*alphabets=(char*)malloc(91*sizeof(char));
for(int i=32;i<123;i++){sprintf((alphabets+(i-32)),"%c",i);}
return alphabets;
}
int find_char(char*char_set,char val){
for(int i=0;i<strlen(char_set);i++){
if(*(char_set+i)==val){
return i;
}
}
return -1;
}
char* Encrypt(char* char_set,char*message,int key){
char*cipher=(char*) malloc(sizeof(char)*1024);
int pos, idx=0;
for(int i=0;i<strlen(message);i++){
pos=find_char(char_set,*(message+i));
*(cipher+idx++)=*(char_set+(pos+key)%91);
}
return cipher;
}
char* Decrypt(char*char_set,char*cipher,int key){
char*decrypt_text=(char*) malloc(sizeof(char)*1024);
int pos, idx=0;
for(int i=0;i<strlen(cipher);i++){
pos=find_char(char_set,*(cipher+i));
*(decrypt_text+idx++)=*(char_set+(pos-key)%91);
}
//printf("\n%s\n",decrypt_text);
return decrypt_text;
}
void main(){
char* char_set=GENERATE_CHAR_SET();
int key;
char*message=(char*)malloc(sizeof(char)*1024);
printf("Enter the message to Encrypt : ");
scanf("%[^\n]%*c",message);
printf("Enter the key :");
scanf("%d",&key);
char* cipher=Encrypt(char_set,message,key);
printf("Encrypted Text: %s\n",cipher);
printf("Decrypted Text: %s\n",Decrypt(char_set,cipher,key));
}