rsa,des,urldecode,hexdecode作为网络数据传输与请求中非常重要的编码方法,具有重要的意义
opennssl作为强有力的开源,为广大c++开发者提供了很大方便,但是,繁杂的api接口,缺乏文档,使其并不好用,
下面仅提供RSa的简单实现,加密解密逻辑,直接上代码
1. Rsa.h
#ifndef RSA_H_
#define RSA_H_
#include "openssl/rsa.h"
#include "openssl/bio.h"
#include "openssl/err.h"
#include "openssl/pem.h"
#include <stdio.h>
class Rsa
{
public:
Rsa()
{
rsa_handle_ = NULL;
}
~Rsa()
{
if(rsa_handle_ != NULL)
{
RSA_free(rsa_handle_);
}
}
public:
// 编解码-加密解密
char* RsaEncoding(char *txt, int txt_len, char *key, int key_len, int *encode_len, bool encrypt);
// 产生公钥、私钥
char* GenerateKey(bool pub, int *len);
//
private:
//
RSA* rsa_handle_;// for Generate public key and private key
//
};
2. Rsa.cpp
#include "Rsa.h"
char* Rsa::RsaEncoding(char *txt, int txt_len, char *key, int key_len, int *encode_len, bool encrypt)
{
if(txt == NULL || txt_len <= 0 || key == NULL || key_len <= 0)
{
return NULL;
}
RSA *rsa = NULL;
BIO* p_bio = NULL;//
if(rsa_handle_ != NULL)
{
rsa = rsa_handle_;
}
else
{
p_bio = BIO_new(BIO_s_mem());
BIO_puts(p_bio, key);
if(p_bio == NULL)
{
printf("p_bio is NULL\n");
return NULL;
}
if(encrypt)
{
rsa = PEM_read_bio_RSAPublicKey(p_bio, &rsa, 0, 0);
}
else
{
rsa = PEM_read_bio_RSAPrivateKey(p_bio, 0, 0, 0);
}
if(rsa == NULL)
{
printf("RSA is NULL\n");
return NULL;
}
}
int rsa_len = RSA_size(rsa);
char *ptr_encode = new char[rsa_len+1];
memset(ptr_encode, 0, rsa_len + 1);
int rc=0;
if(encrypt)
{
rc=RSA_public_encrypt(txt_len,(unsigned char *)txt,(unsigned char*)ptr_encode, rsa, RSA_PKCS1_PADDING);
}
else
{
rc = RSA_private_decrypt(rsa_len,(unsigned char *)txt,(unsigned char*)ptr_encode, rsa, RSA_PKCS1_PADDING);
}
if(rc <= 0)
{
int e=ERR_get_error();
printf("error code is:%s\n",ERR_error_string(e,NULL));
return NULL;
}
*encode_len = rc;
char *encoding_type = "rsa encrypt";
if(!encrypt)
{
encoding_type = "rsa decrypt";
}
printf("%s encoding:\n%s\nencoding_len:%d\n", encoding_type, ptr_encode, *encode_len);
if(rsa_handle_ != NULL)
{
BIO_free(p_bio);
}
else
{
BIO_free(p_bio);
RSA_free(rsa);
}
return ptr_encode;
}
char* Rsa::GenerateKey(bool pub, int *len)
{
if(rsa_handle_ == NULL)
{
rsa_handle_ = RSA_generate_key(4096, RSA_F4, NULL, NULL); // 4096:决定可加密解密的最大字符串长度
}
BIO* p_bio = BIO_new(BIO_s_mem());
if(p_bio == NULL)
{
printf("p_bio is NULL\n");
return NULL;
}
if(pub)
{
if(PEM_write_bio_RSAPublicKey (p_bio, rsa_handle_) == 0)
{
printf("write public key error\n");
return NULL;
}
}
else
{
if(PEM_write_bio_RSAPrivateKey (p_bio, rsa_handle_, 0, 0, 0, 0, 0) == 0)
{
printf("write private key error\n");
return NULL;
}
}
int keylen = BIO_pending(p_bio);
char *pem_key = new char[keylen+1];
memset(pem_key, 0, sizeof(char) * (keylen + 1));
BIO_read(p_bio, pem_key, keylen);
*len = keylen;
printf("Generate %s Key :\n\n%s\nlen = %d\n", type, pem_key, keylen);
BIO_free(p_bio);
return pem_key;
}
3. Demo.cpp
#include "Rsa.h"
int main(int argc, char *argv[])
{
char *content = "[][]{}}}@#!@$#$#$$#%%%^^%&^**((*(()))_)213~~~~~```--___++===??>:\":?..\/\/certification_code=SDSFGSFGFSGSFG&jar_version=ewefwfwrfrfrfMMM&tvid=0&&tv_name=cctv5&name = wang fenasdsff2cr3267677n7898m9m09<F7>m<F8>85ni<F8><F6>n<F8><F6>8n<F6>8<F6><F8>b<F7><F8>b88<F6><F8>8ub<F6>b";
printf("content length : %d\n", strlen(content));
Rsa rsa_handle;
int key_len = 0;
char *public_key = rsa_handle.GenerateKey(true, &key_len);
char *private_key = rsa_handle.GenerateKey(false, &key_len);
char *ptr_content = content;
int enc_len = 0;
int dec_len = 0;
char *encrypt_content = rsa_handle.RsaEncoding(ptr_content, strlen(content), public_key, strlen(public_key), &enc_len, true);
ptr_content = encrypt_content;
char *decrypt_content = rsa_handle.RsaEncoding(ptr_content, enc_len, private_key, strlen(private_key), &dec_len, false);
delete []encrypt_content;
encrypt_content = NULL;
delete []decrypt_content;
decrypt_content = NULL;
delete []public_key;
public_key = NULL;
delete []private_key;
private_key = NULL;
}
// 原创,转载请注明出处,谢谢!
当编译openssl的时候,
(1)在linux下,
./config -fPIC, // 有时不加-fPIC的话,在用(libssl.a libcryto.a)编译so的时候,会出现如下错误:libcrypto.a(rsa_lib.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
./make
(2)在Mac OS下编译的时候,