0% found this document useful (0 votes)
24 views22 pages

Information Network & Security

Uploaded by

selinasacc190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views22 pages

Information Network & Security

Uploaded by

selinasacc190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Additive, Multiplicative & Affine Ciphers

#include <iostream>
#include <string>
using namespace std;

string additiveCipherEncrypt(string message, int key) {


string encrypted_message = "";
for (char& c : message) {
if (isalpha(c)) {
char shift = isupper(c) ? 'A' : 'a';
c = ((c - shift + key) % 26 + 26) % 26 + shift;
}
encrypted_message += c;
}
return encrypted_message;
}

string additiveCipherDecrypt(string message, int key) {


return additiveCipherEncrypt(message, -key);
}

int multiplicativeInverse(int a, int m) {


a = a % m;
for (int x = 1; x < m; x++) {
if ((a * x) % m == 1) {
return x;
}
}
return 1;
}

string multiplicativeCipherEncrypt(string message, int key) {


string encrypted_message = "";
for (char& c : message) {
if (isalpha(c)) {
char shift = isupper(c) ? 'A' : 'a';
c = ((c - shift) * key % 26 + 26) % 26 + shift;
}
encrypted_message += c;
}
return encrypted_message;
}

string multiplicativeCipherDecrypt(string message, int key) {


int inv_key = multiplicativeInverse(key, 26);
string decrypted_message = "";
for (char& c : message) {
if (isalpha(c)) {
char shift = isupper(c) ? 'A' : 'a';
c = ((c - shift) * inv_key % 26 + 26) % 26 + shift;
}
decrypted_message += c;
}
return decrypted_message;
}

string affineCipherEncrypt(string message, int key1, int key2) {


string encrypted_message = "";
for (char& c : message) {
if (isalpha(c)) {
char shift = isupper(c) ? 'A' : 'a';
c = ((c - shift) * key1 + key2) % 26 + shift;
}
encrypted_message += c;
}
return encrypted_message;
}

string affineCipherDecrypt(string message, int key1, int key2) {


int inv_key1 = multiplicativeInverse(key1, 26);
string decrypted_message = "";
for (char& c : message) {
if (isalpha(c)) {
char shift = isupper(c) ? 'A' : 'a';
c = (inv_key1 * ((c - shift - key2 + 26) % 26)) % 26 + shift;
}
decrypted_message += c;
}
return decrypted_message;
}

void cipherMenu() {
int choice;
string message;
int key1, key2;

cout << "Cipher Menu:\n";


cout << "1. Additive Cipher Encrypt\n";
cout << "2. Additive Cipher Decrypt\n";
cout << "3. Multiplicative Cipher Encrypt\n";
cout << "4. Multiplicative Cipher Decrypt\n";
cout << "5. Affine Cipher Encrypt\n";
cout << "6. Affine Cipher Decrypt\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // Clear the newline character from the buffer

if (choice >= 1 && choice <= 6) {


cout << "Enter message: ";
getline(cin, message);
}

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;
}

Row & Column Transposition Cipher

#include <iostream>

#include <string>

#include <algorithm>

#include <vector>

using namespace std;

// Function prototypes

string encryptRowTransposition(string message, string key);

string decryptRowTransposition(string message, string key);

string encryptColumnTransposition(string message, string key);


string decryptColumnTransposition(string message, string key);

