openssl rsa应用与分析

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下编译的时候,

 ./config darwin64-x86_64-cc 
./make




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

seasermy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值