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

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

代码:

  1. #include<openssl/bio.h>
  2. #include<openssl/ssl.h>
  3. #include<openssl/err.h>
  4. #include<openssl/rsa.h>
  5. #include<openssl/pem.h>
  6. #include<stdio.h>
  7. #include<string>
  8. #include<cassert>
  9. #include<iostream>
  10. using namespace std;
  11.  
  12. int EncodeRSAKeyFile(const char * _strPemFileName , const char * _strData , unsigned char * buffer , int length){
  13. std::string strPemFileName = _strPemFileName;
  14. std::string strData = _strData ;
  15. if(strPemFileName.empty() || strData.empty()){
  16. assert(false);
  17. return 0 ;
  18. }
  19.  
  20. FILE * hPubKeyFile = fopen(strPemFileName.c_str() , "rb");
  21. if(hPubKeyFile == NULL){
  22. assert(false);
  23. return 0;
  24. }
  25.  
  26. std::string strRet;
  27. RSA * pRSAPublicKey = RSA_new();
  28. if(PEM_read_RSA_PUBKEY(hPubKeyFile , &pRSAPublicKey , 0 , 0) == NULL){
  29. assert(false);
  30. return 0;
  31. }
  32.  
  33. int nLen = RSA_size(pRSAPublicKey);
  34. char * pEncode = new char[nLen + 1] ;
  35. int ret = RSA_public_encrypt(strData.length() , (const unsigned char *)strData.c_str() , (unsigned char * ) pEncode , pRSAPublicKey , RSA_PKCS1_PADDING);
  36. if(ret >= 0){
  37. strRet = std::string(pEncode , ret) ;
  38. }
  39.  
  40. delete[] pEncode;
  41. RSA_free(pRSAPublicKey);
  42. fclose(hPubKeyFile);
  43. CRYPTO_cleanup_all_ex_data();
  44.  
  45. if(strRet.length() + 1 > length){
  46. return 0;
  47. }
  48.  
  49. memset(buffer , 0 , strRet.length() + 1) ;
  50. memcpy(buffer , &strRet[0] ,strRet.length());
  51.  
  52. return strRet.length() + 1;
  53. }
  54.  
  55. int DecodeRSAKeyFile(const char * _strPemfileName , const char * _strData , unsigned char * buffer , int length){
  56. std::string strPemFileName = _strPemfileName;
  57. std::string strData = _strData ;
  58. if(strPemFileName.empty() || strData.empty()){
  59. assert(false);
  60. return 0;
  61. }
  62.  
  63. FILE* hPriKeyFile = NULL;
  64. hPriKeyFile = fopen(strPemFileName.c_str() , "rb");
  65. if(hPriKeyFile == NULL){
  66. assert(false);
  67. return 0;
  68. }
  69.  
  70. std::string strRet;
  71. RSA* pRSAPriKey = RSA_new();
  72. if(PEM_read_RSAPrivateKey(hPriKeyFile , &pRSAPriKey , 0 , 0) == NULL ){
  73. assert(false);
  74. return 0;
  75. }
  76.  
  77. int nLen = RSA_size(pRSAPriKey);
  78. char * pDecode = new char[nLen + 1];
  79.  
  80. int ret = RSA_private_decrypt(strData.length() , (const unsigned char *)strData.c_str() , (unsigned char *)pDecode , pRSAPriKey , RSA_PKCS1_PADDING);
  81.  
  82. if(ret >= 0){
  83. strRet = std::string((char *)pDecode , ret);
  84. }
  85.  
  86. delete [] pDecode;
  87. RSA_free(pRSAPriKey);
  88. fclose(hPriKeyFile);
  89. CRYPTO_cleanup_all_ex_data();
  90.  
  91. if(strRet.length() + 1 > length){
  92. return 0 ;
  93. } else {
  94. memset(buffer , 0 , strRet.length() + 1);
  95. memcpy(buffer , &strRet[0] , strRet.length());
  96. }
  97.  
  98. return strRet.length() + 1 ;
  99.  
  100. }
  101.  
  102. int main(){
  103. //jia mi
  104. const std::string one = "abcdeF";
  105. std::string strPubKey = "/root/test_2018_pub.key";
  106. const char * char1 = strPubKey.c_str();
  107. const char * char2 = one.c_str();
  108. unsigned char buffer[512] , buffer1[512];
  109. int length = EncodeRSAKeyFile(char1 , char2 , buffer , 512);
  110. std::string strResult = std::string((char *)buffer , length);
  111. //cout << "pwdtxt:" << strResult << endl;
  112. //cout << length << endl;
  113. //return 0;
  114.  
  115. //jiemi
  116. std::string strPriKey = "/root/test_2018.key";
  117. length = DecodeRSAKeyFile(strPriKey.c_str() , strResult.c_str() , buffer1 , 512 );
  118. std::string strOrgTxt = std::string((char *)buffer1 , length);
  119. cout << "orgTxtLength:" << length << endl << "orgTxt:" << strOrgTxt << endl ;
  120.  
  121. return 0;
  122. }

生成公私钥步骤:
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. 分列:将excel单元格的内容拆分为两列

    提要:处理excel数据时有时需要把单元格的内容拆分为两列,可能方便外部软件的链接,可能使数据显示更明晰等等,有人说直接剪切加粘贴不就可以了吗,但是有时数据过多,这样处理很不效率,网上搜索的方法说插入 ...

  2. C#的path.GetFullPath 获取上级目录实现方法

    这篇文章主要介绍了C#的path.GetFullPath 获取上级目录实现方法,包含了具体的C#实现方法以及ASP.net与ASP等的方法对比,非常具有实用价值,需要的朋友可以参考下   本文实例讲述 ...

  3. 本地json文件的编辑器,node-webkit开发的exe程序

    首发:个人博客,更新&纠错&回复 在昨天的dota契合度计算器中,用到了dota英雄数据和dota玩家数据这两个数据库,为了便于网页应用使用,这两个数据库的存储格式是json,即her ...

  4. V​S​2​0​1​2​快​捷​键

    VS2012变化的快捷键: 注释::VS2010是(Ctrl+E,C),VS2012是(Ctrl+K, Ctrl+C),实际操作,按住Ctrl键不放,先按K键,再按C键.相当于Ctrl+K加 Ctrl ...

  5. javaWeb 使用 filter 处理全站乱码问题

    1. web.xml文件中的配置 <filter> <filter-name>CharacterEncodingFilter</filter-name> <f ...

  6. Posix消息队列

    转载于:http://blog.csdn.net/zx714311728/article/details/53197196 1.消息队列 消息队列可以认为是一个消息链表,消息队列是随内核持续的.队列中 ...

  7. Java调用脚本

    几个参考: java调用shell http://www.cnblogs.com/Seamanm/archive/2010/10/04/1842059.html java程序中调用linux命令    ...

  8. noi 2989 糖果

    题目链接:http://noi.openjudge.cn/ch0206/2989/ 首先,数据很大,直接用背包会re. 这里增加的是对%k 的余数维度.f[i][j] 表示前 i 种糖果取到总颗数模 ...

  9. js中的什么时候需要用new来实例化?

    有人说js中函数和类就是一个概念,请问:1 为什么我们在大多数情况下没有用new来实例化一个类(函数),如下 JavaScript code   1 2 3 4 5 6 7 <script> ...

  10. 矩阵的QR分解

    #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> # ...