Information Network & Security
Information Network & Security
#include <iostream>
#include <string>
using namespace std;
void cipherMenu() {
int choice;
string message;
int key1, key2;
switch (choice) {
case 1:
cout << "Enter key: ";
cin >> key1;
cout << "Encrypted Message: " << additiveCipherEncrypt(message, key1) << endl;
break;
case 2:
cout << "Enter key: ";
cin >> key1;
cout << "Decrypted Message: " << additiveCipherDecrypt(message, key1) << endl;
break;
case 3:
cout << "Enter key: ";
cin >> key1;
cout << "Encrypted Message: " << multiplicativeCipherEncrypt(message, key1) <<
endl;
break;
case 4:
cout << "Enter key: ";
cin >> key1;
cout << "Decrypted Message: " << multiplicativeCipherDecrypt(message, key1) <<
endl;
break;
case 5:
cout << "Enter key1: ";
cin >> key1;
cout << "Enter key2: ";
cin >> key2;
cout << "Encrypted Message: " << affineCipherEncrypt(message, key1, key2) <<
endl;
break;
case 6:
cout << "Enter key1: ";
cin >> key1;
cout << "Enter key2: ";
cin >> key2;
cout << "Decrypted Message: " << affineCipherDecrypt(message, key1, key2) <<
endl;
break;
case 7:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
int main() {
int choice;
do {
cipherMenu();
cout << "Do you want to continue? (1 for yes, 0 for no): ";
cin >> choice;
} while (choice != 0);
return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
// Function prototypes
int main() {
int choice;
do {
cin.ignore();
getline(cin, message);
getline(cin, key);
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
return 0;
}
return encrypted;
int k = 0;
for (char col : key) {
grid[row][colIndex] = message[k++];
decrypted += grid[row];
return decrypted;
int k = 0;
grid[row][col] = message[k++];
}
keyOrder.push_back({key[i], i});
sort(keyOrder.begin(), keyOrder.end());
encrypted += grid[row][col];
return encrypted;
keyOrder.push_back({key[i], i});
sort(keyOrder.begin(), keyOrder.end());
int k = 0;
grid[row][col] = message[k++];
decrypted += grid[row];
return decrypted;
#include <iostream>
#include <string>
#include <bitset>
#include <vector>
using namespace std;
return result;
int feedback = 0;
return to_string(feedback) + registerStr.substr(0, registerStr.size() - 1); // Shift left and add feedback
// A5/1 taps
vector<int> taps1 = {8, 10, 13, 16}; // Taps for 19-bit register
if (majority >= 2) {
if (majority <= 1) {
if (reg3[13] == '1') {
return keystream;
messageBin += bitset<8>(ch).to_string();
int main() {
if (key.length() != 64) {
return 1;
cin.ignore();
getline(cin, message);
cout << "Encrypted Message (in binary): " << encryptedMessage << endl;
decryptedMessage += character;
}
return 0;
RSA-1
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
while (b != 0) {
long long t = b;
b = a % b;
a = t;
return a;
}
// Function to compute modular inverse using extended Euclidean algorithm
long long m0 = m, t, q;
long long x0 = 0, x1 = 1;
if (m == 1) return 0;
while (a > 1) {
q = a / m;
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
return x1;
void generateRSAKeys(long long &n, long long &e, long long &d) {
long long p, q;
cin >> p;
cout << "Enter a prime number q: ";
cin >> q;
// Check if p and q are primes (this is just a basic check, for larger primes you would need more robust
methods)
if (p <= 1 || q <= 1) {
cout << "Both numbers must be prime and greater than 1. Exiting.\n";
exit(1);
n = p * q; // n = p * q
d = modInverse(e, phi);
return encryptedMessage;
return decryptedMessage;
int main() {
long long n, e, d;
generateRSAKeys(n, e, d);
cout << "Public Key (e, n): (" << e << ", " << n << ")\n";
cout << "Private Key (d, n): (" << d << ", " << n << ")\n";
cout << "Message must be less than the value of n (" << n << "). Exiting program.\n";
return 1;
return 0;
RSA-2
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
long long modExpo(long long base, long long exp, long long mod) {
if (exp % 2 == 1)
exp /= 2;
return result;
int t1 = 0, t2 = 1, r1 = phi, r2 = e;
int q = r1 / r2;
int t = t1 - q * t2;
t1 = t2;
t2 = t;
int r = r1 - q * r2;
r1 = r2;
r2 = r;
if (t1 < 0) {
t1 += phi;
return t1;
n = p * q;
do {
e = rand() % (phi - 2) + 2;
d = modInverse(e, phi);
int main() {
srand(time(0));
int n, e, d;
generateKeys(n, e, d, p, q);
std::cout << "Public Key: (" << e << ", " << n << ")" << std::endl;
std::cout << "Private Key: (" << d << ", " << n << ")" <<
std::endl;
return 0;