Posts

lab12(euler)

 #include <iostream>  #include <cmath>  using namespace std;  long long gcd(long long a, long long b) {      if (b == 0)          return a;      return gcd(b, a % b);  }  long long eulerTotient(long long n) {      long long result = n;      for (long long i = 2; i * i <= n; i++) {          if (n % i == 0) {              while (n % i == 0)                  n /= i;              result -= result / i;          }      }      if (n > 1)          result -= result / n;      return result;  }  long long modExp(long long base, long long exp, long long mod) {      long long result = 1;      base =...

lab12(fermat)

 #include <iostream>  #include <cmath>  using namespace std;  long long modExp(long long base, long long exp, long long mod) {      long long result = 1;      base = base % mod;      while (exp > 0) {          if (exp % 2 == 1)              result = (result * base) % mod;          base = (base * base) % mod;          exp /= 2;      }      return result;  }  void fermatTheoremDemo() {      long long a, p;      cout << "Enter a prime number (p): ";      cin >> 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...

lab11

#include <iostream> #include <cmath> #include <string> using namespace std; long long modExp(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; } string xorEncryptDecrypt(const string &text, long long sharedKey) {     string result = text;     for (size_t i = 0; i < text.size(); i++) {         result[i] = text[i] ^ (sharedKey % 256);     }     return result; } int main() {     long long p, g, a, b;     cout << "Enter a large prime number (p): ";     cin >> p;     cout << "Enter a primitive root modulo p (g): ";     ...

lab10

#include <iostream> #include <cmath> using namespace std; int gcd(int a, int b) {     if (b == 0)         return a;     return gcd(b, a % b); } long long modExp(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; } long long modInverse(long long e, long long phi) {     long long m0 = phi, t, q;     long long x0 = 0, x1 = 1;     if (phi == 1)         return 0;     while (e > 1) {         q = e / phi;         t = phi;         phi = e % phi;         e = t;       ...

lab9

#include <iostream> #include <cmath> using namespace std; // Function to compute GCD (Greatest Common Divisor) int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // Blum Blum Shub Random Number Generator void blumBlumShub(int seed, int p, int q, int numRandomNumbers) { int M = p * q; // Compute modulus (M) int x = seed % M; // Ensure seed is within range cout << "Generated Pseudo-random numbers: \n"; // Generate 'numRandomNumbers' random numbers for (int i = 0; i < numRandomNumbers; i++) { x = (x * x) % M; // BBS formula cout << x << " "; // Output random number } cout << endl; } // Driver function int main() { int seed, numRandomNumbers; // Two large prime numbers p and q (must be congruent to 3 mod 4) int p = 499; // Prime 1 int q = 547; // Prime 2 // Ensure p and q are congruent to 3 mod 4 if (p % 4 != 3 || q % 4 != 3) { cout << "Error: p and q must be prime ...

lab7

#include <iostream> #include <string> using namespace std; string xorOperation(const string &text, const string &key) {     string result = text;     for (size_t i = 0; i < text.size(); i++) {         result[i] = text[i] ^ key[i % key.size()];     }     return result; } string encryptDES(const string &plainText, const string &key) {     return xorOperation(plainText, key); } string decryptDES(const string &encryptedText, const string &key) {     return xorOperation(encryptedText, key); } int main() {     string plainText, key;     cout << "Enter a plain text sentence or paragraph: ";     getline(cin, plainText);     cout << "Enter a key (word or sentence): ";     getline(cin, key);     string encryptedText = encryptDES(plainText, key);     cout << "Encrypted Message: ";    ...

lab6b

#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; vector<int> getPermutationOrder(string key) {     vector<pair<char, int>> keyOrder;     vector<int> order(key.length());     for (int i = 0; i < key.length(); i++) {         keyOrder.push_back(make_pair(key[i], i));     }     sort(keyOrder.begin(), keyOrder.end());     for (int i = 0; i < keyOrder.size(); i++) {         order[keyOrder[i].second] = i;     }     return order; } string preprocessText(string text) {     for (char &ch : text) {         if (ch == ' ') {             ch = '~';         }     }     return text; } string restoreSpaces(string text) {     for (char &ch : text) {       ...