-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt-01.cpp
More file actions
138 lines (128 loc) · 2.51 KB
/
encrypt-01.cpp
File metadata and controls
138 lines (128 loc) · 2.51 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include<stdio.h>
#include<math.h>
#include<string.h>
long long int p,q,n,t,flag,e[10000000],d[10000000],temp[10000000],j,m[10000000],en[10000000],i;
char msg[10000000];
long long int prime(long long int);
long long int cd(long long int);
void encrypt();
void ce();
long long int prime(long long int pr){
long long int i;
j=sqrt(pr);
for(i=2;i<=j;i++)
{
if(pr%i==0)
return 0;
}
return 1;}
void ce()
{
long long int k;
k=0;
for(i=2;i<t;i++)
{
if(t%i==0) continue;
flag=prime(i);
if(flag==1&&i!=p&&i!=q)
{
e[k]=i;
flag=cd(e[k]);
if(flag>0)
{
d[k]=flag;
k++;
}
if(k==99)
break;
}
}
}
void encrypt()
{
FILE *ptYazma;
FILE *ptKey;
ptKey = fopen("Secondkey.txt","wb+");
ptYazma = fopen("Encrypted.txt", "wb+");
long long int pt,ct,key=e[0],k,len;
i=0;
len=strlen(msg);
while(i!=len)
{
pt=m[i];
pt=pt-96;
k=1;
for(j=0;j<key;j++)
{
k=k*pt;
k=k%n;
}
temp[i]=k;
fprintf(ptKey,"%d ",temp[i]);
ct=k+96;
fprintf(ptYazma,"%c",ct);
en[i]=ct;
i++;
}
en[i]=-1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for(i=0;en[i]!=-1;i++)
printf("%c",en[i]);
}
long long int cd(long long int x)
{
long long int k=1;
while(1)
{
k=k+t;
if(k%x==0)
return(k/x);
}
}
int main(){
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d",&p);
flag=prime(p);
if(flag==0)
{
printf("\nWRONG INPUT\n");
return 0;
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%d",&q);
flag=prime(q);
if(flag==0||p==q)
{
printf("\nWRONG INPUT\n");
return 0;
}
fflush(stdin);
long long int i=0,u,o;
FILE *ptDosya;
char ch=0,c,filename[100];
printf("If you want choose different file press(x): ");
scanf("%c",&u);
if(u=='x'){
printf("Enter the filename to open(with extensions): ");
scanf("%s", filename);
ptDosya = fopen(filename, "r");}
else{
ptDosya = fopen("msg.txt", "r");}
long long int x=0;
printf("\nENTER MESSAGE\n");
do {
ch = getc(ptDosya);
i = ch;
if(i != -1){
printf("%c",ch);
msg[x]=i;
x++;}
} while (ch != EOF);
for(i=0;msg[i]!=NULL;i++)
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();
encrypt();
return 0;
}