Created
July 19, 2024 14:23
-
-
Save Divide-By-0/6edfdaafcdbbfeb038913743524258a9 to your computer and use it in GitHub Desktop.
RSA-SHA256 in C++ Example for Ligetron
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <openssl/rsa.h> | |
#include <openssl/pem.h> | |
#include <openssl/rand.h> | |
#include "sha256.hpp" | |
#include <string> | |
#include <iostream> | |
#include <cstring> | |
#include <vector> | |
#include <random> | |
/* APPLICATION DESCRIPTION: | |
Demonstrates RSA encryption/decryption and SHA256 hashing with key generation | |
and random message generation. | |
*/ | |
#define RSA_KEY_SIZE 2048 | |
#define MESSAGE_LENGTH 64 | |
std::vector<uint8_t> sha256(const std::string& str) { | |
std::vector<uint8_t> hash(32); | |
lonesha256(hash.data(), reinterpret_cast<const uint8_t*>(str.c_str()), str.length()); | |
return hash; | |
} | |
std::string bin2hex(const std::vector<uint8_t>& bin) { | |
std::string hex; | |
for (uint8_t b : bin) { | |
char hex_bytes[3]; | |
sprintf(hex_bytes, "%02x", b); | |
hex += hex_bytes; | |
} | |
return hex; | |
} | |
std::string generate_random_message(size_t length) { | |
const char charset[] = "0123456789" | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
"abcdefghijklmnopqrstuvwxyz"; | |
std::random_device rd; | |
std::mt19937 gen(rd()); | |
std::uniform_int_distribution<> dis(0, sizeof(charset) - 2); | |
std::string message; | |
message.reserve(length); | |
for (size_t i = 0; i < length; ++i) { | |
message += charset[dis(gen)]; | |
} | |
return message; | |
} | |
int main() { | |
// Generate random message | |
std::string message = generate_random_message(MESSAGE_LENGTH); | |
// Generate RSA key pair | |
RSA* rsa = RSA_new(); | |
BIGNUM* bne = BN_new(); | |
BN_set_word(bne, RSA_F4); | |
RSA_generate_key_ex(rsa, RSA_KEY_SIZE, bne, NULL); | |
// Encrypt message | |
std::vector<uint8_t> encrypted(RSA_size(rsa)); | |
int encrypted_length = RSA_public_encrypt(message.length(), | |
reinterpret_cast<const uint8_t*>(message.c_str()), | |
encrypted.data(), | |
rsa, RSA_PKCS1_PADDING); | |
if (encrypted_length == -1) { | |
std::cerr << "Encryption failed" << std::endl; | |
RSA_free(rsa); | |
BN_free(bne); | |
return 1; | |
} | |
// Decrypt message | |
std::vector<uint8_t> decrypted(RSA_size(rsa)); | |
int decrypted_length = RSA_private_decrypt(encrypted_length, | |
encrypted.data(), | |
decrypted.data(), | |
rsa, RSA_PKCS1_PADDING); | |
if (decrypted_length == -1) { | |
std::cerr << "Decryption failed" << std::endl; | |
RSA_free(rsa); | |
BN_free(bne); | |
return 1; | |
} | |
decrypted.resize(decrypted_length); | |
// Calculate SHA256 hash of the original message | |
std::vector<uint8_t> hash = sha256(message); | |
// Print results | |
std::cout << "Original message: " << message << std::endl; | |
std::cout << "Decrypted message: " << std::string(decrypted.begin(), decrypted.end()) << std::endl; | |
std::cout << "SHA256 hash: " << bin2hex(hash) << std::endl; | |
RSA_free(rsa); | |
BN_free(bne); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment