Compare commits

..

5 Commits

Author SHA1 Message Date
justuser-31 fd9fee572e Update README.md 2025-07-26 14:42:15 +00:00
justuser-31 a427f71428 Update README.md 2025-07-26 13:42:38 +00:00
justuser-31 fa4d961574 Outside declaration of botan path 2025-07-26 16:37:33 +03:00
justuser-31 f3743942ee init 2025-07-26 16:21:32 +03:00
justuser-31 1ade51f4ef Initial commit 2025-07-26 12:33:44 +00:00
2 changed files with 27 additions and 29 deletions
View File
+27 -29
View File
@@ -1,37 +1,35 @@
#include "crypto_lib.hpp"
#include "crypto_lib.cpp"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
try {
cout << "=== Asymmetric Cryptography Example ===" << endl;
string generate_new_keys;
cout << "Generate new keys? (y/n): ";
cin >> generate_new_keys;
std::cout << "=== Asymmetric Cryptography Example ===" << std::endl;
std::string generate_new_keys;
std::cout << "Generate new keys? (y/n): ";
std::cin >> generate_new_keys;
string message;
vector<uint8_t> encrypted;
std::string message;
std::vector<uint8_t> encrypted;
if (generate_new_keys == "y") {
// 1. Generate keys
cout << "1. Generating key pair...\n\n";
std::cout << "1. Generating key pair...\n\n";
auto keys = CryptoLib::generate_keys(2048);
auto& private_key = keys.first;
auto& public_key = keys.second;
// 2. Save keys to files
cout << "2. Saving keys to files...\n\n";
std::cout << "2. Saving keys to files...\n\n";
CryptoLib::save_private_key(*private_key, "private_key.pem");
CryptoLib::save_public_key(*public_key, "public_key.pem");
message = "Hello, this is a secret message!";
} else {
cout << "Enter message to decrypt (if exists): ";
cin.ignore();
getline(cin, message);
std::cout << "Enter message to decrypt (if exists): ";
std::cin.ignore();
std::getline(std::cin, message);
if (!message.empty()) {
encrypted = CryptoLib::base64_to_bytes(message);
@@ -41,35 +39,35 @@ int main() {
}
// 3. Load keys from files
cout << "3. Loading keys from files...\n\n";
std::cout << "3. Loading keys from files...\n\n";
auto loaded_private_key = CryptoLib::load_private_key("private_key.pem");
auto loaded_public_key = CryptoLib::load_public_key("public_key.pem");
// 4. Convert public key to string and back
cout << "4. Loaded keys:\n";
string private_key_str = CryptoLib::private_key_to_string(*loaded_private_key);
string public_key_str = CryptoLib::public_key_to_string(*loaded_public_key);
cout << "Private key string as: " << private_key_str.substr(0, 50) << "...\n";
cout << "Public key string as: " << public_key_str.substr(0, 50) << "...\n\n";
std::cout << "4. Loaded keys:\n";
std::string private_key_str = CryptoLib::private_key_to_string(*loaded_private_key);
std::string public_key_str = CryptoLib::public_key_to_string(*loaded_public_key);
std::cout << "Private key string as: " << private_key_str.substr(0, 50) << "...\n";
std::cout << "Public key string as: " << public_key_str.substr(0, 50) << "...\n\n";
cout << "5. Convert public key from string...\n\n";
std::cout << "5. Convert public key from string...\n\n";
auto restored_public_key = CryptoLib::public_key_from_string(public_key_str);
if (encrypted.empty()) {
// 6. Encrypt data
cout << "6. Testing ecnryption..." << endl;
cout << "Original message: " << message << endl;
std::cout << "6. Testing ecnryption..." << std::endl;
std::cout << "Original message: " << message << std::endl;
encrypted = CryptoLib::encrypt(*loaded_public_key, message);
cout << "Encrypted (base64): " << CryptoLib::bytes_to_base64(encrypted) << endl;
cout << "Encrypted data size: " << encrypted.size() << " bytes\n\n";
std::cout << "Encrypted (base64): " << CryptoLib::bytes_to_base64(encrypted) << std::endl;
std::cout << "Encrypted data size: " << encrypted.size() << " bytes\n\n";
}
// 7. Decrypt data
string decrypted_message = CryptoLib::decrypt(*loaded_private_key, encrypted);
cout << "7. Decrypted message: " << decrypted_message << "\n";
std::string decrypted_message = CryptoLib::decrypt(*loaded_private_key, encrypted);
std::cout << "7. Decrypted message: " << decrypted_message << "\n";
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}