Assignment 1: DATE-31-10-2021
Assignment 1: DATE-31-10-2021
DATE-31-10-2021
#include <bits/stdc++.h>
string hex2bin(string s)
mp['0'] = "0000";
mp['1'] = "0001";
mp['2'] = "0010";
mp['3'] = "0011";
mp['4'] = "0100";
mp['5'] = "0101";
mp['6'] = "0110";
mp['7'] = "0111";
mp['8'] = "1000";
mp['9'] = "1001";
mp['A'] = "1010";
mp['B'] = "1011";
mp['C'] = "1100";
mp['D'] = "1101";
mp['E'] = "1110";
mp['F'] = "1111";
bin += mp[s[i]];
return bin;
string bin2hex(string s)
mp["0000"] = "0";
mp["0001"] = "1";
mp["0010"] = "2";
mp["0011"] = "3";
mp["0100"] = "4";
mp["0101"] = "5";
mp["0110"] = "6";
mp["0111"] = "7";
mp["1000"] = "8";
mp["1001"] = "9";
mp["1010"] = "A";
mp["1011"] = "B";
mp["1100"] = "C";
mp["1101"] = "D";
mp["1110"] = "E";
mp["1111"] = "F";
string ch = "";
ch += s[i];
ch += s[i + 1];
ch += s[i + 2];
ch += s[i + 3];
hex += mp[ch];
return hex;
return per;
string s = "";
s += k[j];
s += k[0];
k = s;
s = "";
return k;
if (a[i] == b[i]) {
ans += "0";
else {
ans += "1";
return ans;
// Hexadecimal to binary
pt = hex2bin(pt);
// Initial Permutation
cout << "After initial permutation: " << bin2hex(pt) << endl;
// Splitting
6, 7, 8, 9, 8, 9, 10, 11,
// S-box Table
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25 };
// Expansion D-box
// S-boxes
string op = "";
op += char(val / 8 + '0');
val = val % 8;
op += char(val / 4 + '0');
val = val % 4;
op += char(val / 2 + '0');
val = val % 2;
op += char(val + '0');
// Straight D-box
x = xor_(op, left);
left = x;
// Swapper
if (i != 15) {
swap(left, right);
cout << "Round " << i + 1 << " " << bin2hex(left) << " "
// Combination
// Final Permutation
return cipher;
int main()
// pt is plain text
string pt, key;
cin>>pt;
cin>>key;
// Key Generation
// Hex to binary
key = hex2bin(key);
int shift_table[16] = { 1, 1, 2, 2,
2, 2, 2, 2,
1, 2, 2, 2,
2, 2, 2, 1 };
// Key- Compression Table
// Splitting
// Shifting
// Combining
// Key Compression
rkb.push_back(RoundKey);
rk.push_back(bin2hex(RoundKey));
}
cout << "\nEncryption:\n\n";
reverse(rkb.begin(), rkb.end());
reverse(rk.begin(), rk.end());
DES OUTPUT-