2023年12月18日发(作者:)
return ret;}// 公钥解密函数,返回解密后的数据长度int rsa_op::pubkey_decrypt(const unsigned char *in, int in_len, unsigned char **out, int &out_len){ out_len = RSA_size(_pub_key); *out = (unsigned char *)malloc(out_len); if(NULL == *out) { printf("pubkey_decrypt:malloc error!n"); return -1; } memset((void *)*out, 0, out_len); printf("pubkey_decrypt:Begin RSA_public_decrypt ...n"); int ret = RSA_public_decrypt(in_len, in, *out, _pub_key, RSA_PKCS1_PADDING); return ret;}// 公钥加密函数int rsa_op::pubkey_encrypt(const unsigned char *in, int in_len, unsigned char **out, int &out_len){ out_len = RSA_size(_pub_key); *out = (unsigned char *)malloc(out_len); if(NULL == *out){ printf("pubkey_encrypt:malloc error!n"); return -1; }memset((void *)*out, 0, out_len); printf("pubkey_encrypt:Begin RSA_public_encrypt ...n"); int ret = RSA_public_encrypt(in_len, in, *out, _pub_key, RSA_PKCS1_PADDING/*RSA_NO_PADDING*/); return ret;}// 私钥解密函数,返回解密后的长度int rsa_op::prikey_decrypt(const unsigned char *in, int in_len, unsigned char **out, int &out_len){ out_len = RSA_size(_pri_key); *out = (unsigned char *)malloc(out_len); if(NULL == *out) { printf("prikey_decrypt:malloc error!n"); return -1; } memset((void *)*out, 0, out_len); printf("prikey_decrypt:Begin RSA_private_decrypt ...n"); int ret = RSA_private_decrypt(in_len, in, *out, _pri_key, RSA_PKCS1_PADDING); return ret;}// 释放分配的内存资源void rsa_op::free_res(){ if(_pub_expd) {
delete []_pub_expd; _pub_expd = NULL; } if(_pri_expd) { delete []_pri_expd; _pri_expd = NULL; }if(_module) { delete []_module; _module = NULL; }}// 释放公钥和私钥结构资源int rsa_op::close_key(){ if(_pub_key) { RSA_free(_pub_key); _pub_key = NULL; } if(_pri_key) { RSA_free(_pri_key); _pri_key = NULL; } return 0;}3.2.3 测试代码


发布评论