Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <openssl/rsa.h>
- #include <openssl/aes.h>
- #include <openssl/des.h>
- #include <openssl/pem.h>
- #include <openssl/err.h>
- std::string decryptRSA(const std::string& encryptedString, const std::string& privateKey)
- {
- RSA* rsa = nullptr;
- BIO* keyBio = BIO_new_mem_buf(privateKey.c_str(), -1);
- if (!keyBio)
- {
- std::cerr << "Error creating BIO object" << std::endl;
- return "";
- }
- rsa = PEM_read_bio_RSAPrivateKey(keyBio, &rsa, nullptr, nullptr);
- if (!rsa)
- {
- std::cerr << "Error reading RSA private key" << std::endl;
- ERR_print_errors_fp(stderr);
- BIO_free(keyBio);
- return "";
- }
- int rsaSize = RSA_size(rsa);
- std::vector<unsigned char> decrypted(rsaSize);
- int decryptedSize = RSA_private_decrypt(encryptedString.size(), reinterpret_cast<const unsigned char*>(encryptedString.c_str()), decrypted.data(), rsa, RSA_PKCS1_PADDING);
- if (decryptedSize == -1)
- {
- std::cerr << "Error decrypting RSA message" << std::endl;
- ERR_print_errors_fp(stderr);
- BIO_free(keyBio);
- RSA_free(rsa);
- return "";
- }
- BIO_free(keyBio);
- RSA_free(rsa);
- return std::string(decrypted.begin(), decrypted.begin() + decryptedSize);
- }
- std::string decryptAES(const std::string& encryptedString, const std::string& key)
- {
- AES_KEY aesKey;
- AES_set_decrypt_key(reinterpret_cast<const unsigned char*>(key.c_str()), 128, &aesKey);
- std::string decryptedString(encryptedString.size(), '\0');
- AES_decrypt(reinterpret_cast<const unsigned char*>(encryptedString.c_str()), reinterpret_cast<unsigned char*>(decryptedString.data()), &aesKey);
- return decryptedString;
- }
- std::string decryptTripleDES(const std::string& encryptedString, const std::string& key)
- {
- DES_cblock desKey;
- std::memcpy(desKey, key.c_str(), 8);
- DES_key_schedule desKeySchedule;
- DES_set_key_unchecked(&desKey, &desKeySchedule);
- std::string decryptedString(encryptedString.size(), '\0');
- DES_ecb_encrypt(reinterpret_cast<const unsigned char*>(encryptedString.c_str()), reinterpret_cast<unsigned char*>(decryptedString.data()), &desKeySchedule, DES_DECRYPT);
- return decryptedString;
- }
- int main()
- {
- std::string encryptedString;
- std::string decryptionMethod;
- std::string privateKey;
- std::string key;
- std::cout << "Enter the encrypted string: ";
- std::getline(std::cin, encryptedString);
- std::cout << "Enter the decryption method (RSA, AES, or Triple DES): ";
- std::getline(std::cin, decryptionMethod);
- if (decryptionMethod == "RSA")
- {
- std::cout << "Enter the private key: ";
- std::getline(std::cin, privateKey);
- std::string decryptedString = decryptRSA(encryptedString, privateKey);
- std::cout << "Decrypted string: " << decryptedString << std::endl;
- }
- else if (decryptionMethod == "AES")
- {
- std::cout << "Enter the AES key: ";
- std::getline(std::cin, key);
- std::string decryptedString = decryptAES(encryptedString, key);
- std::cout << "Decrypted string: " << decryptedString << std::endl;
- }
- else if (decryptionMethod == "Triple DES")
- {
- std::cout << "Enter the Triple DES key: ";
- std::getline(std::cin, key);
- std::string decryptedString = decryptTripleDES(encryptedString, key);
- std::cout << "Decrypted string: " << decryptedString << std::endl;
- }
- else
- {
- std::cerr << "Invalid decryption method entered!" << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment