-
Notifications
You must be signed in to change notification settings - Fork 765
/
Copy pathaffinecypher
111 lines (81 loc) · 1.29 KB
/
affinecypher
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int checkcoprime(int num)
{
int a,b,temp;
a=num;
b=26;
while(b!=0)
{
temp=b;
b=a%b;
a=temp;
}
return a;
}
main()
{
int a,b,check,numstr[1000];
char str[1000];
printf("Enter a ( 1 to 25) both included which is co-prime with 26\n");
scanf("%d",&a);
if(a>=1 && a<=25)
{
check=checkcoprime(a);
if(check!=1)
{
printf("Enter the one which is co-prime with 26\n");
exit(0);
}
}
else
{
printf("Enter between 1 and 25\n ");
exit(1);
}
printf("Enter b ( 0 to 25) both included \n");
scanf("%d",&b);
if(b<1 || a>25)
{
printf("Enter b between 0 to 25 \n");
exit(3);
}
printf("Enter the string to be encypted: \n");
fflush(stdin);
gets(str);
// converting string into capital letters
int i,j;
for(i=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[i]=toupper(str[i]);
}
}
str[i]='\0';
printf("Entered string is: %s \n",str);
for(i=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
numstr[i]=str[i]-'A';
}
else
{
numstr[i]=-20;
}
}
printf("The affine cipher is : ");
for(i=0;i<strlen(str);i++)
{
if(numstr[i]!=-20)
{
numstr[i]= ((a*numstr[i])+b)%26;
printf("%c",numstr[i]+'A');
}
else{
printf(" ");
}
}
}