Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9938fb1

Browse files
Codes Uploaded
1 parent f72aa1f commit 9938fb1

File tree

6 files changed

+839
-0
lines changed

6 files changed

+839
-0
lines changed

‎Codes/matrices.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#define matrices
2+
//Matrix for Initial Permutation
3+
int initialPermutation[64] = { 58, 50, 42, 34, 26, 18, 10, 2,
4+
60, 52, 44, 36, 28, 20, 12, 4,
5+
62, 54, 46, 38, 30, 22, 14, 6,
6+
64, 56, 48, 40, 32, 24, 16, 8,
7+
57, 49, 41, 33, 25, 17, 9, 1,
8+
59, 51, 43, 35, 27, 19, 11, 3,
9+
61, 53, 45, 37, 29, 21, 13, 5,
10+
63, 55, 47, 39, 31, 23, 15, 7 };
11+
12+
//D Box Permutation Table (Expansion Permutation) 32bits->48bits RPT
13+
int dBox[48] = { 32, 1, 2, 3, 4, 5, 4, 5,
14+
6, 7, 8, 9, 8, 9, 10, 11,
15+
12, 13, 12, 13, 14, 15, 16, 17,
16+
16, 17, 18, 19, 20, 21, 20, 21,
17+
22, 23, 24, 25, 24, 25, 26, 27,
18+
28, 29, 28, 29, 30, 31, 32, 1 };
19+
20+
// S-box Table
21+
int sbox[8][4][16] = { { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
22+
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
23+
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
24+
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 },
25+
{ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
26+
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
27+
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
28+
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 },
29+
30+
{ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
31+
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
32+
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
33+
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 },
34+
{ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
35+
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
36+
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
37+
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 },
38+
{ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
39+
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
40+
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
41+
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 },
42+
{ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
43+
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
44+
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
45+
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 },
46+
{ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
47+
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
48+
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
49+
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 },
50+
{ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
51+
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
52+
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
53+
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } };
54+
55+
// Permutation Table for P-Box Permutation
56+
int pbox[32] = { 16, 7, 20, 21,
57+
29, 12, 28, 17,
58+
1, 15, 23, 26,
59+
5, 18, 31, 10,
60+
2, 8, 24, 14,
61+
32, 27, 3, 9,
62+
19, 13, 30, 6,
63+
22, 11, 4, 25 };
64+
65+
// Matrix for Final Permutation
66+
int finalPermutation[64] = { 40, 8, 48, 16, 56, 24, 64, 32,
67+
39, 7, 47, 15, 55, 23, 63, 31,
68+
38, 6, 46, 14, 54, 22, 62, 30,
69+
37, 5, 45, 13, 53, 21, 61, 29,
70+
36, 4, 44, 12, 52, 20, 60, 28,
71+
35, 3, 43, 11, 51, 19, 59, 27,
72+
34, 2, 42, 10, 50, 18, 58, 26,
73+
33, 1, 41, 9, 49, 17, 57, 25 };
74+
75+
//Matrix for Key Transformation (64->48bits)
76+
int keyTransformation[56] = { 57, 49, 41, 33, 25, 17, 9,
77+
1, 58, 50, 42, 34, 26, 18,
78+
10, 2, 59, 51, 43, 35, 27,
79+
19, 11, 3, 60, 52, 44, 36,
80+
63, 55, 47, 39, 31, 23, 15,
81+
7, 62, 54, 46, 38, 30, 22,
82+
14, 6, 61, 53, 45, 37, 29,
83+
21, 13, 5, 28, 20, 12, 4 };
84+
85+
// Key- Compression Table (56->48 bits)
86+
int keyCompresssion[48] = { 14, 17, 11, 24, 1, 5,
87+
3, 28, 15, 6, 21, 10,
88+
23, 19, 12, 4, 26, 8,
89+
16, 7, 27, 20, 13, 2,
90+
41, 52, 31, 37, 47, 55,
91+
30, 40, 51, 45, 33, 48,
92+
44, 49, 39, 56, 34, 53,
93+
46, 42, 50, 36, 29, 32 };
94+
95+
//Matrix denoting no of left shifts to be performed in each round
96+
int shiftsMatrix[16] = { 1, 1, 2, 2,
97+
2, 2, 2, 2,
98+
1, 2, 2, 2,
99+
2, 2, 2, 1 };
100+
101+

