c++ 下des的运用

通过openssl 进行加密解密

myDes.h

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/des.h>
#include <iostream>
#include <vector>
using namespace std;typedef enum{CBC=1,
}desmode_t;typedef enum{PAD_PKCS5=1,
}mypadmode_t;class my_Des
{
public:my_Des(std::string userkey,desmode_t userdesmode=CBC, std::string IV=NULL,mypadmode_t padmode=PAD_PKCS5);std::string decrypt(const std::string &cipherText,mypadmode_t mypadmode);std::string decrypt(const std::string &cipherText);std::string des_cbc_pkcs5_decrypt(char *cipherText, int datalen, const std::string &key);std::string des_cbc_pkcs5_encrypt(char *cipherText, int datalen, const std::string &key);std::string des_cbc_pkcs5_encrypt(const std::string &clearText, const std::string &key);
private:std::string padFUN(const std::string &cipherText,mypadmode_t mypadmode);std::string des_cbc_pkcs5_decrypt(const std::string &cipherText, const std::string &key);private:mypadmode_t mypadmode;desmode_t desmode;std::string key;std::string my_iv;
};

myDes.cpp

#include "myDes.h"my_Des::my_Des(std::string userkey,desmode_t userdesmode, std::string IV,mypadmode_t padmode)
{key=userkey;desmode=userdesmode;mypadmode=padmode;my_iv=IV;
}//解密 cbc pkcs5padding 自己实现  //zeropadding / pkcs7padding 跟 pkcs5padding是一样的
std::string my_Des::des_cbc_pkcs5_decrypt(const std::string &cipherText, const std::string &key)
{// static unsigned char cbc_iv[8] = {'m', 'H', 'A', 'x', 's', 'L', 'Y', 'z'};static unsigned char cbc_iv[8];memcpy(cbc_iv,my_iv.data(),sizeof (cbc_iv));//初始化IV向量std::string clearText;DES_cblock keyEncrypt, ivec;memset(keyEncrypt, 0, 8);if (key.length() <= 8)memcpy(keyEncrypt, key.c_str(), key.length());elsememcpy(keyEncrypt, key.c_str(), 8);DES_key_schedule keySchedule;  //密钥表DES_set_key_unchecked(&keyEncrypt, &keySchedule);   //设置密钥,且不检测密钥奇偶性memcpy(ivec, cbc_iv, sizeof(cbc_iv));// 循环解密,每8字节一次const_DES_cblock inputText;DES_cblock outputText;std::vector<unsigned char> vecCleartext;unsigned char tmp[8];for (unsigned int i = 0; i < cipherText.length() / 8; i++){memcpy(inputText, cipherText.c_str() + i * 8, 8);DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_DECRYPT);  //解密memcpy(tmp, outputText, 8);for (int j = 0; j < 8; j++)vecCleartext.push_back(tmp[j]);//重置ivec//memcpy(ivec, outputText, 8);  //解密过程不需要用前一块的结果作为下一块的IV}unsigned int mod=clearText.length() % 8;if (mod != 0){unsigned int tmp1 = clearText.length() / 8 * 8;unsigned int tmp2 = clearText.length() - tmp1;memset(inputText,0, static_cast<size_t>(tmp2));memcpy(inputText, cipherText.c_str() + tmp1, tmp2);DES_ncbc_encrypt(inputText, outputText, static_cast<long>(tmp2), &keySchedule, &ivec, DES_DECRYPT);  //解密memcpy(tmp, outputText, tmp2);for (unsigned int j = 0; j < mod; j++)vecCleartext.push_back(tmp[j]);}clearText.clear();clearText.assign(vecCleartext.begin(),vecCleartext.end());// 返回值去除末尾的填充值unsigned int retlen=clearText.length();unsigned int modlen=static_cast<unsigned int>(clearText[retlen-1]);// for(char i=0;i<retlen;i++)//   clearText.pop_back();// clearText.erase(retlen-modlen,retlen);return clearText;
}
//这里有一个char * 和 string 两个版本, string 为网上大神的版本,char * 为自己改进的,因为字符串中遇到'\0'就截止。但是加密字串难免回产生'\0' 所以 以char * 处理更好
std::string my_Des::des_cbc_pkcs5_encrypt(char *cipherText, int datalen, const std::string &key)
{// static unsigned char cbc_iv[8] = {'j', 'k', 't', '1', '2', '3', '4', '5'};static unsigned char cbc_iv[8];memcpy(cbc_iv,my_iv.data(),sizeof (cbc_iv));//初始化IV向量std::string strCipherText;DES_cblock keyEncrypt, ivec;memset(keyEncrypt, 0, 8);if (key.length() <= 8)memcpy(keyEncrypt, key.c_str(), key.length());elsememcpy(keyEncrypt, key.c_str(), 8);DES_key_schedule keySchedule;  //密钥表DES_set_key_unchecked(&keyEncrypt, &keySchedule);   //设置密钥,且不检测密钥奇偶性memcpy(ivec, cbc_iv, sizeof(cbc_iv));// 循环加密,每8字节一次const_DES_cblock inputText;DES_cblock outputText;std::vector<unsigned char> vecCiphertext;unsigned char tmp[8];for (int i = 0; i < datalen / 8; i++){memcpy(inputText,cipherText + i * 8, 8);DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT);  //加密memcpy(tmp, outputText, 8);for (int j = 0; j < 8; j++)vecCiphertext.push_back(tmp[j]);//重置ivecmemcpy(ivec, outputText, 8);}if (datalen% 8 != 0){int tmp1 =datalen / 8 * 8;int tmp2 = datalen - tmp1;memset(inputText,(8-tmp2), 8);memcpy(inputText, cipherText + tmp1, tmp2);}else{memset(inputText,8, 8);}// 加密函数DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT);  //加密memcpy(tmp, outputText, 8);for (int j = 0; j < 8; j++)vecCiphertext.push_back(tmp[j]);strCipherText.clear();strCipherText.assign(vecCiphertext.begin(), vecCiphertext.end());return strCipherText;
}std::string my_Des::des_cbc_pkcs5_encrypt(const std::string &clearText, const std::string &key)
{static unsigned char cbc_iv[8];memcpy(cbc_iv,my_iv.data(),sizeof (cbc_iv));//初始化IV向量std::string strCipherText;DES_cblock keyEncrypt, ivec;memset(keyEncrypt, 0, 8);if (key.length() <= 8)memcpy(keyEncrypt, key.c_str(), key.length());elsememcpy(keyEncrypt, key.c_str(), 8);DES_key_schedule keySchedule;  //密钥表DES_set_key_unchecked(&keyEncrypt, &keySchedule);   //设置密钥,且不检测密钥奇偶性memcpy(ivec, cbc_iv, sizeof(cbc_iv));// 循环加密,每8字节一次const_DES_cblock inputText;DES_cblock outputText;std::vector<unsigned char> vecCiphertext;unsigned char tmp[8];for (int i = 0; i < clearText.length() / 8; i++){memcpy(inputText, clearText.c_str() + i * 8, 8);DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT);  //加密memcpy(tmp, outputText, 8);for (int j = 0; j < 8; j++)vecCiphertext.push_back(tmp[j]);//重置ivecmemcpy(ivec, outputText, 8);}if (clearText.length() % 8 != 0){int tmp1 = clearText.length() / 8 * 8;int tmp2 = clearText.length() - tmp1;memset(inputText,(8-tmp2), 8);memcpy(inputText, clearText.c_str() + tmp1, tmp2);}else{memset(inputText,8, 8);}// 加密函数DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT);  //加密memcpy(tmp, outputText, 8);for (int j = 0; j < 8; j++)vecCiphertext.push_back(tmp[j]);strCipherText.clear();strCipherText.assign(vecCiphertext.begin(), vecCiphertext.end());return strCipherText;
}std::string my_Des::des_cbc_pkcs5_decrypt(char *cipherText, int datalen, const std::string &key)
{// static unsigned char cbc_iv[8] = {'m', 'H', 'A', 'x', 's', 'L', 'Y', 'z'};static unsigned char cbc_iv[8];memcpy(cbc_iv,my_iv.data(),sizeof (cbc_iv));//初始化IV向量std::string clearText;DES_cblock keyEncrypt, ivec;memset(keyEncrypt, 0, 8);if (key.length() <= 8)memcpy(keyEncrypt, key.c_str(), key.length());elsememcpy(keyEncrypt, key.c_str(), 8);DES_key_schedule keySchedule;  //密钥表DES_set_key_unchecked(&keyEncrypt, &keySchedule);   //设置密钥,且不检测密钥奇偶性memcpy(ivec, cbc_iv, sizeof(cbc_iv));// 循环解密,每8字节一次const_DES_cblock inputText;DES_cblock outputText;std::vector<unsigned char> vecCleartext;unsigned char tmp[8];for (unsigned int i = 0; i < (datalen)/ 8; i++){memcpy(inputText, cipherText + i * 8, 8);DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_DECRYPT);  //解密memcpy(tmp, outputText, 8);for (int j = 0; j < 8; j++)vecCleartext.push_back(tmp[j]);//重置ivec//memcpy(ivec, outputText, 8);  //解密过程不需要用前一块的结果作为下一块的IV}unsigned int mod=(datalen) % 8;if (mod != 0){unsigned int tmp1 = clearText.length() / 8 * 8;unsigned int tmp2 = clearText.length() - tmp1;memset(inputText,0, static_cast<size_t>(tmp2));memcpy(inputText, cipherText + tmp1, tmp2);DES_ncbc_encrypt(inputText, outputText, static_cast<long>(tmp2), &keySchedule, &ivec, DES_DECRYPT);  //解密memcpy(tmp, outputText, tmp2);for (unsigned int j = 0; j < mod; j++)vecCleartext.push_back(tmp[j]);}clearText.clear();clearText.assign(vecCleartext.begin(),vecCleartext.end());// 返回值去除末尾的填充值unsigned int retlen=clearText.length();unsigned int modlen=static_cast<unsigned int>(clearText[retlen-1]);if(modlen<=8&&modlen>0)clearText.erase(retlen-modlen,retlen);return clearText;
}std::string my_Des::decrypt(const std::string &cipherText,mypadmode_t mypadmode)
{switch (desmode) {case CBC:return padFUN(cipherText,mypadmode);}return  "";
}
std::string my_Des::decrypt(const std::string &cipherText)
{switch (desmode) {case CBC:return padFUN(cipherText,mypadmode);}return  "";
}std::string my_Des::padFUN(const std::string &cipherText,mypadmode_t mypadmode)
{switch (mypadmode) {case PAD_PKCS5:return des_cbc_pkcs5_decrypt(cipherText,key);}return  "";
}

