Compare commits
2 Commits
fd9fee572e
...
acf3aeebab
Author | SHA1 | Date | |
---|---|---|---|
acf3aeebab | |||
c1c8992994 |
54
example.cpp
54
example.cpp
@ -3,33 +3,35 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
try {
|
try {
|
||||||
std::cout << "=== Asymmetric Cryptography Example ===" << std::endl;
|
cout << "=== Asymmetric Cryptography Example ===" << endl;
|
||||||
std::string generate_new_keys;
|
string generate_new_keys;
|
||||||
std::cout << "Generate new keys? (y/n): ";
|
cout << "Generate new keys? (y/n): ";
|
||||||
std::cin >> generate_new_keys;
|
cin >> generate_new_keys;
|
||||||
|
|
||||||
std::string message;
|
string message;
|
||||||
std::vector<uint8_t> encrypted;
|
vector<uint8_t> encrypted;
|
||||||
|
|
||||||
if (generate_new_keys == "y") {
|
if (generate_new_keys == "y") {
|
||||||
// 1. Generate keys
|
// 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 keys = CryptoLib::generate_keys(2048);
|
||||||
auto& private_key = keys.first;
|
auto& private_key = keys.first;
|
||||||
auto& public_key = keys.second;
|
auto& public_key = keys.second;
|
||||||
|
|
||||||
// 2. Save keys to files
|
// 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_private_key(*private_key, "private_key.pem");
|
||||||
CryptoLib::save_public_key(*public_key, "public_key.pem");
|
CryptoLib::save_public_key(*public_key, "public_key.pem");
|
||||||
|
|
||||||
message = "Hello, this is a secret message!";
|
message = "Hello, this is a secret message!";
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Enter message to decrypt (if exists): ";
|
cout << "Enter message to decrypt (if exists): ";
|
||||||
std::cin.ignore();
|
cin.ignore();
|
||||||
std::getline(std::cin, message);
|
getline(cin, message);
|
||||||
|
|
||||||
if (!message.empty()) {
|
if (!message.empty()) {
|
||||||
encrypted = CryptoLib::base64_to_bytes(message);
|
encrypted = CryptoLib::base64_to_bytes(message);
|
||||||
@ -39,35 +41,35 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3. Load keys from files
|
// 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_private_key = CryptoLib::load_private_key("private_key.pem");
|
||||||
auto loaded_public_key = CryptoLib::load_public_key("public_key.pem");
|
auto loaded_public_key = CryptoLib::load_public_key("public_key.pem");
|
||||||
|
|
||||||
// 4. Convert public key to string and back
|
// 4. Convert public key to string and back
|
||||||
std::cout << "4. Loaded keys:\n";
|
cout << "4. Loaded keys:\n";
|
||||||
std::string private_key_str = CryptoLib::private_key_to_string(*loaded_private_key);
|
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);
|
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";
|
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 << "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);
|
auto restored_public_key = CryptoLib::public_key_from_string(public_key_str);
|
||||||
|
|
||||||
if (encrypted.empty()) {
|
if (encrypted.empty()) {
|
||||||
// 6. Encrypt data
|
// 6. Encrypt data
|
||||||
std::cout << "6. Testing ecnryption..." << std::endl;
|
cout << "6. Testing ecnryption..." << endl;
|
||||||
std::cout << "Original message: " << message << std::endl;
|
cout << "Original message: " << message << endl;
|
||||||
encrypted = CryptoLib::encrypt(*loaded_public_key, message);
|
encrypted = CryptoLib::encrypt(*loaded_public_key, message);
|
||||||
std::cout << "Encrypted (base64): " << CryptoLib::bytes_to_base64(encrypted) << std::endl;
|
cout << "Encrypted (base64): " << CryptoLib::bytes_to_base64(encrypted) << endl;
|
||||||
std::cout << "Encrypted data size: " << encrypted.size() << " bytes\n\n";
|
cout << "Encrypted data size: " << encrypted.size() << " bytes\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. Decrypt data
|
// 7. Decrypt data
|
||||||
std::string decrypted_message = CryptoLib::decrypt(*loaded_private_key, encrypted);
|
string decrypted_message = CryptoLib::decrypt(*loaded_private_key, encrypted);
|
||||||
std::cout << "7. Decrypted message: " << decrypted_message << "\n";
|
cout << "7. Decrypted message: " << decrypted_message << "\n";
|
||||||
|
|
||||||
} catch (const std::exception& e) {
|
} catch (const exception& e) {
|
||||||
std::cerr << "Error: " << e.what() << std::endl;
|
cerr << "Error: " << e.what() << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user