izukuboi

Untitled

Jul 7th, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <openssl/rsa.h>
  4. #include <openssl/aes.h>
  5. #include <openssl/des.h>
  6. #include <openssl/pem.h>
  7. #include <openssl/err.h>
  8.  
  9. std::string decryptRSA(const std::string& encryptedString, const std::string& privateKey)
  10. {
  11.     RSA* rsa = nullptr;
  12.     BIO* keyBio = BIO_new_mem_buf(privateKey.c_str(), -1);
  13.  
  14.     if (!keyBio)
  15.     {
  16.         std::cerr << "Error creating BIO object" << std::endl;
  17.         return "";
  18.     }
  19.  
  20.     rsa = PEM_read_bio_RSAPrivateKey(keyBio, &rsa, nullptr, nullptr);
  21.     if (!rsa)
  22.     {
  23.         std::cerr << "Error reading RSA private key" << std::endl;
  24.         ERR_print_errors_fp(stderr);
  25.         BIO_free(keyBio);
  26.         return "";
  27.     }
  28.  
  29.     int rsaSize = RSA_size(rsa);
  30.     std::vector<unsigned char> decrypted(rsaSize);
  31.     int decryptedSize = RSA_private_decrypt(encryptedString.size(), reinterpret_cast<const unsigned char*>(encryptedString.c_str()), decrypted.data(), rsa, RSA_PKCS1_PADDING);
  32.  
  33.     if (decryptedSize == -1)
  34.     {
  35.         std::cerr << "Error decrypting RSA message" << std::endl;
  36.         ERR_print_errors_fp(stderr);
  37.         BIO_free(keyBio);
  38.         RSA_free(rsa);
  39.         return "";
  40.     }
  41.  
  42.     BIO_free(keyBio);
  43.     RSA_free(rsa);
  44.  
  45.     return std::string(decrypted.begin(), decrypted.begin() + decryptedSize);
  46. }
  47.  
  48. std::string decryptAES(const std::string& encryptedString, const std::string& key)
  49. {
  50.     AES_KEY aesKey;
  51.     AES_set_decrypt_key(reinterpret_cast<const unsigned char*>(key.c_str()), 128, &aesKey);
  52.  
  53.     std::string decryptedString(encryptedString.size(), '\0');
  54.     AES_decrypt(reinterpret_cast<const unsigned char*>(encryptedString.c_str()), reinterpret_cast<unsigned char*>(decryptedString.data()), &aesKey);
  55.  
  56.     return decryptedString;
  57. }
  58.  
  59. std::string decryptTripleDES(const std::string& encryptedString, const std::string& key)
  60. {
  61.     DES_cblock desKey;
  62.     std::memcpy(desKey, key.c_str(), 8);
  63.  
  64.     DES_key_schedule desKeySchedule;
  65.     DES_set_key_unchecked(&desKey, &desKeySchedule);
  66.  
  67.     std::string decryptedString(encryptedString.size(), '\0');
  68.     DES_ecb_encrypt(reinterpret_cast<const unsigned char*>(encryptedString.c_str()), reinterpret_cast<unsigned char*>(decryptedString.data()), &desKeySchedule, DES_DECRYPT);
  69.  
  70.     return decryptedString;
  71. }
  72.  
  73. int main()
  74. {
  75.     std::string encryptedString;
  76.     std::string decryptionMethod;
  77.     std::string privateKey;
  78.     std::string key;
  79.  
  80.     std::cout << "Enter the encrypted string: ";
  81.     std::getline(std::cin, encryptedString);
  82.  
  83.     std::cout << "Enter the decryption method (RSA, AES, or Triple DES): ";
  84.     std::getline(std::cin, decryptionMethod);
  85.  
  86.     if (decryptionMethod == "RSA")
  87.     {
  88.         std::cout << "Enter the private key: ";
  89.         std::getline(std::cin, privateKey);
  90.  
  91.         std::string decryptedString = decryptRSA(encryptedString, privateKey);
  92.         std::cout << "Decrypted string: " << decryptedString << std::endl;
  93.     }
  94.     else if (decryptionMethod == "AES")
  95.     {
  96.         std::cout << "Enter the AES key: ";
  97.         std::getline(std::cin, key);
  98.  
  99.         std::string decryptedString = decryptAES(encryptedString, key);
  100.         std::cout << "Decrypted string: " << decryptedString << std::endl;
  101.     }
  102.     else if (decryptionMethod == "Triple DES")
  103.     {
  104.         std::cout << "Enter the Triple DES key: ";
  105.         std::getline(std::cin, key);
  106.  
  107.         std::string decryptedString = decryptTripleDES(encryptedString, key);
  108.         std::cout << "Decrypted string: " << decryptedString << std::endl;
  109.     }
  110.     else
  111.     {
  112.         std::cerr << "Invalid decryption method entered!" << std::endl;
  113.     }
  114.  
  115.     return 0;
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment