博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Crypto++ 加/解密算法库
阅读量:4166 次
发布时间:2019-05-26

本文共 5511 字,大约阅读时间需要 18 分钟。

编译 Crypto++ cryptlib 适合VC6 VC7 VC8 VC9 VC10

Crypto++ Library is a free C++ class library of cryptographic schemes.

可以到下面的网址下载最新源代码:

 

Crypto++ Library 是开源的、跨平台的C++, 提供丰富的加密解密算法,包括:MD5,IDEA, Triple-DES,AES (Rijndael), RC6, MARS, Twofish, Serpent, RSA, DSA, SHA-1, SHA-2 等等。

 

支持的编译器如下:

  * MSVC 6.0 - 2010

  * GCC 3.3 - 4.5
  * C++Builder 2010
  * Intel C++ Compiler 9 - 11.1
  * Sun Studio 12u1, Express 11/08, Express 06/10

 

==============================

 

这里简单说明一下使用 MSVC2008 对Crypto++进行编译使用的方法和注意事项, 希望对大家开始学习有帮助!

下面一段的英文比较简单, 说明了Crypto++包含的工程情况, 以及使用注意事项, 一看就清楚:

 

On Windows, Crypto++ can be compiled into 3 forms:a static library

including all algorithms, a DLL with only FIPS Approved algorithms, and
a static library with only algorithms not in the DLL.

 

 

To compile Crypto++ with MSVC, open the "cryptest.dsw" (for MSVC 6 and MSVC .NET

2003) or "cryptest.sln" (for MSVC 2005 - 2010) workspace file and build one or
more of the following projects:

 

//用于生成包含所有算法的静态库

cryptlib - a static libraryincluding all algorithms

 

//用于生成仅包含FIPS作为标准的算法的dll和导入库

cryptopp - This builds the DLL. Please note that if you wish to use Crypto++

  as a FIPS validated module, you must use a pre-built DLL that has undergone
  the FIPS validation process instead of building your own.
 

//基于dll开发的例子程序

dlltest - This builds a sample application that only uses the DLL.

 

//基于cryptlib静态库开发的测试驱动程序

cryptest Non-DLL-Import Configuration - This builds the full static library

  along with a full test driver.
 

//基于dll 和不包含FIPS法静态库开发的测试驱动程序

 

 

cryptest DLL-Import Configuration - This builds a static library containing

  only algorithms not in the DLL, along with a full test driver that uses
  both the DLL and the static library.

 

To use the Crypto++ DLL in your application, #include "dll.h" before including

any other Crypto++ header files, and place the DLL in the same directory as
your .exe file. dll.h includes the line #pragma comment(lib, "cryptopp")
so you don't have to explicitly list the import library in your project
settings.

 

To use a static library form of Crypto++, make the "cryptlib"

project a dependency of your application project, or specify it as
an additional library to link with in your project settings.
In either case you should check the compiler options to
make sure that the library and your application are using the same C++
run-time libraries and calling conventions.

 

名词解释:

FIPS : Federal Information Processing Standards   (美国)联邦信息处理标准

 

 

================================================================

crypto++ Crypto++是一个C++编写的密码学类库,md5 AES DES 所有密码加密的算法都可找到,跨平台

Crypto++是一个C++编写的密码学类库。读过《过河卒》的朋友还记得作者的那个不愿意去微软工作的儿子吗,就是Crypto++的作者Wei Dai。

Crypto++是一个非常强大的密码学库,在密码学界很受欢迎,最初还是Rivest(RSA的R)门下的一个博士姐姐把这个库介绍给我的。虽然网络上可以找到很多密码学相关的代码和库,但是Crypto++有其明显的优点。主要是功能全,统一性好。例如椭圆曲线加密算法和AES在OpenSSL的crypto库中就还没最终完成,而在Crypto++中就支持的比较好,基本上密码学中需要的主要功能都可以在里面找得到。Crypto++是由标准的C++写成的,学习C++、密码学、网络安全都可以通过阅读Crypto++的源代码得到启发和提高。
Crypto++的安装
首先到上下载最新版本的源代码,如果是windows版的,会得到一个VC的项目,直接用VC打开就可以编译了。这里建议大家使用最新版的C++编译器,因为诸如VC6的编译器是不支持C++的标准的,很多符合C++标准的代码不能编译通过。编译的时间比较长,完成后会生成cryptlib.lib这个库文件。可以将Crypto++源文件的目录命名为cryptopp,拷贝到编译器的include目录(例如:C:/VS.NET/VC7/include),将cryptlib.lib文件拷贝到编译器的lib目录。这样我们只需要说明链接cryptlib.lib即可。例如在VC7中在项目->属性->链接器->命令行->附加选项中添加“cryptlib.lib”。
Hello World
现在写一个hello world程序看看能不能编译通过。

#include <iostream>

using namespace std;
#include <cryptopp/aes.h>
using namespace CryptoPP;

int main()

{
       cout << "hello crypto++" << endl;
       cout << "Aes block size is " << AES::BLOCKSIZE << endl;

 return 0;

}

编译运行,一切OK,哈哈:D,可以用了。

 

 

