0x0 RSA介绍
RSA是非对称加解密算法,计算耗时,适用于调用次数少,数据量小的场景。比如密钥交换,MD5或Hash做RSA加密。
默认padding方式RSA_PKCS1_PADDING。
0x1 代码实现
h文件
class RsaCrypt{
public:
enum Mode{
PUBLIC = 0,
PRIVATE
};
public:
RsaCrypt();
~RsaCrypt();
public:
bool Init(Mode mode, const std::string& key);
bool Encrypt(const std::string& input, std::string& output);
bool Decrypt(const std::string& input, std::string& output);
private:
int rsa_size_;
bool initialized_;
Mode mode_;
BIO* bio_;
RSA* rsa_;
};
cpp文件
const static int kRsaPadding = RSA_PKCS1_PADDING;
RsaCrypt::RsaCrypt()
: initialized_(false), rsa_size_(0), mode_(RsaCrypt::PRIVATE), rsa_(NULL), bio_(NULL){
}
RsaCrypt::~RsaCrypt() {
if (bio_) {
BIO_free_all(bio_);
bio_ = NULL;
}
if (rsa_) {
RSA_free(rsa_);
rsa_ = NULL;
}
}
bool RsaCrypt::Encrypt(const std::string &input, std::string &output) {
if (!initialized_) {
LOGE("init not done");
return false;
}
bool result = false;
if (mode_ == RsaCrypt::PUBLIC) {
unsigned char* buf = new unsigned char[rsa_size_];
if (buf) {
int ret = RSA_public_encrypt(input.length(), (const unsigned char*)input.c_str(), buf, rsa_, kRsaPadding);
if (ret > 0) {
output = std::string((char*)buf,ret);
result = true;
} else {
LOGE("RSA_public_encrypt error");
}
delete[] buf;
}
} else {
unsigned char* buf = new unsigned char[rsa_size_];
if (buf) {
int ret = RSA_private_encrypt(input.length(), (const unsigned char*)input.c_str(), buf, rsa_, kRsaPadding);
if (ret > 0) {
output = std::string((char*)buf,ret);
result = true;
} else {
LOGE("RSA_public_encrypt error");
}
delete[] buf;
}
}
return result;
}
bool RsaCrypt::Decrypt(const std::string &input, std::string &output) {
if (!initialized_) {
LOGE("init not done");
return false;
}
bool result = false;
if (mode_ == RsaCrypt::PUBLIC) {
unsigned char* buf = new unsigned char[rsa_size_];
if (buf) {
int ret = RSA_public_decrypt(input.length(), (const unsigned char*)input.c_str(), buf, rsa_, kRsaPadding);
if (ret > 0) {
output = std::string((char*)buf,ret);
result = true;
} else {
LOGE("RSA_public_decrypt error");
}
delete[] buf;
}
} else {
unsigned char* buf = new unsigned char[rsa_size_];
if (buf) {
int ret = RSA_private_decrypt(input.length(), (const unsigned char*)input.c_str(), buf, rsa_, kRsaPadding);
if (ret > 0) {
output = std::string((char*)buf,ret);
result = true;
} else {
LOGE("RSA_private_decrypt error");
}
delete[] buf;
}
}
return result;
}
bool RsaCrypt::Init(RsaCrypt::Mode mode, const std::string &key) {
if (initialized_) {
LOGE("init already done");
return true;
}
mode_ = mode;
bio_ = BIO_new_mem_buf((const void*)key.c_str(), -1);
if (bio_) {
if (mode_ == RsaCrypt::PRIVATE) {
PEM_read_bio_RSAPrivateKey(bio_, &rsa_, NULL, NULL);
} else {
//PEM_read_bio_RSAPublicKey(bio_, &rsa_, NULL, NULL);
PEM_read_bio_RSA_PUBKEY(bio_, &rsa_, NULL, NULL);
}
}
if (!rsa_) {
if (bio_) {
LOGE("rsa key error");
BIO_free_all(bio_);
bio_ = NULL;
}
} else {
rsa_size_ = RSA_size(rsa_);
initialized_ = true;
}
return initialized_;
}
0x2 参数key的格式
const char pub[]="-----BEGIN PUBLIC KEY-----\n"
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA56iwzdromMYI3T4DTtH8\n"
"H5g1WNRdfLJJs94rRLhr/wMDpmCYQx+7VQsbsqO6juxwOhALQ80wdAIZE0MjBeMa\n"
"svmAy0qhSSTRJ5s0/DLNf4nvmX6aQGHJAAD/411t9o88YLMMBKpXqQrdV3wXBWZE\n"
"5qISF6zpEa9e4YXCaGOrRWIrbgjBm1kWSL/H+YKj8D6zzmxooFqSHxnQhyyVv58+\n"
"aPZOYpkcAFC/A5A9twxl1wBtyxUZmynC3Nj2H+03VveYHyg4R3A22WemFUu1T5S6\n"
"FpyiMXXI6jfoO6/Oiq/VYg+IciCAs35/rljuyn9LpwFuzPVTJRiFS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