forked from pranavshah029/Parallelizing-Decryption-Alogirthm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_decryption.cpp
206 lines (156 loc) · 3.67 KB
/
parallel_decryption.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<iostream>
#include<omp.h>
#include<stdlib.h>
using namespace std;
int algo1(char cipher[],char key[]){
char message[100], ch;
int i, key1;
int flag=0;
strcpy(message,cipher);
///////////////////main loop for key iteration///////////////////////////
// #pragma omp parallel for ordered
//#pragma omp for ordered
for(int ii=0;ii<30;ii++){
if(flag==0){
int tmp=0;
tmp=key[ii];
key1=tmp;
// cout<<"key been used "<<key1<<endl;
strcpy(message,cipher);
int count=strlen(message);
#pragma omp parallel for ordered num_threads(9)
for(i = 0; i<=count; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 180)
{
ch = ch - key1;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key1;
if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
// cout<<"Decrypted "<<message<<endl;
count=count-1;
char *output=NULL;
output = strstr(message,"hailhitler");
if(output){
cout<<"!!!!!!!!!!!FOUND AND KEY IS "<<key1<<" and message is "<<message;
flag=1;
return 1;
exit(0);
}
}
}
return 0;
}
////////////algo2/////////////////////
int algo2(char cipher[],char key[]){
int a[26][26],m;
for(int i=0;i<26;i++)
{ m=i;
for(int j=0;j<26;j++)
{ if(m<=25)
{a[i][j]=m+97;
m++;
}
else
{a[i][j]=97;
m=1;
}
}
}
char key1;
int plen,klen;
char e[100],d[100];
strcpy(e,cipher);
plen=strlen(e);
char *output=NULL;
// printf("\n Decrypted text::::");
#pragma omp parallel for num_threads(9)
for(int ii=0;ii<30;ii++){
key1=key[ii];
// cout<<"key been used"<<key1<<endl;;
for(int i=0;i<plen;i++)
{ if(e[i]<key1)
{
d[i]=a[0][27-abs(e[i]-key1)];
}
else
d[i]=a[0][e[i]-key1];
// printf(" %c\n",d[i]);
// key1=key[i];
}
}
output = strstr(d,"hail");
if(output){
cout<<"!!!!!!!!!!!FOUND key is "<<key1<<" and message is "<<d;
return 1;
exit(0);
}
}
//////////////main///////////////////
int main()
{
FILE *fptr,*fkey;
char filename[15];
char ch,ch1,ch2;
char cipher[100];
char key[128];
///////////////////cipher section////////////////////////////
fptr = fopen("cipher.txt", "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
int z=0;
while (ch != EOF)
{
ch = fgetc(fptr);
cipher[z]=ch;
z++;
}
//////////////////key section/////////////////
fkey = fopen("key.txt", "r");
if (fkey == NULL)
{
printf("Cannot open file \n");
exit(0);
}
z=0;
char line[128];
while ( fgets ( line, sizeof line, fkey ) != NULL ){
key[z]=atoi(line);
z++;
}
////////////////call alogorithm////////////////////////
int a,b;
#pragma omp parallel sections num_threads(2)
{
#pragma omp section
a=algo1(cipher,key);
#pragma omp section
b=algo2(cipher,key);
}
if(a==1){
cout<<"\n!!!!!!!!!!!FINAL DECRYPTION RESULT"<<endl<<"ALGORITHM USED IS CAESER!!!!!!!!!!!!!!!!!!";
}
else if(b=1){
cout<<"\n!!!!!!!!!!!FINAL DECRYPTION RESULT"<<endl<<"ALGORITHM USED IS POLYALPHABATIC!!!!!!!!!!!!!!!!!!";
}
else{
cout<<"ERROR";
}
return 0;
}