Crypto++首页上提供了Crypto++ User Guide这个入门指南的链接,这本指南属于一定要看的资料,不过可惜的是这也是找到的唯一一本指南了:(。User Guide上面的有一些例子,其中对AES用法的说明不太直接,而AES之类对称加密算法又比较常用,所以我这里写了一个AES的例子。

例子是直接用AES加密一个块,AES的数据块大小为128位,密钥长度可选择128位、192位或256位。直接用AES加密一个块很少用,因为我们平常都是加密任意长度的数据,需要选择CFB等加密模式。但是直接的块加密是对称加密的基础。

#include <iostream>
using namespace std;

#include <cryptopp/aes.h>

using namespace CryptoPP;

int main()

{

        //AES中使用的固定参数是以类AES中定义的enum数据类型出现的,而不是成员函数或变量

        //因此需要用::符号来索引

        cout << "AES Parameters: " << endl;

        cout << "Algorithm name : " << AES::StaticAlgorithmName() << endl;     

        //Crypto++库中一般用字节数来表示长度,而不是常用的字节数

        cout << "Block size     : " << AES::BLOCKSIZE * 8 << endl;
        cout << "Min key length : " << AES::MIN_KEYLENGTH * 8 << endl;
        cout << "Max key length : " << AES::MAX_KEYLENGTH * 8 << endl;

        //AES中只包含一些固定的数据,而加密解密的功能由AESEncryption和AESDecryption来完成

        //加密过程
        AESEncryption aesEncryptor; //加密器

 

        unsigned char aesKey[AES::DEFAULT_KEYLENGTH];                   //密钥

        unsigned char inBlock[AES::BLOCKSIZE] = "123456789";    //要加密的数据块

        unsigned char outBlock[AES::BLOCKSIZE];                                 //加密后的密文块

        unsigned char xorBlock[AES::BLOCKSIZE];                                 //必须设定为全零

        memset( xorBlock, 0, AES::BLOCKSIZE );                                 //置零

 

        aesEncryptor.SetKey( aesKey, AES::DEFAULT_KEYLENGTH );  //设定加密密钥

        aesEncryptor.ProcessAndXorBlock( inBlock, xorBlock, outBlock ); //加密

 

        //以16进制显示加密后的数据

        for( int i=0; i<16; i++ ) {

                cout << hex << (int)outBlock[i] << " ";

        }

        cout << endl;

 

        //解密

        AESDecryption aesDecryptor;

        unsigned char plainText[AES::BLOCKSIZE];

        aesDecryptor.SetKey( aesKey, AES::DEFAULT_KEYLENGTH );

        aesDecryptor.ProcessAndXorBlock( outBlock, xorBlock, plainText );

 

        for( int i=0; i<16; i++ ) {      cout << plainText[i];    }

        cout << endl;
        return 0;
}

这里面有几个地方要注意一下:
AES并不是一个类,而是类Rijndael的一个typedef。
Rijndael虽然是一个类,但是其用法和namespace很像,本身没有什么成员函数和成员变量,只是在类体里面定义了一系列的类和数据类型(enum),真正能够进行加密解密的AESEncryption和AESDecryption都是定义在这个类内部的类。
AESEncryption和AESDecryption除了可以用SetKey()这个函数设置密钥,在构造函数中也能设置密钥,参数和SetKey()是一样的。
ProcessAndXorBlock()可能会让人比较疑惑,函数名的意思是ProcessBlock and XorBlock,ProcessBlock就是对块进行加密或解密,XorBlock在各种加密模式中有用,这里我们不需要应用模式,因此把用来Xor操作的xorBlock置为0,那么Xor操作就不起作用了。

转载:

你可能感兴趣的文章
介绍一下海量数据的处理方法
查看>>
什么是构架设计图 ?有哪些组成?
查看>>
软件系统的架构(ArchitECture)有两个要素是什么?
查看>>
什么是非侵入式设计?
查看>>
可遇见框架技术之面试问题
查看>>
系统设计类面试题
查看>>
架构师的职责都有哪些?
查看>>
看女程序员是怎么坑大师兄的, 网友: 真的惨,笑死我了!
查看>>
C/C++程序员面试基础知识(一)
查看>>
程序员提离职遭领导威胁,一线企业总监我都认识,我让你混不下去
查看>>
朝九晚六吊打互联网企业,程序员:又开始无脑吹国企了!
查看>>
网友话数万元转行程序员,但是却没人要,网友:是学历问题吗?
查看>>
程序员辞掉30W年薪接私活:6个月就能赚回30W,庆幸自己当初辞职
查看>>
马云四天三谈996被骂上热搜:抱歉,这届年轻人不好“骗”了!
查看>>
中国牛逼的程序员有哪些?入职华为两天转正,半个月升主任
查看>>
为什么大学里的计算机老师,不去大公司当程序员呢?
查看>>
eclipse的java facets的runtimes如何删除tomcat?
查看>>
oracle序列的cache_size说明
查看>>
oracle中两个时间戳相减得到间隔毫秒数
查看>>
Oracle中将毫秒数转换为timestamp类型的两种方法
查看>>