-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaffine.c
More file actions
95 lines (95 loc) · 1.17 KB
/
affine.c
File metadata and controls
95 lines (95 loc) · 1.17 KB
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
#include<stdio.h>
#include<string.h>
void encrypt(char *a,int key1,int key2)
{
int i,l=strlen(a);
for(i=0;i<l;i++)
{
a[i]=(((a[i]-65)*key1)+key2)%26;
}
for(i=0;i<l;i++){
a[i]=a[i]+65;
}
for(i=0;i<l;i++)
printf("%c",a[i]);
printf("\n");
}
void decrypt(char *a,int key1,int key2)
{
int num=26;
int i,l=strlen(a),mod;
mod=modinverse(num,key1);
printf("modular inverse value is%d\n",mod);
for(i=0;i<l;i++)
{
a[i]=((a[i]-65)-key2);
if(a[i]<0)
{
while(a[i]<0)
{
a[i]=a[i]+num;
}
a[i]=((a[i]*mod)%26);
}
else
{
a[i]=(a[i]*mod)%26;
}
}
for(i=0;i<l;i++){
a[i]=a[i]+65;
}
for(i=0;i<l;i++)
printf("%c",a[i]);
printf("\n");
}
int modinverse(int a,int b)
{
int z=a;
int q,r,t1=0,t2=1,t;
while(b<0)
{
b=b+a;
}
while(a>1)
{
r=a%b;
q=a/b;
a=b;
b=r;
t=t1-q*t2;
t1=t2;
t2=t;
}
if(t1<0)
t1=t1+z;
return t1;
}
int gcd(int a,int b)
{
int r;
while(b>0)
{
r=a%b;
a=b;
b=r;
}
return a;
}
void main()
{
int key1,key2,g,num=26;
char a[100];
printf("enter the text\n");
scanf("%s",a);
printf("enter the key values\n");
scanf("%d %d",&key1,&key2);
g=gcd(num,key1);
if(g==1)
{
encrypt(a,key1,key2);
decrypt(a,key1,key2);
}
else
printf("enter valid key1\n");
}