在使用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. 在linux中安装adb和fastboot工具

    我用的是archlinux,在官方的软件仓库里就可以找到对应的包,包的名字叫:android-tools 据说debian系列的软件包是两个,分别是:android-tools-adb, androi ...

  2. Redis的简介与安装

    1.简介 Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...

  3. python核心编程学习记录之函数与函数式编程

    @func function 意思是func(function) @func(a) function 意思是func(a)这是个函数对象,在去调用function函数 如果要传额外的值,只传值用*tu ...

  4. 使用 jsPlumb 绘制拓扑图 —— 异步加载与绘制的实现

    本文实现的方法可以边异步加载数据边绘制拓扑图. 有若干点需要说明一下: 1.  一次性获取所有数据并绘制拓扑图, 请参见文章: <使用 JsPlumb 绘制拓扑图的通用方法> ; 本文实现 ...

  5. python发送邮件方法

    1.普通文本邮件 #!/usr/bin/env python # -*- coding:utf-8 -*- import smtplib from email.mime.text import MIM ...

  6. Linux C Programing - Terminal(1)

    #include <stdio.h> //getchar() putchar() printf() gets() puts() sprintf() #include <stdlib. ...

  7. 今日随笔:scrollTop与overflow

    今天想写一个页面一加载滚动条就自动滚到底部的效果,结果在IE上实现成功了,chrome上完全没反应,最后测试了一下,居然是因为css文件中,html,body都写了overflow:auto这一语句, ...

  8. Android ListView实现不同item的方法和原理分析

    ListView实现不同item的方法和原理分析 一问题抛出Listview是android里面的重要组件,用来显示一个竖向列表,这个没有什么问题:但是有个时候列表里面的item不是一样的,如下图,列 ...

  9. 在虚拟机发布网站,设置服务器外网访问ip端口号

    这是虚机上的发布网站的网站端口号 这一步要在实机设置 做完这一步,在外网就可以访问你刚刚发布的站点了

  10. JS常规的验证代码 - 手机号,邮箱,字符串查找

    //在字符串中执行查找 function isDisgit(s){ var reg = /^[0-9]{1,20}$/; var result = reg.exec(s); //如果格式不正确,返回n ...