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 buffer[120];

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

输出示例:

相关推荐
Sun_gentle2 小时前
java.lang.RuntimeException: Could not load wrapper properties from ‘C:\Users\
java·开发语言·安卓
键盘鼓手苏苏2 小时前
Flutter for OpenHarmony:git 纯 Dart 实现的 Git 操作库(在应用内实现版本控制) 深度解析与鸿蒙适配指南
开发语言·git·flutter·华为·rust·自动化·harmonyos
橙露2 小时前
面向对象编程思想:Java 与 Python 的封装、继承与多态对比分析
java·开发语言·python
ShineWinsu2 小时前
对于C++:模版进阶的解析
开发语言·c++·面试·笔试·求职·进阶·模版
啊哈哈121383 小时前
Python基本语法复盘笔记1(输入输出/字符串/列表)
开发语言·笔记·python
qq_150841993 小时前
3天基于VS2026的C#编程入门及动态调用CH341DLLA64读写I2C从机
开发语言·c#
Tony Bai4 小时前
Go 1.26 :go mod init 默认行为的变化与 Go 版本管理的哲学思辨
开发语言·后端·golang
xyq20244 小时前
WebForms SortedList 深度解析
开发语言
Hx_Ma164 小时前
测试题(三)
java·开发语言·后端