rsa

#ifndef RSA_CLC_H
#define RSA_CLC_H#include<stdio.h>
#include <fstream>
#include <iostream>
#include <string>
#include <QString>#include <openssl/rsa.h>
#include<openssl/pem.h>
#include<openssl/err.h>
#include <openssl/bio.h>//服务端公钥用于本地加密
const static char* server_pulicKey ="-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBAKmV1KA9fjRrySfFbgozIDD0n19rL1pEUmM7wdho5qjl2w+wMlpxlqeO\n"
"YdbdMwtufbOekFQ4IcffORQfIynBpPeR12F5gQkC0sG1O8HkKdiDS88gcpVVKX1D\n"
"O77gW9p45Na5ZvajSYMo+4SIQeGWn5y8rCUfNfi3Vh/HeterMiHNAgMBAAE=\n"
"-----END RSA PUBLIC KEY-----";//客户端私钥用于服务端消息解码
const static char* clientPrivateKey ='-----BEGIN RSA PRIVATE KEY-----\n""MIICYQIBAAKBgQCSgVkZKHBVA/Fwdio4N2H1tjXTuwUW1ep9Vdzb3alBGJY575i2\n""N4mVxjvgCTyZjnj0s9QCA+I7mvFpKShTglHHdfEOvO+7T/p246Iq9VGfwstYfdTE\n""CNMiMzX1HdEfcZccEe65IJc8OfeoDHVPHvaGkxfU00kJA/rcdswJOn9hvQIDAQAB\n""AoGAMRs4ES2z0Vs7/1UsO6GcrS6BBlJGl2vmaFJycCPxSoeCOzMrPdCJxnEmuqZ9\n""8H6H3oW0R2Sj3RTHXFdWFi+Xg9PYOEiNg2/HrcvPBG0+/abreDpvIV3Xj6eqEK8i\n""WCS0aQqfehW+QIfITdU4jRI1XjDdbHDAHY+7/ZsKdtyq0cECRQC4aWst/K7N0GMZ\n""DTCx1KJNGTzII8fzPiCvkEaT/22UY/GH9muDVyXDgThwez3c+Wtax+TmuScOe06R\n""0J86bj8X/kIATQI9AMtg1TGuLSlr58PuSfZLe7psBsv1UBfaXde43mtte1jaVpyF\n""6i9Tprw4S+Y7hUqfUxbE6QL6anEpIyQfMQJFALRrPtcRHhxyQmHiC8AP3nI7vMG6\n""cAdEw0nAh6yH/DdvyIBS0EEBZArwQ72YdZ/ZzfRVGVe50cJTVT1HtGDAN65LrVsJ\n""AjxmhoQzLUkbDoih3yht/xN6oSaz/xDHVY/lRg7qPSDnY/oxodiyzwrI6+BnSi4h\n""tjveyJlmpPKv5TsP5KECRQCnQ4/U07F91Vr8X33x1LlT4h66SRZm+PRodaNw0gIS\n""Nps6pDX5CrA0FnLypO878JDTL0q/0mUx5otsfJo3IGF0jCaywQ==\n""-----END RSA PRIVATE KEY-----\n";using namespace std;
class Rsa_CLC
{
public:Rsa_CLC();QString rcvFromServer(string data);std::string sendToServer(QString data);
private:std::string Server_publicKey_jiami(string data);std::string Client_privateKey_jiemi(string data);
};#endif // RSA_CLC_H

