RSA加密解密代码

RSA加密解密代码(Ubuntu下)

下面是一个用C++和OpenSSL库在Ubuntu下进行RSA加密和解密的验证代码示例。你可以用它来测试RSA加密和解密的过程。

安装OpenSSL库(如果尚未安装):

sudo apt-get install libssl-dev

RSA加密和解密代码(rsa_example.cpp):

#include

#include <openssl/rsa.h>

#include <openssl/pem.h>

#include <openssl/err.h>

#include

using namespace std;

// 打印错误信息

void print_errors() {

char buffer120;

while (ERR_get_error()) {

ERR_error_string_n(ERR_get_error(), buffer, sizeof(buffer));

cout << buffer << endl;

}

}

// 生成RSA密钥对并保存

void generate_rsa_keys() {

RSA* rsa = RSA_new();

BIGNUM* bn = BN_new();

if (!BN_set_word(bn, RSA_F4)) {

print_errors();

return;

}

复制代码
if (!RSA_generate_key_ex(rsa, 2048, bn, NULL)) {
    print_errors();
    return;
}

// 保存私钥
FILE* privateKeyFile = fopen("private.pem", "wb");
PEM_write_RSAPrivateKey(privateKeyFile, rsa, NULL, NULL, 0, NULL, NULL);
fclose(privateKeyFile);

// 保存公钥
FILE* publicKeyFile = fopen("public.pem", "wb");
PEM_write_RSAPublicKey(publicKeyFile, rsa);
fclose(publicKeyFile);

RSA_free(rsa);
BN_free(bn);

}

// 使用RSA公钥加密

int encrypt_with_rsa_public_key(const string& message, unsigned char** encryptedMessage) {

FILE* publicKeyFile = fopen("public.pem", "rb");

RSA* rsa = PEM_read_RSAPublicKey(publicKeyFile, NULL, NULL, NULL);

fclose(publicKeyFile);

复制代码
int rsaSize = RSA_size(rsa);
*encryptedMessage = (unsigned char*)malloc(rsaSize);

int result = RSA_public_encrypt(message.length(), (unsigned char*)message.c_str(), *encryptedMessage, rsa, RSA_PKCS1_OAEP_PADDING);
if (result == -1) {
    print_errors();
    RSA_free(rsa);
    return -1;
}

RSA_free(rsa);
return result;

}

// 使用RSA私钥解密

string decrypt_with_rsa_private_key(unsigned char* encryptedMessage, int encryptedMessageLen) {

FILE* privateKeyFile = fopen("private.pem", "rb");

RSA* rsa = PEM_read_RSAPrivateKey(privateKeyFile, NULL, NULL, NULL);

fclose(privateKeyFile);

复制代码
unsigned char* decryptedMessage = (unsigned char*)malloc(RSA_size(rsa));
int result = RSA_private_decrypt(encryptedMessageLen, encryptedMessage, decryptedMessage, rsa, RSA_PKCS1_OAEP_PADDING);
if (result == -1) {
    print_errors();
    RSA_free(rsa);
    return "";
}

string decryptedText((char*)decryptedMessage, result);
RSA_free(rsa);
free(decryptedMessage);

return decryptedText;

}

int main() {

// 生成密钥对

generate_rsa_keys();

复制代码
string message = "Hello, RSA Encryption!";
cout << "Original Message: " << message << endl;

// 使用RSA公钥加密
unsigned char* encryptedMessage = NULL;
int encryptedMessageLen = encrypt_with_rsa_public_key(message, &encryptedMessage);
if (encryptedMessageLen == -1) {
    return 1;
}

// 打印加密后的数据
cout << "Encrypted Message: ";
for (int i = 0; i < encryptedMessageLen; i++) {
    printf("%02x ", encryptedMessage[i]);
}
cout << endl;

// 使用RSA私钥解密
string decryptedMessage = decrypt_with_rsa_private_key(encryptedMessage, encryptedMessageLen);
cout << "Decrypted Message: " << decryptedMessage << endl;

free(encryptedMessage);
return 0;

}

代码解释:

生成RSA密钥对:generate_rsa_keys函数会生成2048位的RSA密钥对,并将它们分别保存为private.pem和public.pem。

RSA公钥加密:encrypt_with_rsa_public_key函数使用公钥对消息进行加密。

RSA私钥解密:decrypt_with_rsa_private_key函数使用私钥解密加密后的消息。

加密后输出:加密后的数据以十六进制格式输出,便于查看。

编译与运行:

编译代码:

g++ -o rsa_example rsa_example.cpp -lssl -lcrypto

运行:

./rsa_example

输出示例:

相关推荐
隐积1 小时前
3.1.7.Qt容器大家族(3):QVariant——万能盒子
开发语言·qt
Tri_Function4 小时前
动态规划DP1(c++)
c++
名字还没想好☜4 小时前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
初级代码游戏5 小时前
iOS开发 Swift 速记1:变量和基本数据类型
开发语言·ios·swift
酷在前行6 小时前
【R生态】PERMANOVA 进阶实战:距离选择、PERMDISP、两两比较与受限置换(保姆级教程)
开发语言·r语言
cxr8286 小时前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱
清水迎朝阳6 小时前
客户端软件 — 用户统计方案
服务器·c++·客户端·用户统计
废弃的小码农7 小时前
功能测试--Day07--Python编程基础
开发语言·python
再卷也是菜8 小时前
C++17(下)
c++
fengci.8 小时前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php