Posts

lab12

Fermats Theorem – Program #include <iostream> #include <cmath> using namespace std; // Function to perform modular exponentiation (a^exp % mod) long long modExp(long long base, long long exp, long long mod) { long long result = 1; base = base % mod; // Reduce base if it is larger than mod while (exp > 0) { if (exp % 2 == 1) // If exponent is odd result = (result * base) % mod; base = (base * base) % mod; // Square base exp /= 2; // Divide exponent by 2 } return result; } // Function to demonstrate Fermat's Little Theorem void fermatTheoremDemo() { long long a, p; // Input prime number p cout << "Enter a prime number (p): "; cin >> p; // Input integer a (not divisible by p) cout << "Enter an integer a (a should not be divisible by p): "; cin >> a; if (a % p == 0) { cout << "Error: 'a' should not be divisible by 'p'." << endl; return; } // Compute a^(p-1) % p ...

lab10

 #include<iostream> #include<cmath> using namespace std; int gcd(int a, int b) {     return b == 0 ? a : gcd(b, a % b); } long long modExp(long long b, long long e, long long m) {     long long r = 1;     while (e) {         if (e % 2) r = (r * b) % m;         b = (b * b) % m;         e /= 2;     }     return r; } long long modInv(long long e, long long phi) {     long long m0 = phi, x0 = 0, x1 = 1;     while (e > 1) {         long long q = e / phi, t = phi;         phi = e % phi, e = t;         t = x0, x0 = x1 - q * x0, x1 = t;     }     return x1 < 0 ? x1 + m0 : x1; } void genKeys(long long &n, long long &e, long long &d) {     long long p = 61, q = 53, phi;     n = p * q, phi = (p - 1) * (q - 1);     e = 17; ...

lab9

 #include<iostream> using namespace std; int g(int a, int b) {     return b == 0 ? a : g(b, a % b); } void bbs(int s, int p, int q, int n) {     int m = p * q, x = s % m;     while (n--) {         x = (x * x) % m;         cout << x << " ";     }     cout << endl; } int main() {     int s, n, p = 499, q = 547, m = p * q;     if (p % 4 != 3 || q % 4 != 3) {         cout << "p and q must be ≡ 3 mod 4\n";         return 1;     }     cout << "Enter seed (coprime with " << m << "): ";     cin >> s;     if (g(s, m) != 1) {         cout << "Seed must be coprime with " << m << endl;         return 1;     }     cout << "Enter count: ";     cin >> n; ...

lab8

 #include <iostream> #include <limits> using namespace std; void lcg(int s, int a, int c, int m, int n) {     unsigned int x = s;     for (int i = 0; i < n; i++) {         x = (a * x + c) % m;         cout << x << " ";     }     cout << endl; } int main() {     unsigned int s;     int n;     cin >> s >> n;     int a = 1664525, c = 1013904223;     unsigned int m = UINT_MAX;     lcg(s, a, c, m, n);     return 0; }

lab7

 #include <iostream> #include <string> using namespace std; string xorOp(const string &text, const string &key) {     string res = text;     for (size_t i = 0; i < text.size(); i++)         res[i] = text[i] ^ key[i % key.size()];     return res; } int main() {     string pt, key, enc, dec, inKey;     cout << "Enter text: ";     getline(cin, pt);     cout << "Enter key: ";     getline(cin, key);      enc = xorOp(pt, key);     cout << "Encrypted: ";     for (char c : enc) cout << hex << (int)(unsigned char)c << " ";     cout << "\nEnter key to decrypt: ";     getline(cin, inKey);      dec = xorOp(enc, inKey);     cout << (dec == pt ? "Decryption successful: " + dec : "Decryption failed!") << endl;     return 0; }

lab6

 #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; // Function to generate permutation order from the key vector<int> getPermutationOrder(string key) { vector<pair<char, int>> keyOrder; vector<int> order(key.length()); // Store characters of key along with their original positions for (int i = 0; i < key.length(); i++) { keyOrder.push_back(make_pair(key[i], i)); } // Sort key characters to determine order sort(keyOrder.begin(), keyOrder.end()); // Assign order mapping for (int i = 0; i < keyOrder.size(); i++) { order[keyOrder[i].second] = i; } return order; } // Function to replace spaces with '~' to preserve them string preprocessText(string text) { for (char &ch : text) { if (ch == ' ') { ch = '~'; // Replace space with '~' to maintain its position } } return text; } // Function to restore spaces after decryption strin...

lab11

 #include <iostream> #include <cmath> using namespace std; long long modExp(long long b, long long e, long long m) { long long r = 1; while (e > 0) { if (e % 2 == 1) r = (r * b) % m; b = (b * b) % m; e /= 2; } return r; int main() { long long p, g, a, b, A, B, sA, sB; cout << "Enter a prime number (p): "; cin >> p; cout << "Enter a primi ve root (g): "; cin >> g; cout << "User A, enter your private key (a): "; cin >> a; cout << "User B, enter your private key (b): "; cin >> b; A = modExp(g, a, p); B = modExp(g, b, p); cout << "Public Key formula: (g^Private Key) % p\n"; cout << "User A's public key = (" << g << "^" << a << ") % " << p << " = " << A << endl; cout << "User B's public key = (" << g << "^" <...

