zoukankan      html  css  js  c++  java
  • Linux 编写c++程序之openssl

    在使用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++

  • 相关阅读:
    hdu 1395 2^x(mod n) = 1(C++)(欧拉定理 分解素因数)
    6. 数论准备知识
    hdu 2973 YAPTCHA(C++)(威尔逊定理)
    牛客小白月赛12——B.华华教月月做数学
    牛客小白月赛12——A.华华听月月唱歌
    5. 卡特兰数(Catalan)公式、证明、代码、典例.
    4.质数判定和质数筛法(埃拉托色尼筛选法,线性筛法/欧拉筛法)
    3.牛顿迭代法求解方程的根
    Codeforces刷题
    刷题计划
  • 原文地址:https://www.cnblogs.com/a_bu/p/4466189.html
Copyright © 2011-2022 走看看