#include <iostream>
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/spawn.hpp>
#include <json.hpp>
#include <boost/filesystem.hpp>
#include <fstream>
#include <cstdlib>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/pem.h>
#include <openssl/kdf.h>
/**
*三方库
* boost_1_84_0
* Win64OpenSSL-3_2_0
*
*
* 生成私钥 openssl genpkey -algorithm RSA -out private_key.pem -aes256 -pass pass:
* 生成公钥 openssl rsa -pubout -in private_key.pem -out public_key.pem
**/
namespace asio = boost::asio;
namespace beast = boost::beast;
namespace websocket = beast::websocket;
using tcp = asio::ip::tcp;
using json = nlohmann::json;
std::string rsa_encrypt(const std::string& plaintext, EVP_PKEY* public_key) {
OpenSSL_add_all_algorithms();
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
// 使用 RSA 加密算法
EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new(public_key, nullptr);
EVP_PKEY_encrypt_init(pctx);
// 获取加密输出长度
size_t encrypted_length;
EVP_PKEY_encrypt(pctx, nullptr, &encrypted_length, reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length());
std::string encrypted(encrypted_length, '\0');
// 执行加密
EVP_PKEY_encrypt(pctx, reinterpret_cast<unsigned char*>(&encrypted[0]), &encrypted_length, reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length());
// 释放资源
EVP_PKEY_CTX_free(pctx);
EVP_CIPHER_CTX_free(ctx);
// 返回加密后的结果
return encrypted;
}
std::string rsa_decrypt(const std::string& ciphertext, EVP_PKEY* private_key) {
OpenSSL_add_all_algorithms();
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
// 使用 RSA 解密算法
EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new(private_key, nullptr);
EVP_PKEY_decrypt_init(pctx);
// 获取解密输出长度
size_t decrypted_length;
EVP_PKEY_decrypt(pctx, nullptr, &decrypted_length, reinterpret_cast<const unsigned char*>(ciphertext.c_str()), ciphertext.length());
std::string decrypted(decrypted_length, '\0');
// 执行解密
EVP_PKEY_decrypt(pctx, reinterpret_cast<unsigned char*>(&decrypted[0]), &decrypted_length, reinterpret_cast<const unsigned char*>(ciphertext.c_str()), ciphertext.length());
// 释放资源
EVP_PKEY_CTX_free(pctx);
EVP_CIPHER_CTX_free(ctx);
// 返回解密后的结果
return decrypted;
}
// 从字符串加载 RSA 公钥
EVP_PKEY* load_public_key_from_string(const std::string& public_key_str) {
BIO* bio = BIO_new_mem_buf(public_key_str.c_str(), public_key_str.length());
EVP_PKEY* public_key = PEM_read_bio_PUBKEY(bio, nullptr, nullptr, nullptr);
BIO_free(bio);
return public_key;
}
// 从字符串加载 RSA 私钥
EVP_PKEY* load_private_key_from_string(const std::string& private_key_str) {
BIO* bio = BIO_new_mem_buf(private_key_str.c_str(), -1);
EVP_PKEY* private_key = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, (void*)"");
BIO_free(bio);
return private_key;
}
void on_connect(const boost::system::error_code& ec,
tcp::socket socket,
websocket::stream<tcp::socket> ws,
UCHAR pIndex,
asio::yield_context yield) {
if (ec) {
std::cerr << "Connect error: " << ec.message() << std::endl;
return;
}
try {
// Perform WebSocket handshake
ws.async_handshake("192.168.122.1", "/websocket", yield);
while (1) {
json vJson;
json vParam;
vParam["index"] = std::to_string(pIndex);
vJson["c"] = "ReqUser";
vJson["a"] = "getEncode";
vJson["p"] = vParam;
std::string sendStr = vJson.dump();
// Send a message
ws.async_write(asio::buffer(sendStr), yield);
// Receive a message
beast::flat_buffer buffer;
ws.async_read(buffer, yield);
std::cout << "index:" << std::to_string(pIndex) << ", Received: " << beast::make_printable(buffer.data()) << std::endl;
}
// Close the WebSocket connection
ws.async_close(websocket::close_code::normal, yield);
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
int main() {
/*asio::io_context io_context;
tcp::resolver resolver(io_context);
auto endpoints = resolver.resolve("192.168.122.1", "8080");
for (UCHAR i = 0; i < 3; i++) {
asio::spawn(io_context, [&, endpoints, i](asio::yield_context yield) {
UCHAR vIndex = i;
tcp::socket socket(io_context);
websocket::stream<tcp::socket> ws(std::move(socket));
boost::system::error_code ec;
asio::async_connect(ws.next_layer(), endpoints, yield[ec]);
asio::spawn(io_context, [&](asio::yield_context yield) {
on_connect(ec, std::move(socket), std::move(ws), vIndex, yield);
});
});
}
io_context.run();*/
/*EVP_PKEY* public_key = load_public_key_from_string("-----BEGIN PUBLIC KEY-----\n"
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA30H4zHRoqCNTW0XBG9SO\n"
"lWX5woPi82nix + h2o2b9H15msUbF2 / HN5h6WjEyeZbv7HvVJw77BYxyiZeQzYcVl\n"
"d4JH / lElkBtkuMLLdwBeSxwvgsQvlrxMh1ruv8CePtXayZ7nFkto76E9CX6xR9kV\n"
"Tm + EdDta45SG / IBJmxE1hoX9dbX + ns + 5bigmeS9C / NZIznpr + OGygCf81apAey8f\n"
"19bJAbZYTjHA2f365Dgtspxm6Slg4US8WK1R5saVyRJ6UP2IGbsQEsTCjGkgV7c4\n"
"s9 + mSN2c3G8rxyDuH9RWkCDQspkubonBKiG49GTIYiCFXqwXzKpbUIY1y01djbht\n"
"TQIDAQAB\n"
"---- - END PUBLIC KEY---- -\n");*/
// 明文和密钥
// 文件路径
std::ifstream publicStream("c:\\public_key.pem");
std::ifstream privateStream("c:\\private_key.pem");
// 读取文件内容
std::string publicContent((std::istreambuf_iterator<char>(publicStream)), std::istreambuf_iterator<char>());
std::string privateContent((std::istreambuf_iterator<char>(privateStream)), std::istreambuf_iterator<char>());
std::string plaintext = "Hello, OpenSSL!";
EVP_PKEY* public_key = load_public_key_from_string(publicContent);
EVP_PKEY* private_key = load_private_key_from_string(privateContent);
// 加密
std::string ciphertext = rsa_encrypt(plaintext, public_key);
std::cout << "Encrypted: " << ciphertext << std::endl;
// 解密
std::string decrypted = rsa_decrypt(ciphertext, private_key);
std::cout << "Decrypted: " << decrypted << std::endl;
return 0;
}