首页 > 其他分享 >Crypto++编译使用

Crypto++编译使用

时间:2022-11-08 15:12:47浏览次数:61  
标签:std CBC EncryptedText string decryptor ++ Crypto 编译 key

简述

Crypto++库是一个用c++ 编写的密码类库,是一个自由软件。有关它的信息可以访问以下两个网站:

 

 

下载

进入Crypto++主页,下载对应的版本(我下载的是最新的:Crypto++ 5.6.3

解压缩之后,我们会看到里面包含大量的头文件、源文件、以及工程文件,用VS2013打开cryptest.sln,然后进行编译(清理->重新生成)即可。

编译完成,会生成cryptest.exe、cryptlib.lib(E:\Crypto++5.6.3\Win32\Output\Debug)。

使用

新建一个Win32控制台应用程序TestCrypto++。

  1. 右键“属性”,选择“配置属性”-> C/C++ ->常规,附加包含目录E:\Crypto++5.6.3
  2. 右键“属性”,选择“配置属性”-> 链接器 ->常规,附加库目录E:\Crypto++5.6.3\Win32\Output\Debug
  3. 右键“属性”,选择“配置属性”-> C/C++ -> 代码生成,运行库选择“多线程调试(/MTd)”(Release模式下对应“多线程(/MT)”)。

此时,使用Crypto++的开发环境就搭建好了,在Crypto++ User Guide中的例子。

AES加解密

Ok,编写测试程序。。。

效果

这里写图片描述

源码

// TestCrypto++.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include <aes.h>  
#include <Hex.h>      // StreamTransformationFilter  
#include <modes.h>    // CFB_Mode  
#include <iostream>   // std:cerr    
#include <sstream>   // std::stringstream    
#include <string>  

using namespace std;
using namespace CryptoPP;
#pragma comment(lib, "cryptlib.lib" )  

std::string ECB_AESEncryptStr(std::string sKey, const char *plainText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);


    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);

    ECB_Mode_ExternalCipher::Encryption ecbEncryption(aesEncryption);
    StreamTransformationFilter ecbEncryptor(ecbEncryption, new HexEncoder(new StringSink(outstr)));
    ecbEncryptor.Put((byte *)plainText, strlen(plainText));
    ecbEncryptor.MessageEnd();

    return outstr;
}

std::string ECB_AESDecryptStr(std::string sKey, const char *cipherText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

    ECB_Mode<AES >::Decryption ecbDecryption((byte *)key, AES::MAX_KEYLENGTH);

    HexDecoder decryptor(new StreamTransformationFilter(ecbDecryption, new StringSink(outstr)));
    decryptor.Put((byte *)cipherText, strlen(cipherText));
    decryptor.MessageEnd();

    return outstr;
}

std::string CBC_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

    //填iv    
    byte iv[AES::BLOCKSIZE];
    memset(iv, 0x30, AES::BLOCKSIZE);
    sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);

    CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);

    StreamTransformationFilter cbcEncryptor(cbcEncryption, new HexEncoder(new StringSink(outstr)));
    cbcEncryptor.Put((byte *)plainText, strlen(plainText));
    cbcEncryptor.MessageEnd();

    return outstr;
}

std::string CBC_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

    //填iv    
    byte iv[AES::BLOCKSIZE];
    memset(iv, 0x30, AES::BLOCKSIZE);
    sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);


    CBC_Mode<AES >::Decryption cbcDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);

    HexDecoder decryptor(new StreamTransformationFilter(cbcDecryption, new StringSink(outstr)));
    decryptor.Put((byte *)cipherText, strlen(cipherText));
    decryptor.MessageEnd();

    return outstr;
}

std::string CBC_CTS_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

    //填iv    
    byte iv[AES::BLOCKSIZE];
    memset(iv, 0x30, AES::BLOCKSIZE);
    sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);

    CBC_CTS_Mode_ExternalCipher::Encryption cbcctsEncryption(aesEncryption, iv);

    StreamTransformationFilter cbcctsEncryptor(cbcctsEncryption, new HexEncoder(new StringSink(outstr)));
    cbcctsEncryptor.Put((byte *)plainText, strlen(plainText));
    cbcctsEncryptor.MessageEnd();

    return outstr;
}

std::string CBC_CTS_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

    //填iv    
    byte iv[AES::BLOCKSIZE];
    memset(iv, 0x30, AES::BLOCKSIZE);
    sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);


    CBC_CTS_Mode<AES >::Decryption cbcctsDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);

    HexDecoder decryptor(new StreamTransformationFilter(cbcctsDecryption, new StringSink(outstr)));
    decryptor.Put((byte *)cipherText, strlen(cipherText));
    decryptor.MessageEnd();

    return outstr;
}

std::string CFB_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

    //填iv    
    byte iv[AES::BLOCKSIZE];
    memset(iv, 0x30, AES::BLOCKSIZE);
    sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);

    CFB_Mode_ExternalCipher::Encryption cfbEncryption(aesEncryption, iv);

    StreamTransformationFilter cfbEncryptor(cfbEncryption, new HexEncoder(new StringSink(outstr)));
    cfbEncryptor.Put((byte *)plainText, strlen(plainText));
    cfbEncryptor.MessageEnd();

    return outstr;
}

std::string CFB_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

    //填iv    
    byte iv[AES::BLOCKSIZE];
    memset(iv, 0x30, AES::BLOCKSIZE);
    sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);


    CFB_Mode<AES >::Decryption cfbDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);

    HexDecoder decryptor(new StreamTransformationFilter(cfbDecryption, new StringSink(outstr)));
    decryptor.Put((byte *)cipherText, strlen(cipherText));
    decryptor.MessageEnd();

    return outstr;
}

std::string OFB_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

    //填iv    
    byte iv[AES::BLOCKSIZE];
    memset(iv, 0x30, AES::BLOCKSIZE);
    sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);

    OFB_Mode_ExternalCipher::Encryption ofbEncryption(aesEncryption, iv);

    StreamTransformationFilter ofbEncryptor(ofbEncryption, new HexEncoder(new StringSink(outstr)));
    ofbEncryptor.Put((byte *)plainText, strlen(plainText));
    ofbEncryptor.MessageEnd();

    return outstr;
}

std::string OFB_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

    //填iv    
    byte iv[AES::BLOCKSIZE];
    memset(iv, 0x30, AES::BLOCKSIZE);
    sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);


    OFB_Mode<AES >::Decryption ofbDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);

    HexDecoder decryptor(new StreamTransformationFilter(ofbDecryption, new StringSink(outstr)));
    decryptor.Put((byte *)cipherText, strlen(cipherText));
    decryptor.MessageEnd();

    return outstr;
}

std::string CTR_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

    //填iv    
    byte iv[AES::BLOCKSIZE];
    memset(iv, 0x30, AES::BLOCKSIZE);
    sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);

    CTR_Mode_ExternalCipher::Encryption ctrEncryption(aesEncryption, iv);

    StreamTransformationFilter ctrEncryptor(ctrEncryption, new HexEncoder(new StringSink(outstr)));
    ctrEncryptor.Put((byte *)plainText, strlen(plainText));
    ctrEncryptor.MessageEnd();

    return outstr;
}

std::string CTR_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)
{
    std::string outstr;

    //填key    
    SecByteBlock key(AES::MAX_KEYLENGTH);
    memset(key, 0x30, key.size());
    sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

    //填iv    
    byte iv[AES::BLOCKSIZE];
    memset(iv, 0x30, AES::BLOCKSIZE);
    sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);


    CTR_Mode<AES >::Decryption ctrDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);

    HexDecoder decryptor(new StreamTransformationFilter(ctrDecryption, new StringSink(outstr)));
    decryptor.Put((byte *)cipherText, strlen(cipherText));
    decryptor.MessageEnd();

    return outstr;
}

