Crypto++编译使用

简述

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

  • 简述
  • 下载
  • 使用
  • AES加解密
    • 效果
    • 源码
  • 更多参考

下载

进入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;
}

更多参考

时间: 2024-09-18 18:58:19

Crypto++编译使用的相关文章

Python Paramiko模块的安装与使用详解_python

一.前言 常见的解决方法都会需要对远程服务器必要的配置,如果远程服务器只有一两台还好说,如果有N台,还需要逐台进行配置,或者需要使用代码进行以上操作时,上面的办法就不太方便了.而使用paramiko可以很好的解决以上问题,比起前面的方法,它仅需要在本地上安装相应的软件(python以及PyCrypto),对远程服务器没有配置要求,对于连接多台服务器,进行复杂的连接操作特别有帮助.下面本文就来详细的介绍Python Paramiko模块的安装与使用,一起学习学习吧.. 二.安装 安装paramik

PHP编译安装时常见错误解决办法

  这篇文章主要介绍了PHP编译安装时常见错误解决办法,本文涵盖了PHP编译安装中的大多数错误,同时给出解决方法,需要的朋友可以参考下 This article is post on https://coderwall.com/p/ggmpfa configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution 代码如下: yum -y install libxslt-devel

CentOS 6.2编译安装Nginx1.0.14+MySQL5.5.22+PHP5.3.10步骤分享

说明: 操作系统:CentOS 6.2 32位 准备篇: 一.配置好IP.DNS .网关,确保使用远程连接工具能够连接服务器 二.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT(允许80端口通过防火墙) -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j

Linux 2.6.19.x 内核编译配置选项简介

Linux 2.6.19.x 内核编译配置选项简介 版权声明 本文作者是一位自由软件爱好者,所以本文虽然不是软件,但是本着 GPL 的精神发布.任何人都可以自由使用.转载.复制和再分发,但必须保留作者署名,亦不得对声明中的任何条款作任何形式的修改,也不得附加任何其它条件.您可以自由链接.下载.传播此文档,但前提是必须保证全文完整转载,包括完整的版权信息和作译者声明. 其他作品 本文作者十分愿意与他人共享劳动成果,如果你对我的其他翻译作品或者技术文章有兴趣,可以在如下位置查看现有作品的列表: 金步

密码类库Crypto++™ Library 5.1的研究与应用

在计算机被广泛应用的信息时代,信息本身就是时间,就是财富.大量信息用数据形式存放在计算机系统里.信息的传输则通过公共信道.这些计算机系统和公共信道是不设防的,是很脆弱的,容易受到攻击和破坏,信息的丢失不容易被发现,而且后果是极其严重.如何保护信息的安全已不仅仅是军事和政府部门感兴趣的问题,其他企事业单位也愈感迫切.因为在网络化的今天,计算机犯罪每年使他们遭受的损失极其巨大,而且还在发展中.密码是有效而且可行的保护信息安全的办法.随着计算机网络不断渗透到各个领域,密码学的应用也随着扩大.数字签名.

【区块链】Truffle 部署 编译 测试 智能合约 的 完整实践操作

Truffle 部署 编译 测试 智能合约 的 完整实践操作 目标 搭建开发环境 创建一个Truffle项目 编写智能合约 编译转移智能合约 测试智能合约 创建用户界面连接智能合约 在浏览器中访问Dapp 搭建开发环境 Node.js v6+ LTS and npm (comes with Node) Git 这里配置脚本略过.... xiaoyu@LIXIAOYUdeMacBook-Pro.com node -v v8.5.0 xiaoyu@LIXIAOYUdeMacBook-Pro.com

【原创】Tribler开发--BG Process的编译

遵照 http://www.tribler.org/trac/wiki/BrowserPlugin 中对SwarmPlugin的说明,首先需要编译出BG Process这个东东. 下载源码 svn co http://svn.tribler.org/abc/branches/mainbranch/,将源码放到你自己指定路径后执行  ? 1 2 3 4 5 C:\Python27>python.exe Tribler\Plugin\Build\Win32\setupBGexe.py py2exe

wpa_supplicant编译时出现 undefined reference to `sha256_vector&amp;amp;#39;

问题描述 wpa_supplicant编译时出现 undefined reference to `sha256_vector' CC ../src/drivers/drivers.c CC ../src/l2_packet/l2_packet_linux.c ../src/wps/wps_common.o: In function wps_derive_keys': /home/atheros/Atheros-P2P/wpa_supplicant-2.1/wpa_supplicant/../sr

nginx 编译错误解决方法 [备忘]

参见编译错误信息 can not detect int size 增加参数可以跳过 --with-ld-opt="-lstdc++" --with-cpp_test_module   参考编译错误信息 make[3]: Leaving directory `/apps/lib/openssl-1.0.0k/crypto' make[2]: Leaving directory `/apps/lib/openssl-1.0.0k' make[1]: *** [/apps/lib/opens