int main() {

int choice;

string message, key;

do {

cout << "===== Transposition Cipher Menu =====" << endl;

cout << "1. Row Transposition Encryption" << endl;

cout << "2. Row Transposition Decryption" << endl;

cout << "3. Column Transposition Encryption" << endl;

cout << "4. Column Transposition Decryption" << endl;

cout << "5. Exit" << endl;

cout << "Enter your choice: ";

cin >> choice;

if (choice >= 1 && choice <= 4) {

cout << "Enter message: ";

cin.ignore();

getline(cin, message);

cout << "Enter key: ";

getline(cin, key);

switch (choice) {
case 1:

cout << "Encrypted Message (Row Transposition): "

<< encryptRowTransposition(message, key) << endl;

break;

case 2:

cout << "Decrypted Message (Row Transposition): "

<< decryptRowTransposition(message, key) << endl;

break;

case 3:

cout << "Encrypted Message (Column Transposition): "

<< encryptColumnTransposition(message, key) << endl;

break;

case 4:

cout << "Decrypted Message (Column Transposition): "

<< decryptColumnTransposition(message, key) << endl;

break;

case 5:

cout << "Exiting..." << endl;

break;

default:

cout << "Invalid choice. Please try again." << endl;

} while (choice != 5);

return 0;
}

// Row Transposition Encryption

string encryptRowTransposition(string message, string key) {

int columns = key.length();

int rows = (message.length() + columns - 1) / columns;

message += string(rows * columns - message.length(), 'X');

string encrypted = "";

for (char col : key) {

int colIndex = col - '1';

for (int row = 0; row < rows; ++row) {

encrypted += message[row * columns + colIndex];

return encrypted;

// Row Transposition Decryption

string decryptRowTransposition(string message, string key) {

int columns = key.length();

int rows = message.length() / columns;

vector<string> grid(rows, string(columns, ' '));

int k = 0;
for (char col : key) {

int colIndex = col - '1';

for (int row = 0; row < rows; ++row) {

grid[row][colIndex] = message[k++];

string decrypted = "";

for (int row = 0; row < rows; ++row) {

decrypted += grid[row];

return decrypted;

// Column Transposition Encryption

string encryptColumnTransposition(string message, string key) {

int columns = key.length();

int rows = (message.length() + columns - 1) / columns;

message += string(rows * columns - message.length(), 'X');

vector<string> grid(rows, string(columns, ' '));

int k = 0;

for (int row = 0; row < rows; ++row) {

for (int col = 0; col < columns; ++col) {

grid[row][col] = message[k++];
}

string encrypted = "";

vector<pair<char, int>> keyOrder;

for (int i = 0; i < columns; ++i) {

keyOrder.push_back({key[i], i});

sort(keyOrder.begin(), keyOrder.end());

for (auto& [ch, col] : keyOrder) {

for (int row = 0; row < rows; ++row) {

encrypted += grid[row][col];

return encrypted;

// Column Transposition Decryption

string decryptColumnTransposition(string message, string key) {

int columns = key.length();

int rows = message.length() / columns;

vector<string> grid(rows, string(columns, ' '));

vector<pair<char, int>> keyOrder;


for (int i = 0; i < columns; ++i) {

keyOrder.push_back({key[i], i});

sort(keyOrder.begin(), keyOrder.end());

int k = 0;

for (auto& [ch, col] : keyOrder) {

for (int row = 0; row < rows; ++row) {

grid[row][col] = message[k++];

string decrypted = "";

for (int row = 0; row < rows; ++row) {

decrypted += grid[row];

return decrypted;

A5/1 stream Cipher

#include <iostream>

#include <string>

#include <bitset>

#include <vector>
using namespace std;

// Function to XOR two binary strings

string xorStrings(const string &a, const string &b) {

string result = "";

for (size_t i = 0; i < a.size(); i++) {

result += (a[i] == b[i]) ? '0' : '1';

return result;

// Function to perform a shift on a register based on feedback taps

string shiftRegister(const string &registerStr, const vector<int> &taps) {

int feedback = 0;

for (int tap : taps) {

feedback ^= (registerStr[tap] - '0'); // XOR the tapped positions

return to_string(feedback) + registerStr.substr(0, registerStr.size() - 1); // Shift left and add feedback

// Function to generate the keystream using A5/1 algorithm

string generateKeystream(const string &key) {

// Initialize the registers

string reg1 = key.substr(0, 19);

string reg2 = key.substr(19, 22);


string reg3 = key.substr(41, 23);

// A5/1 taps

vector<int> taps1 = {8, 10, 13, 16}; // Taps for 19-bit register

vector<int> taps2 = {10, 12, 13}; // Taps for 22-bit register

vector<int> taps3 = {10, 12, 13}; // Taps for 23-bit register

string keystream = "";

for (int i = 0; i < 100; i++) {

// Majority rule: count the number of 1's in positions

int majority = (reg1[8] - '0') + (reg2[10] - '0') + (reg3[10] - '0');

// Shift registers based on the majority rule

if (majority >= 2) {

reg1 = shiftRegister(reg1, taps1);

if (majority <= 1) {

reg2 = shiftRegister(reg2, taps2);

if (reg3[13] == '1') {

reg3 = shiftRegister(reg3, taps3);

// Generate keystream bit by XORing the last bits of each register


keystream += to_string((reg1[18] - '0') ^ (reg2[21] - '0') ^ (reg3[22] - '0'));

return keystream;

// Function to encrypt/decrypt a message using A5/1 algorithm

string encryptDecrypt(const string &message, const string &key) {

// Generate the keystream

string keystream = generateKeystream(key);

// Ensure the message is in binary format

string messageBin = "";

for (char ch : message) {

messageBin += bitset<8>(ch).to_string();

// XOR the message with the keystream

return xorStrings(messageBin, keystream);

int main() {

string key, message;

cout << "Enter a 64-bit binary key: ";


cin >> key;

// Ensure the key is 64 bits

if (key.length() != 64) {

cout << "Key must be 64 bits long." << endl;

return 1;

cout << "Enter the message: ";

cin.ignore();

getline(cin, message);

// Encrypt the message

string encryptedMessage = encryptDecrypt(message, key);

cout << "Encrypted Message (in binary): " << encryptedMessage << endl;

// Decrypt the message (same as encryption)

string decryptedMessageBin = encryptDecrypt(encryptedMessage, key);

// Convert decrypted binary message back to text

string decryptedMessage = "";

for (size_t i = 0; i < decryptedMessageBin.length(); i += 8) {

string byte = decryptedMessageBin.substr(i, 8);

char character = static_cast<char>(bitset<8>(byte).to_ulong());

decryptedMessage += character;
}

cout << "Decrypted Message: " << decryptedMessage << endl;

return 0;

RSA-1

#include <iostream>

#include <cmath>

#include <cstdlib>

#include <ctime>

using namespace std;

// Function to compute greatest common divisor (GCD)

long long gcd(long long a, long long b) {

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 modInverse(long long a, long long m) {

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;

if (x1 < 0) x1 += m0;

return x1;

// Function to generate RSA keys with user input for p and q

void generateRSAKeys(long long &n, long long &e, long long &d) {

long long p, q;

// User input for prime numbers

cout << "Enter a prime number p: ";

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

long long phi = (p - 1) * (q - 1); // phi = (p - 1) * (q - 1)

// Choose e such that 1 < e < phi and gcd(e, phi) = 1

e = 17; // e is commonly chosen as 17 in RSA (though it should be coprime with phi)

// Calculate d such that d * e ≡ 1 (mod phi)

d = modInverse(e, phi);

// Function to encrypt message using public key (e, n)

long long encrypt(long long message, long long e, long long n) {

long long encryptedMessage = 1;

// Encrypt: C = M^e mod n

for (long long i = 0; i < e; i++) {

encryptedMessage = (encryptedMessage * message) % n;


}

return encryptedMessage;

// Function to decrypt message using private key (d, n)

long long decrypt(long long encryptedMessage, long long d, long long n) {

long long decryptedMessage = 1;

// Decrypt: M = C^d mod n

for (long long i = 0; i < d; i++) {

decryptedMessage = (decryptedMessage * encryptedMessage) % n;

return decryptedMessage;

int main() {

long long n, e, d;

// Step 1: Generate RSA keys based on user input

generateRSAKeys(n, e, d);

cout << "Public Key (e, n): (" << e << ", " << n << ")\n";

cout << "Private Key (d, n): (" << d << ", " << n << ")\n";

// Step 2: Encrypt a message

long long message;

cout << "\nEnter a message (numeric value): ";


cin >> message;

if (message < 0 || message >= n) {

cout << "Message must be less than the value of n (" << n << "). Exiting program.\n";

return 1;

long long encryptedMessage = encrypt(message, e, n);

cout << "Encrypted Message: " << encryptedMessage << endl;

// Step 3: Decrypt the message

long long decryptedMessage = decrypt(encryptedMessage, d, n);

cout << "Decrypted Message: " << decryptedMessage << endl;

return 0;

RSA-2

#include <iostream>

#include <cmath>

#include <cstdlib>

#include <ctime>

int gcd(int a, int b) {

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) {

long long result = 1;

while (exp > 0) {

if (exp % 2 == 1)

result = (result * base) % mod;

base = (base * base) % mod;

exp /= 2;

return result;

int modInverse(int e, int phi) {

int t1 = 0, t2 = 1, r1 = phi, r2 = e;

while (r2 > 0) {

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;

void generateKeys(int &n, int &e, int &d, int p, int q) {

n = p * q;

int phi = (p - 1) * (q - 1);

do {

e = rand() % (phi - 2) + 2;

} while (gcd(e, phi) != 1);

d = modInverse(e, phi);

int main() {

srand(time(0));

int p = 61, q = 53;

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;

int plainText = 89;

std::cout << "\nOriginal Plaintext: " << plainText << std::endl;

long long cipherText = modExpo(plainText, e, n);

std::cout << "Ciphertext: " << cipherText << std::endl;

long long decryptedText = modExpo(cipherText, d, n);

std::cout << "Decrypted Text: " << decryptedText << std::endl;

long long signature = modExpo(plainText, d, n);

std::cout << "\nSignature: " << signature << std::endl;

long long verifiedText = modExpo(signature, e, n);

std::cout << "Verified Text: " << verifiedText << std::endl;

return 0;

You might also like