本文共 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++是一个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操作就不起作用了。 转载: