Compare commits

..

8 Commits

Author SHA1 Message Date
justuser-31 90774b5b55 Restructure 2026-06-11 13:18:17 +03:00
justuser-31 5ccb0d8a56 Merge branch 'main' of ssh://git.del.pw:2222/justuser-31/just_simple_cryptography 2026-06-11 13:18:17 +03:00
justuser-31 64c41acf53 Change code style 2026-06-11 13:18:17 +03:00
justuser-31 d0d4fb056c Update README.md 2026-06-11 13:18:17 +03:00
justuser-31 79d59f41ea Outside declaration of botan path 2026-06-11 13:18:17 +03:00
justuser-31 368f28edc2 Update README.md 2026-06-11 13:18:17 +03:00
justuser-31 ec049ecb74 init 2026-06-11 13:18:17 +03:00
justuser-31 0e96de724f Initial commit 2026-06-11 13:18:17 +03:00
2 changed files with 29 additions and 27 deletions
View File
+29 -27
View File
@@ -1,35 +1,37 @@
#include "crypto_lib.cpp"
#include "crypto_lib.hpp"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
try {
std::cout << "=== Asymmetric Cryptography Example ===" << std::endl;
std::string generate_new_keys;
std::cout << "Generate new keys? (y/n): ";
std::cin >> generate_new_keys;
cout << "=== Asymmetric Cryptography Example ===" << endl;
string generate_new_keys;
cout << "Generate new keys? (y/n): ";
cin >> generate_new_keys;
std::string message;
std::vector<uint8_t> encrypted;
string message;
vector<uint8_t> encrypted;
if (generate_new_keys == "y") {
// 1. Generate keys
std::cout << "1. Generating key pair...\n\n";
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
std::cout << "2. Saving keys to files...\n\n";
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 {
std::cout << "Enter message to decrypt (if exists): ";
std::cin.ignore();
std::getline(std::cin, message);
cout << "Enter message to decrypt (if exists): ";
cin.ignore();
getline(cin, message);
if (!message.empty()) {
encrypted = CryptoLib::base64_to_bytes(message);
@@ -39,35 +41,35 @@ int main() {
}
// 3. Load keys from files
std::cout << "3. Loading keys from files...\n\n";
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
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 << "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 << "5. Convert public key from string...\n\n";
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
std::cout << "6. Testing ecnryption..." << std::endl;
std::cout << "Original message: " << message << std::endl;
cout << "6. Testing ecnryption..." << endl;
cout << "Original message: " << message << endl;
encrypted = CryptoLib::encrypt(*loaded_public_key, message);
std::cout << "Encrypted (base64): " << CryptoLib::bytes_to_base64(encrypted) << std::endl;
std::cout << "Encrypted data size: " << encrypted.size() << " bytes\n\n";
cout << "Encrypted (base64): " << CryptoLib::bytes_to_base64(encrypted) << endl;
cout << "Encrypted data size: " << encrypted.size() << " bytes\n\n";
}
// 7. Decrypt data
std::string decrypted_message = CryptoLib::decrypt(*loaded_private_key, encrypted);
std::cout << "7. Decrypted message: " << decrypted_message << "\n";
string decrypted_message = CryptoLib::decrypt(*loaded_private_key, encrypted);
cout << "7. Decrypted message: " << decrypted_message << "\n";
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
return 1;
}