int _tmain(int argc, _TCHAR* argv[])
{
    string plainText = "This Program shows how to use ECB, CBC, CBC_CTS, CFB, OFB and CTR mode of AES in Crypto++.";
    string aesKey = "0123456789ABCDEF0123456789ABCDEF";//256bits, also can be 128 bits or 192bits  
    string aesIV = "ABCDEF0123456789";//128 bits  
    string ECB_EncryptedText, ECB_DecryptedText,
        CBC_EncryptedText, CBC_DecryptedText,
        CBC_CTS_EncryptedText, CBC_CTS_DecryptedText,
        CFB_EncryptedText, CFB_DecryptedText,
        OFB_EncryptedText, OFB_DecryptedText,
        CTR_EncryptedText, CTR_DecryptedText;

    //ECB  
    ECB_EncryptedText = ECB_AESEncryptStr(aesKey, plainText.c_str());//ECB加密  
    ECB_DecryptedText = ECB_AESDecryptStr(aesKey, ECB_EncryptedText.c_str());//ECB解密  

    //CBC  
    CBC_EncryptedText = CBC_AESEncryptStr(aesKey, aesIV, plainText.c_str());//CBC加密  
    CBC_DecryptedText = CBC_AESDecryptStr(aesKey, aesIV, CBC_EncryptedText.c_str());//CBC解密  

    //CBC_CTS  
    CBC_CTS_EncryptedText = CBC_CTS_AESEncryptStr(aesKey, aesIV, plainText.c_str());//CBC_CTS加密  
    CBC_CTS_DecryptedText = CBC_CTS_AESDecryptStr(aesKey, aesIV, CBC_CTS_EncryptedText.c_str());//CBC_CTS解密  

    //CFB  
    CFB_EncryptedText = CFB_AESEncryptStr(aesKey, aesIV, plainText.c_str());//CFB加密  
    CFB_DecryptedText = CFB_AESDecryptStr(aesKey, aesIV, CFB_EncryptedText.c_str());//CFB解密  

    //OFB  
    OFB_EncryptedText = OFB_AESEncryptStr(aesKey, aesIV, plainText.c_str());//OFB加密  
    OFB_DecryptedText = OFB_AESDecryptStr(aesKey, aesIV, OFB_EncryptedText.c_str());//OFB解密  

    //CTR  
    CTR_EncryptedText = CTR_AESEncryptStr(aesKey, aesIV, plainText.c_str());//CTR加密  
    CTR_DecryptedText = CTR_AESDecryptStr(aesKey, aesIV, CTR_EncryptedText.c_str());//CTR解密  

    cout << "Crypto++ AES-256 加密测试" << endl;
    cout << "分别使用ECB,CBC, CBC_CTR,CFB,OFB和CTR模式" << endl;
    cout << "加密用密钥:" << aesKey << endl;
    cout << "密钥长度:" << AES::MAX_KEYLENGTH * 8 << "bits" << endl;
    cout << "IV:" << aesIV << endl;
    cout << endl;

    cout << "ECB测试" << endl;
    cout << "原文:" << plainText << endl;
    cout << "密文:" << ECB_EncryptedText << endl;
    cout << "恢复明文:" << ECB_DecryptedText << endl << endl;

    cout << "CBC测试" << endl;
    cout << "原文:" << plainText << endl;
    cout << "密文:" << CBC_EncryptedText << endl;
    cout << "恢复明文:" << CBC_DecryptedText << endl << endl;

    cout << "CBC_CTS测试" << endl;
    cout << "原文:" << plainText << endl;
    cout << "密文:" << CBC_CTS_EncryptedText << endl;
    cout << "恢复明文:" << CBC_CTS_DecryptedText << endl << endl;

    cout << "CFB测试" << endl;
    cout << "原文:" << plainText << endl;
    cout << "密文:" << CFB_EncryptedText << endl;
    cout << "恢复明文:" << CFB_DecryptedText << endl << endl;

    cout << "OFB测试" << endl;
    cout << "原文:" << plainText << endl;
    cout << "密文:" << OFB_EncryptedText << endl;
    cout << "恢复明文:" << OFB_DecryptedText << endl << endl;

    cout << "CTR测试" << endl;
    cout << "原文:" << plainText << endl;
    cout << "密文:" << CTR_EncryptedText << endl;
    cout << "恢复明文:" << CTR_DecryptedText << endl << endl;

    getchar();
    return 0;
}

 

更多参考

 

http://blog.csdn.net/u011012932/article/details/51659452

标签:std,CBC,EncryptedText,string,decryptor,++,Crypto,编译,key
From: https://www.cnblogs.com/lidabo/p/16869771.html

相关文章

  • 《数据结构与算法分析(C++语言描述)》
    在看这本书总结了笔记,并分享出来。有问题请及时联系博主:​​Alliswell_WP​​,转载请注明出处。书籍:《数据结构与算法分析(C++语言描述)》作者:LarryNyhoff著、黄达明等译源代......
  • Eclipse_2022_09 版本 反编译插件
      eclipse包地址:https://www.eclipse.org/downloads/packages/截图如下:  最新版的eclipse中自带class反编译功能。  可自行选择对应的反编译插件类型 ......
  • C++学习
     平时有时间也可以刷一刷​​leetcode​​​,​​hihocoder​​,很多题目思考起来还是很有意思的。对于其他的课本,我觉得《算法导论》《编程珠玑》《编程之美》都是不错的经......
  • C/C++ 编程
    写一个函数找出一个整数数组中,第二大的数 constintMINNUMBER=-32767;intfind_sec_max(intdata[],intcount){intmaxnumber=data[0];i......
  • Xmake v2.7.3 发布,包组件和 C++ 模块增量构建支持
    Xmake是一个基于Lua的轻量级跨平台构建工具。它非常的轻量,没有任何依赖,因为它内置了Lua运行时。它使用xmake.lua维护项目构建,相比makefile/CMakeLists.txt,配置语......
  • pip error: Microsoft Visual C++ 14.0 or greater is required
    https://zhuanlan.zhihu.com/p/471661231去官网下载cpp编译工具:https://my.visualstudio.com/Downloads/Featured?mkt=zh-cn下载后直接安装默认值比起有的教程安装......
  • C++ 不知图系列之基于邻接矩阵实现广度、深度搜索
    1.前言图是一种抽象数据结构,本质和树结构是一样的。图与树相比较,图具有封闭性,可以把树结构看成是图结构的基础部件。在树结构中,如果把兄弟节点之间或子节点之间横向连接,......
  • 二叉搜索树 - C++ 实现
    二叉搜索树-C++实现......
  • 脚本-交叉编译bash5.0
    #!/bin/bash#通用交叉编译脚本##build编译环境#host编译出的程序的运行环境#target编译出的程序(编译器)编译出来的程序的运行环境#一般不需要PATH_NAME="b......
  • VC++_读取SHELL命令结果
    voidCFfmpeg_divide_videoDlg::OnButtonBeginDivide(){SECURITY_ATTRIBUTESsa;HANDLEhRead,hWrite;sa.nLength=sizeof(SECURITY_ATTRIBUTES);sa.lpSecurityDe......