在使用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的更多相关文章

  1. 在Linux上编写C#程序

    自从C#开源之后,在Linux编写C#程序就成了可能.Mono-project就是开源版本的C#维护项目.在Linux平台上使用的C#开发工具为monodevelop.安装方式如下: 首先需要安装一些 ...

  2. Linux Kernel(Android) 加密算法汇总(四)-应用程序调用OpenSSL加密演算法

    Linux Kernel(Android) 加密算法总结(三)-应用程序调用内核加密算法接口 讲到了怎样调用内核中的接口的方法. 本节主要是介绍怎样Android C/C++应用程序调用Openssl ...

  3. linux下对qt编写的程序进行部署

    当我们完成程序设计之后,需要将可执行程序交付客户,而运行环境里面可能是没有相关支持库的,这个时候就涉及到部署的相关问题.对于我们在Linux下基于QT编写的图像处理程序,我们采用linuxdeploy ...

  4. linux中VI编写C程序。。。

    在linux中编写C程序时不像编写shell那样开头要#!/bin/bash,但是在C程序中要指定头文件(头文件是指输入输出,宏等,而且要首先声明,也是必须要开始就声明的) 写好C代码后要给C文件赋予 ...

  5. Linux多任务编程之六:编写多进程程序及其代码(转)

    来源:CSDN  作者:王文松  转自Linux公社 ------------------------------------------------------------------------- ...

  6. Rust Aya 编写 eBPF 程序

    本文地址:https://www.ebpf.top/post/ebpf_rust_aya 1. 前言 Linux 内核 6.1 版本中有一个非常引人注意的变化:引入了对 Rust 编程语言的支持.Ru ...

  7. linux下QT程序输出乱码解决方法

    参考文章:http://blog.csdn.net/jiang1013nan/article/details/6667871 http://my.oschina.net/zjlaobusi/blog/ ...

  8. 如何在windows中编写R程序包(转载)

    网上有不少R包的编译过程介绍,挑选了一篇比较详细的,做了稍许修改后转载至此,与大家分享 如何在windows中编写R程序包 created by helixcn modified by binaryf ...

  9. Linux下C程序的编译,运行,及调试

    先查看linux有没有gcc 和 gdb $ gcc -v $ gdb -v 如果没有安装gcc,可以 $ yum install gcc 要获取管理员权限才能安装软件,$ su root (有的li ...

随机推荐

  1. 随机采样方法整理与讲解(MCMC、Gibbs Sampling等)

    本文是对参考资料中多篇关于sampling的内容进行总结+搬运,方便以后自己翻阅.其实参考资料中的资料写的比我好,大家可以看一下!好东西多分享!PRML的第11章也是sampling,有时间后面写到P ...

  2. 关于qt的集成开发环境

    事情是这样的,在archlinux中安装好qt5之后,只发现了一个 qt designer,这个只能用来写界面.其它的几个程序(qt assistant)算是查文档的这类辅助的程序. 记得在windo ...

  3. Windows下pry安装和配置

    Windows下pry安装和配置 pry是一个增强型的交互式命令行工具,比irb强大. 有自动完成功能,自动缩进,有颜色.有更强大的调试功能. pry 安装很简单. 在终端输入: gem instal ...

  4. 网站中集成jquery.imgareaselect实现图片的本地预览和选择截取

    imgAreaSelect 是由 Michal Wojciechowski开发的一款非常好用的jquery插件,实现了图片的截取功能.其文档和Demo也是很详尽的.大家可以到http://odynie ...

  5. [转] GitHub上README.md教程

    点击阅读原文 最近对它的README.md文件颇为感兴趣.便写下这贴,帮助更多的还不会编写README文件的同学们. README文件后缀名为md.md是markdown的缩写,markdown是一种 ...

  6. Unity3D UGUI学习系列索引(暂未完成)

    U3D UGUI学习1 - 层级环境 U3D UGUI学习2 - Canvas U3D UGUI学习3 - RectTransform U3D UGUI学习4 - Text U3D UGUI学习5 - ...

  7. mac攻略(三) -- apache站点配置

    Mac OS X 中默认有两个目录可以直接运行你的 Web 程序, 一个是系统级的 Web 根目录:/Library/WebServer/Documents/ 此根目录我们平常使用地址http://l ...

  8. latex 异或

    用\lxor \(\lxor\) 用\veebar \(\veebar\) 用\oplus \(\oplus\) ... 怎么不是我想象的那样... 算了.

  9. C++全局和静态变量初始化

    转自:http://www.cnblogs.com/zhenjing/archive/2010/10/15/1852116.html 对于C语言的全局和静态变量,不管是否被初始化,其内存空间都是全局的 ...

  10. 从零开始学iPhone开发(1)——工具的使用

    前提:已经具备了苹果电脑或者iMac,或者安装好了x86苹果而且已经联网. 苹果系统版本要求是:Max OS X Lion,或者 Mountain Lion 我们对iPhone进行使用的工具是XCod ...