lab5

#include <iostream> #include <vector> #include <string> #include <cctype> using namespace std; int modInv(int a) { for (int x = 1; x < 26; ++x) if ((a * x) % 26 == 1) return x; return -1; } int det(vector<vector<int>>& m) { return ((m[0][0]*(m[1][1]*m[2][2]-m[1][2]*m[2][1]) - m[0][1]*(m[1][0]*m[2][2]-m[1][2]*m[2][0]) + m[0][2]*(m[1][0]*m[2][1]-m[1][1]*m[2][0])) % 26 + 26) % 26; } vector<vector<int>> inv(vector<vector<int>>& m) { int d = modInv(det(m)); vector<vector<int>> r(3, vector<int>(3)); r[0][0] = (m[1][1]*m[2][2] - m[1][2]*m[2][1]) * d % 26; r[0][1] = -(m[0][1]*m[2][2] - m[0][2]*m[2][1]) * d % 26; r[0][2] = (m[0][1]*m[1][2] - m[0][2]*m[1][1]) * d % 26; r[1][0] = -(m[1][0]*m[2][2] - m[1][2]*m[2][0]) * d % 26; r[1][1] = (m[0][0]*m[2][2] - m[0][2]*m[2][0]) * d % 26; r[1][2] = -(m[0][0]*m...

lab4

 #include <iostream> #include <vector> #include <unordered_set> #include <iomanip> #include <cctype> using namespace std; vector<vector<char>> buildMatrix(string key) { unordered_set<char> seen; string forma edKey, alpha = "abcdefghiklmnopqrstuvwxyz"; for (char c : key + alpha) { c = (c == 'j' ? 'i' : tolower(c)); if (isalpha(c) && seen.insert(c).second) forma edKey += c; } vector<vector<char>> mat(5, vector<char>(5)); for (int i = 0; i < 25; ++i) mat[i / 5][i % 5] = forma edKey[i]; cout << "\nKey Matrix:\n"; for (auto& row : mat) { for (char c : row) cout << setw(2) << c << ' '; cout << endl; } return mat; } pair<int, int> findPos(const vector<vector<char>>& mat, char c) { if (c == 'j') c = 'i'; for (int i ...

lab3

#include <iostream> #include <string> using namespace std; string transform(const string& txt, const string& key, bool encrypt) { string res = ""; for (size_t i = 0; i < txt.size(); ++i) { char t = txt[i]; if (isalpha(t)) { char base = isupper(t) ? 'A' : 'a'; char k = tolower(key[i % key.size()]) - 'a'; int shi = encrypt ? k : -k; res += (char)(((tolower(t) - 'a' + shi + 26) % 26) + base); } else { res += t; } } return res; } int main() { string txt, key,en; int opt; cout << "=== Polyalphabe c (Vigenère) Cipher ===\n"; cout << "Enter the text: "; getline(cin, txt); cout << "Enter the key (only le ers): "; getline(cin, key); for (char c : key) { if (!isalpha(c)) { cout << "Key...

lab2

 #include <iostream> #include <string> #include <unordered_map> #include <cctype> using namespace std; string encMono(const string& txt, const string& key) { string res = ""; unordered_map<char, char> mp; for (int i = 0; i < 26; i++) { mp['a' + i] = tolower(key[i]); mp['A' + i] = toupper(key[i]); } for (char ch : txt) res += isalpha(ch) ? mp[ch] : ch; return res; } string decMono(const string& txt, const string& key) { string res = ""; unordered_map<char, char> mp; for (int i = 0; i < 26; i++) { mp[tolower(key[i])] = 'a' + i; mp[toupper(key[i])] = 'A' + i; } for (char ch : txt) res += isalpha(ch) ? mp[ch] : ch; return res; } bool validKey(const string& key) { if (key.size() != 26) return false; bool seen[26] = {false}; for (char ch : k...

lab1

#include <iostream> #include <string> using namespace std; string caesarEncrypt(string text, int shi ) { string result = ""; for (char c : text) { if (isalpha(c)) { char base = isupper(c) ? 'A' : 'a'; result += (char)((c - base + shi ) % 26 + base); } else { result += c; } } return result; } string caesarDecrypt(string text, int shi ) { return caesarEncrypt(text, 26 - (shi % 26)); } int main() { string inputText; int shi ; int choice; string encrypted, decrypted; cout << "=== Caesar Cipher ===\n"; cout << "Enter the text: "; getline(cin, inputText); cout << "Enter the shi value: "; cin >> shi ; encrypted = caesarEncrypt(inputText, shi ); cout << "Encrypted Text: " << encrypted << endl; decrypted = caesarDec...