Rsa_CLC.cpp

#include "Rsa_CLC.h"Rsa_CLC::Rsa_CLC()
{}
std::string Rsa_CLC::Client_privateKey_jiemi(string data)
{OpenSSL_add_all_algorithms();BIO* bp = BIO_new( BIO_s_mem() );BIO_puts(bp, clientPrivateKey);RSA* rsaK;rsaK = PEM_read_bio_RSAPrivateKey( bp, NULL, NULL, NULL );if (NULL == rsaK) {perror("read key file fail!");}else{//printf("read success!\n");}int nLen = RSA_size(rsaK);char *pEncode = new char[nLen + 1];int ret = RSA_private_decrypt(data.length(),(const unsigned char*)data.c_str(),(unsigned char *)pEncode,rsaK,RSA_PKCS1_PADDING);std::string strRet;if (ret >= 0) {strRet = std::string(pEncode, ret);}delete[] pEncode;CRYPTO_cleanup_all_ex_data();BIO_free_all( bp );RSA_free(rsaK);return strRet;
}std::string Rsa_CLC::Server_publicKey_jiami(string data)
{OpenSSL_add_all_algorithms();BIO* bp = BIO_new( BIO_s_mem() );BIO_puts(bp,server_pulicKey);RSA* rsaK = PEM_read_bio_RSAPublicKey( bp,NULL,NULL,NULL);if (NULL == rsaK) {return "";}int nLen = RSA_size(rsaK);char *pEncode = new char[nLen + 1];int ret = RSA_public_encrypt(data.length(),(const unsigned char*)data.c_str(),(unsigned char *)pEncode,rsaK,RSA_PKCS1_PADDING);std::string strRet;if (ret >= 0) {strRet = std::string(pEncode, ret);}delete[] pEncode;CRYPTO_cleanup_all_ex_data();BIO_free_all( bp );RSA_free(rsaK);return strRet;
}QString Rsa_CLC::rcvFromServer(string data)
{std::string rcvstr=Client_privateKey_jiemi(data);QString ret=QString::fromStdString(rcvstr);return ret;
}
std::string Rsa_CLC::sendToServer(QString data)
{std::string str=data.toStdString();std::string sendstr=Server_publicKey_jiami(str);return  sendstr;
}

rsa des加密解密,与c++混合使用. c++ 篇相关推荐

  1. 加密解密_使用RSA密钥对加密解密数据

    使用RSA密钥对加密解密数据 作者: 郭政鸿 2021/1/6 前言: 前几天看了非对称加密, 那非对称加密处理常见的https中的应用, 平时我们可以用来做什么呢? 1. 生成RSA密钥对 使用op ...

  2. getcoo php_PHP简单实现DES加密解密的方法

    本文实例讲述了PHP简单实现DES加密解密的方法.分享给大家供大家参考,具体如下: des加密: function des_encrypt($str, $key) { $block = mcrypt_ ...

  3. .net实现md5加密 sha1加密 sha256加密 sha384加密 sha512加密 des加密解密

    写项目时,后台一直用md5加密,一天群里人问,除了MD5还有其它的加密方法吗?当时只知道还有个SHA,但怎么实现什么的都不清楚,于是当网上找了下,把几种常见的加密方法都整理了下,用winform写了个 ...

  4. python des解密_python实现DES加密解密方法实例详解

    本文实例讲述了python实现DES加密解密方法.分享给大家供大家参考.具体分析如下: 实现功能:加密中文等字符串 密钥与明文可以不等长 这里只贴代码,加密过程可以自己百度,此处python代码没有优 ...

  5. java 实现 DES加密 解密算法

    DES算法的入口参数有三个:Key.Data.Mode.其中Key为8个字节共64位,是DES算法的工作密钥:Data也为8个字节64位,是要被加密或被解密的数据:Mode为DES的工作方式,有两种: ...

  6. DES加密解密算法Java实现

    DES 使用一个 56 位的密钥以及附加的 8 位奇偶校验位,产生最大 64 位的分组大小.这是一个迭代的分组密码,使用称为 Feistel 的技术,其中将加密的文本块分成两半.使用子密钥对其中一半应 ...

  7. DES加密解密与AES加密解密

    × 目录 [1]AES加密算法和DES加密算法的效率比较 [2]AES和DES加密解密代码 随着开发时间的变长,当初认为比较难的东西,现在渐渐也就变的不那么难了!特别对于一些经常很少使用的类,时间长了 ...

  8. java rsa 117_java实现RSA非对称加密解密

    之前写过一篇java实现AES对称加密解密 在对密码加密传输的场景下 RSA非对称加密解密可能会更加适合. 原理就是后台生成一对公钥和私钥,公钥给前端用来加密,后台用私钥去解密,保证了传输过程中就算被 ...

  9. 转载并学习实现三重DES加密解密代码(一)

    作者:finallyliuyu 出处:博客园 声明:此篇博文代码来自于邹德强先生.由于目前找到的版本是残缺版,所以我又进行了补全.读一份好代码,可以领略到作者的编程风格和语言驾驭能力,同时又能从其中汲 ...

最新文章

  1. ECCV2020 | 北京大学提出RGB-D语义分割新网络,多模态信息融合
  2. thinkphp查询
  3. EBS   常见的AD命令
  4. 点击页面元素,这个Vite插件竟然帮我打开了Vue组件文件!超级好用!
  5. bootstrap 学习网址
  6. 中山大学Delphi视频教程 共51课
  7. python与office结合可以干什么-震惊!当Python遇到Excel后,将开启你的认知虫洞
  8. XILINX FPGA数字信号处理——5、离散傅里叶变换原理及信号频谱分析实现
  9. 神经网络加速器设计研究:GoSPA ISCA2021论文研读
  10. 侧信道实验实验二 S盒DPA侧信道攻击
  11. [Bada开发]使用静态库
  12. 树的搜索问题1(深度优先、广度优先,爬山法和best-first)
  13. excel标题行列浮动显示/冻结窗口
  14. 【云原生 • Docker】Docker常用命令总结(值得收藏)
  15. 【网络工程师】<软考中级>网络互联与互联网
  16. 在Keil MDK中创建STM32F4系列的工程模板(标准外设库)
  17. 天气相关免费数据集下载
  18. (2021,StyleGAN3)无失真(Alias-Free)生成对抗网络
  19. maven -pl -am -amd 参数
  20. iOS安全攻防 防 防 防 防不住 . . . . . .

热门文章

  1. 撸一遍STM32最小系统板
  2. matlab匿名函数 函数句柄,matlab匿名函数函数句柄.docx
  3. JAVA基础之IDEA快捷键
  4. vue.runtime.esm.js:619 [Vue warn]: Duplicate keys detected: ‘/carsend‘. This may cause an update err
  5. 雅思培训心得(1)摸底测试听力与阅读
  6. K-means聚类算法、Pandas绘制概率密度图和TSNE展示聚类结果
  7. 载入动态的GIF 图片
  8. 4.1 画图抓图工具
  9. 计算机组装与维护试卷 中职,中职-2012《计算机组装与维护》期末考试试题
  10. 在AUTO CAD 2005中重新设置坐标原点的方法