Advertisement
Tark_Wight

RSA.Cracker

May 4th, 2024
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. long long ModularExponentiation(long long base, long long exponent, long long modulus) {
  6.     long long result = 1;
  7.     while (exponent > 0) {
  8.         if (exponent % 2 == 1) {
  9.             result = (result * base) % modulus;
  10.         }
  11.         base = (base * base) % modulus;
  12.         exponent /= 2;
  13.     }
  14.     return result;
  15. }
  16.  
  17. int EulerTotientFunction(int number) {
  18.     int result = number;
  19.     if (number % 2 == 0) {
  20.         while (number % 2 == 0) {
  21.             number /= 2;
  22.         }
  23.         result /= 2;
  24.     }
  25.     int i = 3;
  26.     while (i * i <= number) {
  27.         if (number % i == 0) {
  28.             while (number % i == 0) {
  29.                 number /= i;
  30.             }
  31.             result /= i;
  32.             result *= (i - 1);
  33.         }
  34.         i += 2;
  35.     }
  36.     if (number > 1) {
  37.         result /= number;
  38.         result *= (number - 1);
  39.     }
  40.     return result;
  41. }
  42.  
  43. std::pair<int, int> GeneratePrivateKey() {
  44.     int privateKey = 0, modulus;
  45.     int publicKey;
  46.     std::cout << "Enter the public key: ";
  47.     std::cin >> publicKey >> modulus;
  48.     for (int i = 1; i < modulus; ++i) {
  49.         if ((i * publicKey) % EulerTotientFunction(modulus) == 1) {
  50.             privateKey = i;
  51.             std::cout << "Private key: " << privateKey << " " << modulus << std::endl;
  52.             break;
  53.         }
  54.     }
  55.  
  56.     return { privateKey, modulus };
  57. }
  58.  
  59. void DecodeMessage() {
  60.     std::vector<int> encryptedCode;
  61.     int number;
  62.     std::cout << "Enter the encrypted numbers: ";
  63.     while (std::cin >> number) {
  64.         encryptedCode.push_back(number);
  65.         if (std::cin.peek() == '\n')
  66.             break;
  67.     }
  68.  
  69.     auto [privateKey, modulus] = GeneratePrivateKey();
  70.  
  71.     std::string decryptedString;
  72.     decryptedString += char(96 + ModularExponentiation(encryptedCode[0], privateKey, modulus));
  73.     for (size_t i = 1; i < encryptedCode.size(); ++i) {
  74.         int currentDecoded = ModularExponentiation(encryptedCode[i], privateKey, modulus);
  75.         int difference = currentDecoded - encryptedCode[i - 1];
  76.         if (difference < 0) difference += modulus;
  77.         decryptedString += char(96 + difference);
  78.     }
  79.     std::cout << "Decrypted string: " << decryptedString << std::endl;
  80. }
  81.  
  82. void EncodeMessage() {
  83.     std::string message;
  84.     std::cout << "Enter your message (no spaces): ";
  85.     std::cin >> message;
  86.     int publicKey, modulus;
  87.     std::cout << "Enter the public key: ";
  88.     std::cin >> publicKey >> modulus;
  89.  
  90.     int lastEncodedValue = ModularExponentiation(message[0] - 96, publicKey, modulus);
  91.     std::string encryptedString = std::to_string(lastEncodedValue) + " ";
  92.     for (size_t i = 1; i < message.length(); ++i) {
  93.         lastEncodedValue = ModularExponentiation(message[i] - 96 + lastEncodedValue, publicKey, modulus);
  94.         encryptedString += std::to_string(lastEncodedValue) + " ";
  95.     }
  96.     std::cout << "Encrypted string: " << encryptedString << std::endl;
  97. }
  98.  
  99. int main() {
  100.     std::cout << "1) Decode\n2) Encode\n";
  101.     int choice;
  102.     std::cin >> choice;
  103.     if (choice == 1) {
  104.         DecodeMessage();
  105.     } else {
  106.         EncodeMessage();
  107.     }
  108.     return 0;
  109. }
  110.  
Tags: RSA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement