Cryptography
Cryptography
On
Cryptography And Network
Security Sessional
Computer Science And Engineering
Sylhet Engineering College
Prepared For
Md. Mehrab Hossain
Adjunct Lecturer
Department: CSE
Prepared By
Pranta Routh
Reg: 2020331524
Algorithm:
1. If b == 0, then GCD(a, b) = a.
2. Otherwise, compute GCD(b, a % b) recursively.
Code:
#include <iostream>
using namespace std;
return a;
return gcd(b, a % b);
}
int main() {
int a, b;
return 0;
Input:
Output:
2.Prime Checking Using Seive:
Algorithm:
- If isPrime[p] is true, mark all multiples of p (i.e., p*p, p*p + p, ... ≤ n) as false.
Code:
#include <iostream>
#include <vector>
isPrime.assign(n + 1, true);
if (isPrime[p]) {
isPrime[i] = false;
int main() {
cout << "Enter the upper limit for primes (e.g., 1000): ";
vector<bool> isPrime;
sieve(limit, isPrime);
cout << "Enter a number to check if it's prime (<= " << limit << "): ";
else
cout << num << " is not a prime number." << endl;
} else {
cout << "Number exceeds the sieve limit. Increase the limit." << endl;
return 0;
Input:
Output:
3. Prime Factorization:
Algorithm:
Code:
#include <iostream>
#include <vector>
using namespace std;
vector<int> primeFactorization(int n) {
vector<int> factors;
factors.push_back(2);
n /= 2;
}
for (int i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
factors.push_back(i);
n /= i;
}
}
if (n > 2)
factors.push_back(n);
return factors;
}
int main() {
int num;
cout << "Enter a positive integer to factor: ";
return 0;
}
Input:
Output:
4.Normal Hash Function:
Algorithm:
A normal (non-secure) hash function for demonstration:
This is not secure for real-world cryptography but useful for learning purposes.
Code:
#include <iostream>
#include <string>
}
return hash;
int main() {
string text;
getline(cin, text);
return 0;
}
Input:
Output:
5.Parity Check Code:
Algorithm:
Code:
#include <iostream>
#include <string>
int countOnes = 0;
while (byte) {
countOnes += byte & 1;
byte >>= 1;
return countOnes % 2;
int main() {
string data;
getline(cin, data);
cout << "Computed parity bit (even parity): " << parityBit << endl;
return 0;
}
Input:
Output:
6. Merkle – Damgard :
Algorithm:
4. Final hash = hn
Code:
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
h ^= block;
h = (h << 3) | (h >> (32 - 3)); // Left rotate by 3
return h;
}
unsigned int blockToInt(const string& block) {
unsigned int val = 0;
}
return val;
message += '\0';
stringstream ss;
ss << hex << setfill('0') << setw(8) << h;
return ss.str();
}
int main() {
string input;
cout << "Enter a string to hash using Merkle–Damgård: ";
getline(cin, input);
return 0;
Input:
Output:
7. Simplified DES (SDES):
Algorithm:
3. First Round
5. Second Round
#include <iostream>
#include <bitset>
#include <vector>
return output;
}
int SBOX[2][4][4] = {
};
int s0 = SBOX[0][row][col];
int s1 = SBOX[1][row][col];
int sb = sbox(xorOut);
return cipher;
}
return plain;
}
int main() {
return 0;
Input:
Output: