码迷,mamicode.com
首页 > 编程语言 > 详细

Linux 编写c++程序之openssl

时间:2015-04-29 16:57:31      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

在使用openssl 库前,需检测是否安装openssl , shell 窗口输入:openssl version  , 在openssl 安装完成之后, 可通过vi 编写测试代码 。

本例中附上加密,解密代码,方法分别是: EncodeRSAKeyFile(...) , DecodeRSAKeyFile(...)
这些示例代码在网上可以找到。

 

代码:

#include<openssl/bio.h>
#include<openssl/ssl.h>
#include<openssl/err.h>
#include<openssl/rsa.h>
#include<openssl/pem.h>
#include<stdio.h>
#include<string>
#include<cassert>
#include<iostream>
using namespace std;

int EncodeRSAKeyFile(const char * _strPemFileName , const char * _strData , unsigned char * buffer , int length){
	std::string strPemFileName = _strPemFileName;
	std::string strData = _strData ;
	if(strPemFileName.empty() || strData.empty()){
		assert(false);
		return 0 ;
	}
	
	FILE * hPubKeyFile = fopen(strPemFileName.c_str() , "rb");
	if(hPubKeyFile == NULL){
		assert(false);
		return 0;
	}

	std::string strRet;
	RSA * pRSAPublicKey = RSA_new();
	if(PEM_read_RSA_PUBKEY(hPubKeyFile , &pRSAPublicKey , 0 , 0) == NULL){
		assert(false);
		return 0;
	}

	int nLen = RSA_size(pRSAPublicKey);
	char * pEncode = new char[nLen + 1] ;
	int ret = RSA_public_encrypt(strData.length() , (const unsigned char *)strData.c_str() , (unsigned char * ) pEncode , pRSAPublicKey , RSA_PKCS1_PADDING);
	if(ret >= 0){
		strRet = std::string(pEncode , ret) ;
	}

	delete[] pEncode;
	RSA_free(pRSAPublicKey);
	fclose(hPubKeyFile);
	CRYPTO_cleanup_all_ex_data();
	
	if(strRet.length() + 1 > length){
		return 0;
	}

	memset(buffer , 0 , strRet.length() + 1) ;
	memcpy(buffer , &strRet[0] ,strRet.length());

	return strRet.length() + 1;
}

int DecodeRSAKeyFile(const char * _strPemfileName , const char * _strData , unsigned char * buffer ,  int length){
	std::string strPemFileName = _strPemfileName;
	std::string strData = _strData ;
	if(strPemFileName.empty() || strData.empty()){
		assert(false);
		return 0;
	}

	FILE* hPriKeyFile = NULL;
	hPriKeyFile =  fopen(strPemFileName.c_str() , "rb");
	if(hPriKeyFile == NULL){
		assert(false);
		return 0;
	}

	std::string strRet;
	RSA* pRSAPriKey = RSA_new();
	if(PEM_read_RSAPrivateKey(hPriKeyFile , &pRSAPriKey , 0 , 0) == NULL ){
		assert(false);
		return 0;
	}

	int nLen = RSA_size(pRSAPriKey);
	char * pDecode = new char[nLen + 1];

	int ret = RSA_private_decrypt(strData.length() , (const unsigned char *)strData.c_str() , (unsigned char *)pDecode , pRSAPriKey , RSA_PKCS1_PADDING);

	if(ret >= 0){
		strRet = std::string((char *)pDecode , ret);
	}

	delete [] pDecode;
	RSA_free(pRSAPriKey);
	fclose(hPriKeyFile);
	CRYPTO_cleanup_all_ex_data();

	if(strRet.length() + 1 > length){
		return 0 ;
	} else {
		memset(buffer , 0 , strRet.length() + 1);
		memcpy(buffer , &strRet[0] , strRet.length());
	}

	return strRet.length() + 1 ;

}

int main(){
	//jia mi
	const std::string one = "abcdeF";
	std::string strPubKey = "/root/test_2018_pub.key";
	const char * char1 = strPubKey.c_str();
	const char * char2 = one.c_str();
	unsigned char buffer[512] , buffer1[512];
	int length = EncodeRSAKeyFile(char1 , char2 , buffer , 512);
	std::string strResult = std::string((char *)buffer , length);
	//cout << "pwdtxt:" << strResult << endl;
 	//cout << length << endl;
	//return 0;

	//jiemi
	std::string strPriKey = "/root/test_2018.key";
	length = DecodeRSAKeyFile(strPriKey.c_str() , strResult.c_str() , buffer1 , 512 );
	std::string strOrgTxt = std::string((char *)buffer1 , length);
	cout << "orgTxtLength:" << length << endl <<  "orgTxt:" << strOrgTxt << endl ;

	return 0;
}

 

生成公私钥步骤:
openssl genrsa -out test_2048.key 2048 //私钥
openssl rsa -in test_2048.key -pubout -out test_2048_pub.key //公钥

gcc 编译指令:
gcc testzs.cpp -o testzsexe  -lcrypto -ldl -lstdc++

Linux 编写c++程序之openssl

标签:

原文地址:http://www.cnblogs.com/a_bu/p/4466189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!