1.安装openssl包
sudo apt-get install openssl
sudo apt-get install libssl-dev
借鉴csdn大神代码,进行了改写,调试
2.tools.h文件
-
#ifndef TOOLS_H
-
#define TOOLS_H
-
#include<openssl/rsa.h>
-
#include <openssl/bio.h>
-
#include<openssl/pem.h>
-
#include<openssl/err.h>
-
#include<iostream>
-
using namespace std;
-
-
static const std::string base64_chars =
-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-
"abcdefghijklmnopqrstuvwxyz"
-
"0123456789+/";
-
-
static inline bool is_base64(unsigned char c) {
-
return (isalnum(c) || (c == '+') || (c == '/'));
-
}
-
-
-
-
static RSA* GetKeyRSA(std::string strKey,const int type)//type = 1 公钥 type = 2 私钥
-
{
-
int nPublicKeyLen = strKey.size(); //strKey为base64编码的公钥字符串
-
for(int i = 64; i < nPublicKeyLen; i+=64)
-
{
-
if(strKey[i] != '\n')
-
{
-
strKey.insert(i, "\n");
-
}
-
i++;
-
}
-
if(type == 1){
-
strKey.insert(0, "-----BEGIN PUBLIC KEY-----\n");
-
strKey.append("\n-----END PUBLIC KEY-----\n");
-
}else{
-
strKey.insert(0, "-----BEGIN PRIVATE KEY-----\n");
-
strKey.append("\n-----END PRIVATE KEY-----\n");
-
}
-
-
BIO *bio = NULL;
-
RSA *rsa = NULL;
-
char *chPublicKey = const_cast<char *>(strKey.c_str());
-
printf("chPublicKey===%s\n",chPublicKey);
-
if ((bio = BIO_new_mem_buf(chPublicKey, -1)) == NULL) //从字符串读取RSA公钥
-
{
-
return NULL;
-
}
-
if(type == 1)
-
rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL); //从bio结构中得到rsa结构
-
// rsa = PEM_read_bio_RSAPublicKey(bio,&rsa,NULL,NULL);
-
else
-
rsa = PEM_read_bio_RSAPrivateKey(bio,NULL,NULL,NULL);
-
if (NULL == rsa)
-
{
-
BIO_free_all(bio);
-
unsigned long ulErr = ERR_get_error(); // 获取错误号
-
char szErrMsg[1024] = {0};
-
char *pTmp = NULL;
-
pTmp = ERR_error_string(ulErr,szErrMsg); // 格式:error:errId:库:函数:原因
-
cout << szErrMsg;
-
cout << "load public key fail error=" <<ulErr << " msg=" << szErrMsg;
-
}
-
else
-
{
-
cout<< "load public key ok "<<endl;
-
}
-
return rsa;
-
}
-
-
-
static std::string base64_decode(std::string const& encoded_string) {
-
int in_len = encoded_string.size();
-
int i = 0;
-
int j = 0;
-
int in_ = 0;
-
unsigned char char_array_4[4], char_array_3[3];
-
std::string ret;
-
-
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
-
char_array_4[i++] = encoded_string[in_]; in_++;
-
if (i ==4) {
-
for (i = 0; i <4; i++)
-
char_array_4[i] = base64_chars.find(char_array_4[i]);
-
-
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
-
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
-
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
-
-
for (i = 0; (i < 3); i++)
-
ret += char_array_3[i];
-
i = 0;
-
}
-
}
-
if (i) {
-
for (j = i; j <4; j++)
-
char_array_4[j] = 0;
-
-
for (j = 0; j <4; j++)
-
char_array_4[j] = base64_chars.find(char_array_4[j]);
-
-
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
-
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
-
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
-
-
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
-
}
-
-
return ret;
-
}
-
-
-
static char* base64Encode(char const* origSigned, unsigned origLength)
-
{
-
unsigned int numOrig24BitValues = 0;
-
unsigned int numResultBytes = 0;
-
int havePadding = 0;
-
int havePadding2 = 0;
-
char* result = NULL;
-
unsigned int i = 0;
-
-
-
unsigned char const* orig = (unsigned char const*)origSigned; // in case any input bytes have the MSB set
-
if (orig == NULL) return NULL;
-
-
numOrig24BitValues = origLength / 3;
-
havePadding = origLength > numOrig24BitValues * 3;
-
havePadding2 = origLength == numOrig24BitValues * 3 + 2;
-
numResultBytes = 4 * (numOrig24BitValues + havePadding);
-
result = (char*)malloc(numResultBytes + 1); // allow for trailing '/0'
-
-
// Map each full group of 3 input bytes into 4 output base-64 characters:
-
-
for (i = 0; i < numOrig24BitValues; ++i)
-
{
-
result[4 * i + 0] = base64_chars[(orig[3 * i] >> 2) & 0x3F];
-
result[4 * i + 1] = base64_chars[(((orig[3 * i] & 0x3) << 4) | (orig[3 * i + 1] >> 4)) & 0x3F];
-
result[4 * i + 2] = base64_chars[((orig[3 * i + 1] << 2) | (orig[3 * i + 2] >> 6)) & 0x3F];
-
result[4 * i + 3] = base64_chars[orig[3 * i + 2] & 0x3F];
-
}
-
-
// Now, take padding into account. (Note: i == numOrig24BitValues)
-
if (havePadding)
-
{
-
result[4 * i + 0] = base64_chars[(orig[3 * i] >> 2) & 0x3F];
-
if (havePadding2)
-
{
-
result[4 * i + 1] = base64_chars[(((orig[3 * i] & 0x3) << 4) | (orig[3 * i + 1] >> 4)) & 0x3F];
-
result[4 * i + 2] = base64_chars[(orig[3 * i + 1] << 2) & 0x3F];
-
}
-
else
-
{
-
result[4 * i + 1] = base64_chars[((orig[3 * i] & 0x3) << 4) & 0x3F];
-
result[4 * i + 2] = '=';
-
}
-
result[4 * i + 3] = '=';
-
}
-
-
result[numResultBytes] = (char)('/0');
-
return result;
-
-
}
-
-
#endif // TOOLS_H
2.verify.h
-
#include "tools.h"
-
char* rsa_sign(const char* privateKey,const char* content);
-
bool rsa_verify(const char* publicKey,const char* sign,const char* content);
-
char* EncryData(const char* publicKey,const char* content);
-
void DecryData(const char* privateKey,const char* content);
3.verify.c
-
#include "rsapro.h"
-
#include<string.h>
-
char *rsa_sign(const char *privateKey, const char *content)
-
{
-
char p_sign[10000] = {0};
-
RSA *p_rsa = GetKeyRSA(privateKey,2);
-
int rsa_len = RSA_size(p_rsa);
-
if(RSA_sign(NID_md5,(unsigned char*)content,strlen(content),(unsigned char*)p_sign,(unsigned int *)&rsa_len,p_rsa)<0){
-
cout<<"RSA_SIGN FAILED"<<endl;
-
return "";
-
}
-
RSA_free(p_rsa);
-
char *sign = base64Encode(p_sign,rsa_len);
-
cout<<"sign===="<<sign<<endl;
-
return sign;
-
-
-
}
-
-
bool rsa_verify(const char *publicKey, const char *sign, const char *content)
-
{
-
-
char p_ver[1000] = {0};
-
RSA *p_rsa = GetKeyRSA(publicKey,1);
-
int rsa_len = RSA_size(p_rsa);
-
std::string dSign = base64_decode(sign);
-
int res = RSA_verify(NID_md5,(unsigned char*)content,strlen(content) ,(unsigned char*)dSign.c_str(),dSign.length() ,p_rsa);
-
RSA_free(p_rsa);
-
return res == 1;
-
}
-
-
char* EncryData(const char *publicKey, const char *content)
-
{
-
-
RSA *p_rsa = GetKeyRSA(publicKey,1);
-
int rsa_len = RSA_size(p_rsa);
-
char p_e[1000] = {0};
-
-
if(RSA_public_encrypt(strlen(content), (unsigned char *)content, (unsigned char*)p_e, p_rsa, RSA_PKCS1_PADDING)<0) {
-
cout<<"RSA_public_encrypt err"<<endl;
-
return NULL;
-
}
-
-
while (true) {
-
if(strlen(p_e) == 128){
-
break;
-
}
-
if(RSA_public_encrypt(strlen(content), (unsigned char *)content, (unsigned char*)p_e, p_rsa, RSA_PKCS1_PADDING)<0)
-
cout<<"RSA_public_encrypt err"<<endl;
-
-
}
-
RSA_free(p_rsa);
-
char * bp_e = base64Encode(p_e,rsa_len);
-
cout<<"encrydate====="<<bp_e<<endl;
-
return bp_e;
-
-
}
-
-
void DecryData(const char *privateKey, const char *content)
-
{
-
-
RSA *p_rsa = GetKeyRSA(privateKey,2);
-
int rsa_len = RSA_size(p_rsa);
-
char p_de[1000] = {0};
-
std::string unBase64 = base64_decode(content);
-
if (RSA_private_decrypt(unBase64.length(), (unsigned char *)unBase64.c_str(), (unsigned char*)p_de, p_rsa, RSA_PKCS1_PADDING)<0) {//RSA_PKCS1_PADDING
-
cout<<"RSA_private_decrypt err"<<endl;
-
return ;
-
}
-
RSA_free(p_rsa);
-
cout<<"RSA_private_decrypt:"<<p_de<<endl;
-
}
4.main.c
-
#include "verify.h"
-
#include<string.h>
-
int main(int argc, char *argv[])
-
{
-
printf("test main.....\n");
-
char *publicKey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqfi52bS5eBwlVUxmtj2h6r47S8nxWzEkMS4aAS8W/PBkubAHi3OZk/ty8cnca3U4wW2P6fOtho3IcYlHAbZ0nGgLr9YzW8Qjf5/oG478QP1T2ikRyAd8xzh+9A4RUYJxxpTqpQkbz47OiB0zCCH2zVrgYhqKl5U6q70KD/ND4tNvjVW71tRfSM2A4EeJMfzydKa+Fh5vYsYBtPRKQijfVZ9LaGXUPTt++JNQ6yNEDhfYrh/CeOYbhFtUB8gQ4+UqzEEyucb5Web4hnBrU2/E2NsPUVXB5q+r0cX4CgeGs55uGoKzaG2vL8jnx7hS58SpXRYn2JO/YxU1yCUzI71FawIDAQAB";
-
char *privateKey="MIIEogIBAAKCAQEAqfi52bS5eBwlVUxmtj2h6r47S8nxWzEkMS4aAS8W/PBkubAHi3OZk/ty8cnca3U4wW2P6fOtho3IcYlHAbZ0nGgLr9YzW8Qjf5/oG478QP1T2ikRyAd8xzh+9A4RUYJxxpTqpQkbz47OiB0zCCH2zVrgYhqKl5U6q70KD/ND4tNvjVW71tRfSM2A4EeJMfzydKa+Fh5vYsYBtPRKQijfVZ9LaGXUPTt++JNQ6yNEDhfYrh/CeOYbhFtUB8gQ4+UqzEEyucb5Web4hnBrU2/E2NsPUVXB5q+r0cX4CgeGs55uGoKzaG2vL8jnx7hS58SpXRYn2JO/YxU1yCUzI71FawIDAQABAoIBAAvcNDOxvaNY6235R819nXfA0GhobfP8CNd5rQzaGVOK/qxKUSx044mE6S9f84Z4Bn7Qw1MC0t/pssvhVVgUQCzNwcsCOlEz7XoxUUPjBU0P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ktvIu9vEW5Ia8FDOWoGTappV+Uj/ss=";
-
char *verifystr="gxycloudtest";
-
printf("len==%s\n",strlen(privateKey));
-
//rsa_sign(privateKey,verifystr);
-
return 0;
-
}
阅读(5664) | 评论(0) | 转发(0) |