‎Codes/nestedParallelism.cpp

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include <bits/stdc++.h>
2+
#include <omp.h>
3+
#include <fstream>
4+
#include "matrices.h"
5+
#include "numberConversions.h"
6+
using namespace std;
7+
string permutation(string str, int* arr, int n)
8+
{
9+
string res = "";
10+
for (int i = 0; i < n; i++) {
11+
res += str[arr[i] - 1];
12+
}
13+
return res;
14+
}
15+
16+
string shift_left(string k, int shifts)
17+
{
18+
string s = "";
19+
for (int i = 0; i < shifts; i++) {
20+
for (int j = 1; j < 28; j++) {
21+
s += k[j];
22+
}
23+
s += k[0];
24+
k = s;
25+
s = "";
26+
}
27+
return k;
28+
}
29+
30+
string xorOperation(string str1, string str2)
31+
{
32+
string xored = "";
33+
for (int i = 0; i < str1.size(); i++) {
34+
if (str1[i] == str2[i])
35+
xored += "0";
36+
else
37+
xored += "1";
38+
}
39+
return xored;
40+
}
41+
string encryption(string pt,vector<string> roundKeys)
42+
{
43+
// String to binary
44+
pt = str2bin(pt);
45+
// Initial Permutation Process
46+
pt = permutation(pt, initialPermutation, 64);
47+
// Splitting of 64bits plain text to LPT and RPT of 32 bits each
48+
string left = pt.substr(0, 32);
49+
string right = pt.substr(32, 32);
50+
for (int i = 0; i < 16; i++) {
51+
// Exapansion Permutation
52+
string expandedRPT = permutation(right, dBox, 48);
53+
// XOR of RoundKey and expandedRPT
54+
string x = xorOperation(roundKeys[i],expandedRPT);
55+
// S-boxes
56+
//result string array for storing the 4 bits outputs for 8*6 bits input
57+
string result[8];
58+
//res stores the final result from s box. i.e. concat all the result array elements
59+
string res = "";
60+
int row[8],col[8],val[8];
61+
//nested level parallelism
62+
#pragma omp parallel for num_threads(8) schedule(static)
63+
for (int i = 0; i < 8; i++) {
64+
//the value of '0' is 48, '1' is 49 and so on. but since we are referring the matrix index, we are interested in 0,1,..
65+
//So, the '0' should be subtracted . i.e. the 49 value of '1' will be 49-48=1.
66+
int tid=omp_get_thread_num();
67+
row[tid] = 2 * int(x[tid * 6] - '0') + int(x[tid * 6 + 5] - '0');
68+
col[tid] = 8 * int(x[tid * 6 + 1] - '0') + 4 * int(x[tid * 6 + 2] - '0') + 2 * int(x[tid * 6 + 3] - '0') + int(x[tid * 6 + 4] - '0');
69+
val[tid] = sbox[tid][row[tid]][col[tid]];
70+
result[tid]= decimalToBinary(val[tid]);
71+
}
72+
for(int i=0;i<8;i++) res+=result[i];
73+
// P-Box Permutation
74+
res = permutation(res, pbox, 32);
75+
// XOR of left and res
76+
x = xorOperation(res, left);
77+
left = x;
78+
// Swap left and right in every rounds except the last round
79+
if (i != 15) {
80+
swap(left, right);
81+
}
82+
}
83+
// Left and Right combined
84+
string combined = left + right;
85+
// Final Permutation to obtain 64bits cipher text
86+
string cipher = bin2str(permutation(combined, finalPermutation, 64));
87+
return cipher;
88+
}
89+
int main(){
90+
double t1=omp_get_wtime();
91+
//Reading from the file and storing the plaintext in pt vector
92+
vector<string> pt;
93+
string c,temp;
94+
ifstream MyReadFile("plaintext.txt");
95+
while(getline(MyReadFile,c)){
96+
temp+=c;
97+
}
98+
while(temp.length()%8!=0){
99+
temp+=" ";
100+
}
101+
for(int i=0;i<temp.length();i=i+8){
102+
pt.push_back(temp.substr(i,8));
103+
}
104+
MyReadFile.close();
105+
string key = "ABC12532110EDA56";
106+
key = convertToBinary(key);
107+
key = permutation(key, keyTransformation, 56); // key without parity
108+
//Splitting 56 bit keys to left and right of 28 bits each
109+
string left = key.substr(0, 28);
110+
string right = key.substr(28, 28);
111+
vector<string> roundKeys; // Declaring vector for storing keys of 16 rounds
112+
for (int i = 0; i < 16; i++) {
113+
//Left Shift and Right Shift done to the respective left and right keys in each round
114+
left = shift_left(left, shiftsMatrix[i]);
115+
right = shift_left(right, shiftsMatrix[i]);
116+
string combinedkey = left + right;
117+
// Key Compression : Converting 56 bit key to 48 bit combined key
118+
string RoundKey = permutation(combinedkey, keyCompresssion, 48);
119+
roundKeys.push_back(RoundKey);
120+
}
121+
122+
//Encryption
123+
vector<string> ciphertext;
124+
string cipher[pt.size()];
125+
cout << "Encrypting..."<<endl;
126+
omp_set_nested(1); //enable nested parallelism
127+
#pragma omp parallel for schedule(guided) num_threads(8)
128+
for(int i=0;i<pt.size();i++){
129+
cipher[i] = encryption(pt[i],roundKeys);
130+
}
131+
for(int i=0;i<pt.size();i++){
132+
ciphertext.push_back(cipher[i]);
133+
}
134+
//Writing Cipher Text to File
135+
ofstream writeObj;
136+
remove("encrypted.txt");
137+
writeObj.open("encrypted.txt",ios::app);
138+
int j=0;
139+
for(j=0;j<ciphertext.size()-1;j++){
140+
writeObj << ciphertext[j];
141+
}
142+
writeObj<<ciphertext[j+1];
143+
writeObj.close();
144+
cout << "Encryption Process Completed."<<endl;
145+
146+
//Decryption : Reversing the round keys and executing the encryption process to get Plain Text
147+
reverse(roundKeys.begin(),roundKeys.end());
148+
string decrypted;
149+
string text[ciphertext.size()];
150+
cout << "Decrypting..."<<endl;
151+
omp_set_nested(1);
152+
#pragma omp parallel for schedule(guided) num_threads(8)
153+
for(int i=0;i<ciphertext.size();i++){
154+
text[i] = encryption(ciphertext[i],roundKeys);
155+
}
156+
for(int i=0;i<ciphertext.size();i++){
157+
decrypted+=text[i];
158+
}
159+
double t2=omp_get_wtime();
160+
cout << "Decryption Process Completed"<<endl;
161+
cout << "\nDecrypted Plain Text:\n " << decrypted << endl;
162+
cout <<endl<< "Time taken:"<<(t2-t1)<<"s"<<endl;
163+
}
164+

