Professional Documents
Culture Documents
21BCB0134 VL2023240502947 Ast02
21BCB0134 VL2023240502947 Ast02
Name : Pranshu
kumar Reg. No :
21BCB0134
Encryption/Decryption
DES Algorithm:
The process begins with the 64-bit plain text block getting handed over to an initial
permutation (IP) function.
The initial permutation (IP) is then performed on the plain text.
Next, the initial permutation (IP) creates two halves of the permuted block, referred to as
Left Plain Text (LPT) and Right Plain Text (RPT).
Each LPT and RPT goes through 16 rounds of the encryption process.
Finally, the LPT and RPT are rejoined, and a Final Permutation (FP) is performed on the
newly combined block.
The result of this process produces the desired 64-bit ciphertext.
The encryption process step (step 4, above) is further broken down into five stages:
Key transformation
Expansion permutation
S-Box permutation
P-Box permutation
XOR and swap
For decryption, we use the same algorithm, and we reverse the order of the 16 round keys.
2
Code:
#include <iostream>
using namespace
std;
//Functions
int temp[n];
temp[i] = arr[index[i] -
arr[i] = temp[i];
l[i] = arr[i];
3
+) r[j] = arr[i];
4
}
{ int decimal = 0;
{ int i = 0;
while (opSn != 0) {
ar[i++] = opSn %
2; opSn = opSn /
2;
arr3[i] = arr1[i];
+) arr3[i] = arr2[j];
int S0[4][4] = {{1, 0, 3, 2}, {3, 2, 1, 0}, {0, 2, 1, 3}, {3, 1, 3, 2}},
//S0
5
int rowS0bin[2] = {a[0], a[3]}, colS0bin[2] = {a[1], a[2]};
6
int rowS0dec = bin2dec(rowS0bin, 2), colS0dec = bin2dec(colS0bin, 2);
dec2bin(opS0dec, opS0bin);
//S1
dec2bin(opS1dec, opS1bin);
opS0S1[i] = opS0bin[i];
+) opS0S1[i] = opS1bin[j];
int temp[n];
temp[i] =
left_array[i];
left_array[i] =
n; i++)
right_array[i] = temp[i];
7
void XOR(int arr1[], int arr2[], int n) {
int temp[n];
8
for (int i = 0; i < n; i++) {
+) arr2[i] = temp[i];
int temp[d];
+) temp[i] = arr[i];
+) arr[i] = temp[j];
class KeyGeneration
{ private:
public:
KeyGeneration()
9
Permutation(master_key, P10_rule,
printArray(master_key,
printArray(temp_left,
printArray(temp_right,
leftRotate(temp_left, 1, 5);
leftRotate(temp_right, 1, 5);
printArray(temp_left,
printArray(temp_right,
k1[i] =
master_key[i];
printArray(k1, 8);
leftRotate(temp_left, 2, 5);
leftRotate(temp_right, 2, 5);
1
0
cout << "l = ";
1
1
printArray(temp_left,
printArray(temp_right,
k2[i] =
master_key[i];
printArray(k2, 8);
};
class Roundfunction
{ private:
public:
printArray(r_arr2,
printArray(r_arr2, 8);
1
2
cout << endl;
1
3
Split(r_arr2, 8, a, b);
printArray(a,
"; printArray(b,
endl;
S_box(a, b, opS0S1);
printArray(opS0S1,
printArray(l_arr, 4);
};
{ private:
1
4
public:
encrypt() {
cout <<
endl;
printArray(arr, 8);
printArray(l_arr, 4);
printArray(r_arr, 4);
printArray(l_arr, 4);
printArray(r_arr, 4);
1
Roundfunction::roundfun(key2, l_arr, r_arr, fk1);
1
Permutation(fk1, IP_inv_rule, 8);
cout << "After IP-Inverse, 8-bit Cipher Text is: " <<
};
{ private:
public:
decrypt() {}
endl; printArray(arr,
8);
Split(arr, 8, l_arr,
<< endl;
printArray(l_arr, 4);
printArray(r_arr, 4);
endl; printArray(l_arr,
4);
1
printArray(r_arr, 4);
1
Permutation(fk1, IP_inv_rule, 8);
cout << "After IP-Inverse, 8-bit Plain Text is: " << endl;
printArray(fk1, 8);
};
int main() {
char input;
int arr[8] = {}, master_key[10] = {}, k1[8] = {}, k2[8] = {}, fk1[8] = {};
//Key
cout << "Enter 10-bit Master Key (using space)" << endl;
KeyGeneration k;
cout << "Enter e for Encryption | Enter d for Decryption | Enter b for Both" << endl;
cout << "Enter 8-bit Plain Text (using space)" << endl;
1
}
if ((sizeof(arr) / sizeof(arr[0])) != 8)
encrypt e;
8; i++)
arr[i] = fk1[i];
cout <<
endl;
decrypt d;
cout << "Enter 8-bit Plain Text (using space)" << endl;
if ((sizeof(arr) / sizeof(arr[0])) != 8)
encrypt e;
if ((sizeof(arr) / sizeof(arr[0])) != 8)
decrypt d;
1
d.decryp(arr, k1, k2, fk1);
} else
1
throw "Error, Choose correct option";
return 0;
1
Output:
1
1
1
1
RSA IMPLEMENTATION
RSA Algorithm:
Begin
1. Choose two prime numbers p and q.
2. Compute n = p*q.
3. Calculate phi = (p-1) * (q-1).
4. Choose an integer e such that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.
5. Calculate d as d ≡ e−1 (mod phi(n)); here, d is the modular multiplicative inverse of e modulo
phi(n).
6. For encryption, c = me mod n, where m = original message.
7. For decryption, m = c d mod n.
End
An integer.
Not be a factor of n.
1
The private key has been made of d.
Code:
#include<iostream>
#include<math.h>
// find gcd
int t;
while(1) {
t= a%b;
if(t==0)
return b;
a = b;
b= t;
int main() {
double q = 11;
double n=p*q;//calculate
n double track;
//public key
double e=7;
//for checking that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.
while(e<phi) {
track =
gcd(e,phi);
2
if(track==1)
2
break;
else
e++;
//private key
double d1=1/e;
double d=fmod(d1,phi);
double message = 9;
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);
cout<<"\n"<<"p = "<<p;
cout<<"\n"<<"q = "<<q;
cout<<"\n"<<"n = pq = "<<n;
cout<<"\n"<<"phi = "<<phi;
cout<<"\n"<<"e = "<<e;
cout<<"\n"<<"d = "<<d;
cout<<"\n"<<"Decrypted message =
"<<m; return 0;
2
Output:
**************************************************