‎Codes/numberConversions.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <bits/stdc++.h>
2+
#include <string.h>
3+
#define numberConversions
4+
std::string str2bin(std::string s)
5+
{
6+
std::string binary;
7+
for(int i=0;i<s.length();i++){
8+
binary+=std::bitset<8>(s[i]).to_string();
9+
}
10+
return binary;
11+
}
12+
std::string bin2str(std::string str)
13+
{
14+
std::string s;
15+
for(int i=0;i<str.length();i=i+8){
16+
std::string bin=str.substr(i,8);
17+
char c = static_cast<char>(std::bitset<8>(bin.substr(0,8)).to_ulong());;
18+
s+=c;
19+
}
20+
return s;
21+
}
22+
std::string decimalToBinary(int val){
23+
std::string res;
24+
//When ASCII encoding is used, the integer value of '0' is 48. So, adding '0' i.e. 48 to get the integer value of 1 as '1'.
25+
//i.e. Converting digit to its corresponding character.
26+
res += char(val/ 8 + '0');
27+
val= val% 8;
28+
res += char(val/ 4 + '0');
29+
val= val% 4;
30+
res += char(val/ 2 + '0');
31+
val= val% 2;
32+
res += char(val+ '0');
33+
return res;
34+
}
35+
std::string convertToBinary(std::string s)
36+
{
37+
std::string res="";
38+
for(int i=0;i<s.length();i++){
39+
switch(s[i]){
40+
case '0':
41+
res+="0000";
42+
break;
43+
case '1':
44+
res+="0001";
45+
break;
46+
case '2':
47+
res+="0010";
48+
break;
49+
case '3':
50+
res+="0011";
51+
break;
52+
case '4':
53+
res+="0100";
54+
break;
55+
case '5':
56+
res+="0101";
57+
break;
58+
case '6':
59+
res+="0110";
60+
break;
61+
case '7':
62+
res+="0111";
63+
break;
64+
case '8':
65+
res+="1000";
66+
break;
67+
case '9':
68+
res+="1001";
69+
break;
70+
case 'A':
71+
case 'a':
72+
res+="1010";
73+
break;
74+
case 'B':
75+
case 'b':
76+
res+="1011";
77+
break;
78+
case 'C':
79+
case 'c':
80+
res+="1100";
81+
break;
82+
case 'D':
83+
case 'd':
84+
res+="1101";
85+
break;
86+
case 'E':
87+
case 'e':
88+
res+="1110";
89+
break;
90+
case 'F':
91+
case 'f':
92+
res+="1111";
93+
break;
94+
}
95+
}
96+
return res;
97+
}
98+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /