Crypto++学习
// MyTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iomanip>//setw与setfill头文件
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include "cryptlib.h"
#include "md5.h"
#include "filters.h"
#include "hex.h"
#include "base64.h"
#include "files.h"
#include "sha.h"
#include "crc.h"
#include "ripemd.h"
USING_NAMESPACE(CryptoPP)
USING_NAMESPACE(std)
int _tmain(int argc, _TCHAR *argv[])
{
/************************************************************************/
/* 散列算法 */
/************************************************************************/
/************************************************************************/
/* MD5 */
/************************************************************************/
printf("MD5开始\n");
byte message[] = "HelloWorld!";
int mesLen = strlen((char *)message - 1);
byte mres[16];//MD5 128 bits=16bytes
Weak::MD5 md5;
md5.Update(message, mesLen);
md5.Final(mres);
/************************************************************************/
/* md5转换到string */
/************************************************************************/
string digest3;
StringSource ss3(mres, sizeof(mres), true,
new HexEncoder(new StringSink(digest3))
);
cout << digest3 << endl;
printf("\n");
system("pause");
/************************************************************************/
/* md5转换到byte数组 */
/************************************************************************/
for(int i = 0; i < 16; i++)
printf("%02x", mres[i]);
printf("\n");
system("pause");
/************************************************************************/
/* 使用SecByteBlock */
/************************************************************************/
char *inputCh = "abcdefghijklmnopqrstuvwxyz";
const byte *input = (byte *)inputCh;
SecByteBlock digest(md5.DigestSize());
//md5.CalculateDigest(digest ,input, strlen(inputCh)); //此一句等于下面两句
md5.Update(input, strlen(inputCh));
md5.Final(digest);
for(int i = 0; i < md5.DigestSize(); i++)
printf("%02x", digest[i]);
printf("\n");
system("pause");
for (unsigned j = 0; j < md5.DigestSize(); j++)
cout << setw(2) << setfill('0') << hex << (int)digest[j];
printf("\n");
system("pause");
string src = "123";
string digest1;
/************************************************************************/
/* 另一种方法转换md5 */
/************************************************************************/
Weak::MD5 md51;
StringSource(src, true,
new HashFilter(md51,
new HexEncoder(new StringSink(digest1))
)
);
cout << digest1 << endl;
printf("\n");
system("pause");
/************************************************************************/
/* MD5对文件产生签名 */
/************************************************************************/
{
std::locale loc1 = std::locale::global(std::locale(".936")); //解决中文文件名问题
Weak::MD5 md5;
string filePath = "e:\\test.txt";
string digest4;
FileSource f(filePath.c_str(), true,
new HashFilter(
md5, new HexEncoder(new StringSink(digest4))
)
);
std::locale::global(std::locale(loc1));
cout << "file: " << digest4 << endl;
}
printf("\n");
system("pause");
printf("MD5结束\n");
/************************************************************************/
/* 二进制数组编码 */
/************************************************************************/
/************************************************************************/
/* 将一个byte数组按16进制编码后输出 */
/************************************************************************/
string encoded;
byte m[] = "HelloWorld!";
StringSource ss(m, sizeof(m), true,
new HexEncoder(new StringSink(encoded))
);
cout << encoded << endl;
system("pause");
/************************************************************************/
/* 将一个string按16进制编码后输出 */
/************************************************************************/
string encoded1;
string ms = "HelloWorld!";
StringSource ss1(ms, true,
new HexEncoder(new StringSink(encoded1))
);
cout << encoded1 << endl;
system("pause");
/************************************************************************/
/* 将一个byte数组按base64编码后输出 */
/************************************************************************/
string encoded2;
StringSource s(m, sizeof(m), true,
new Base64Encoder(new StringSink(encoded2))
);
cout << encoded2 << endl;
system("pause");
/************************************************************************/
/* SHA-1对字符串签名 */
/************************************************************************/
{
SHA1 sha1;
string digest5;
string src = "HelloWorld!";
StringSource(src, true,
new HashFilter(sha1,
new HexEncoder(new StringSink(digest5))
)
);
cout << "SHA-1: " << digest5 << endl;
system("pause");
string filePath = "e:\\test.txt";
string digest4;
FileSource f(filePath.c_str(), true,
new HashFilter(
sha1, new HexEncoder(new StringSink(digest4))
)
);
cout << "file: " << digest4 << endl;
system("pause");
}
{
SHA256 sha256;
string digest246;
string src = "HelloWorld!";
StringSource(src, true,
new HashFilter(sha256,
new HexEncoder(new StringSink(digest246))
)
);
cout << "SHA-256: " << digest246 << endl;
system("pause");
string filePath = "e:\\test.txt";
string digest4;
FileSource f(filePath.c_str(), true,
new HashFilter(
sha256, new HexEncoder(new StringSink(digest4))
)
);
cout << "file: " << digest4 << endl;
system("pause");
}
{
SHA384 sha384;
string digest384;
string src = "HelloWorld!";
StringSource(src, true,
new HashFilter(sha384,
new HexEncoder(new StringSink(digest384))
)
);
cout << "SHA-384: " << digest384 << endl;
system("pause");
string filePath = "e:\\test.txt";
string digest4;
FileSource f(filePath.c_str(), true,
new HashFilter(
sha384, new HexEncoder(new StringSink(digest4))
)
);
cout << "file: " << digest4 << endl;
system("pause");
}
{
SHA512 sha512;
string digest512;
string src = "HelloWorld!";
StringSource(src, true,
new HashFilter(sha512,
new HexEncoder(new StringSink(digest512))
)
);
cout << "SHA-512: " << digest512 << endl;
system("pause");
string filePath = "e:\\test.txt";
string digest4;
FileSource f(filePath.c_str(), true,
new HashFilter(
sha512, new HexEncoder(new StringSink(digest4))
)
);
cout << "file: " << digest4 << endl;
system("pause");
}
/************************************************************************/
/* CRC32跟MD5类似 */
/************************************************************************/
{
CRC32 crc;
string src = "HelloWorld!";
string digestcrc;
char *inputCh = "HelloWorld!";
const byte *input = (byte *)inputCh;
SecByteBlock digest(crc.DigestSize());
//md5.CalculateDigest(digest ,input, strlen(inputCh)); //此一句等于下面两句
crc.Update(input, strlen(inputCh));
crc.Final(digest);
cout << "CRC-32: ";
for(int i = 0; i < crc.DigestSize(); i++)
printf("%02x", digest[i]);
printf("\n");
StringSource(src, true,
new HashFilter(crc,
new HexEncoder(new StringSink(digestcrc))
)
);
cout << "CRC-32: " << digestcrc << endl;
printf("\n");
system("pause");
string filePath = "e:\\test.txt";
string digest4;
FileSource f(filePath.c_str(), true,
new HashFilter(
crc, new HexEncoder(new StringSink(digest4))
)
);
cout << "file: " << digest4 << endl;
system("pause");
}
/************************************************************************/
/* RIPEMD160 */
/************************************************************************/
{
RIPEMD160 md160;
string src = "HelloWorld!";
string digestcrc;
char *inputCh = "HelloWorld!";
const byte *input = (byte *)inputCh;
SecByteBlock digest(md160.DigestSize());
//md5.CalculateDigest(digest ,input, strlen(inputCh)); //此一句等于下面两句
md160.Update(input, strlen(inputCh));
md160.Final(digest);
cout << "RIPEMD-160: ";
for(int i = 0; i < md160.DigestSize(); i++)
printf("%02x", digest[i]);
printf("\n");
StringSource(src, true,
new HashFilter(md160,
new HexEncoder(new StringSink(digestcrc))
)
);
cout << "RIPEMD-160: " << digestcrc << endl;
printf("\n");
system("pause");
string filePath = "e:\\test.txt";
string digest4;
FileSource f(filePath.c_str(), true,
new HashFilter(
md160, new HexEncoder(new StringSink(digest4))
)
);
cout << "file: " << digest4 << endl;
system("pause");
}
/************************************************************************/
/* 对称加密算法 DES AES */
/************************************************************************/
/************************************************************************/
/* DES ValidateDES(); */
/************************************************************************/
/************************************************************************/
/* AES ValidateRijndael(); */
/************************************************************************/
/************************************************************************/
/* 非对称加密算法 */
/************************************************************************/
/************************************************************************/
/* RSA GenerateRSAKey、RSAEncryptString、RSADecryptString、RSASignFile、RSAVerifyFile */
/************************************************************************/
system("pause");
//http://www.cppblog.com/ArthasLee/archive/2010/12/01/135186.html
return 0;
}
ChannelSwitch
#include <iostream>
#include <cstring>
#include <cryptopp/channels.h>
#include <cryptopp/filters.h>
#include <cryptopp/sha.h>
#include <cryptopp/hex.h>
int main(int argc, char *argv[]) {
std::string message = "Now is the time for all good men to come to the aide of their country";
// Allow user to override default message from command line arg.
if(argc == 2 && argv[1] != NULL)
message = std::string(argv[1]);
// Set hash variables
std::string s1, s2, s3, s4;
CryptoPP::SHA1 sha1; CryptoPP::SHA224 sha224; CryptoPP::SHA256 sha256; CryptoPP::SHA512 sha512;
// Run hash functions
CryptoPP::HashFilter f1(sha1, new CryptoPP::HexEncoder(new CryptoPP::StringSink(s1)));
CryptoPP::HashFilter f2(sha224, new CryptoPP::HexEncoder(new CryptoPP::StringSink(s2)));
CryptoPP::HashFilter f3(sha256, new CryptoPP::HexEncoder(new CryptoPP::StringSink(s3)));
CryptoPP::HashFilter f4(sha512, new CryptoPP::HexEncoder(new CryptoPP::StringSink(s4)));
// Set route to default
CryptoPP::ChannelSwitch cs;
cs.AddDefaultRoute(f1);
cs.AddDefaultRoute(f2);
cs.AddDefaultRoute(f3);
cs.AddDefaultRoute(f4);
CryptoPP::StringSource ss(message, true /*pumpAll*/, new CryptoPP::Redirector(cs));
std::cout << "Message: " << message << std::endl;
std::cout << "SHA-1: " << s1 << std::endl;
std::cout << "SHA-224: " << s2 << std::endl;
std::cout << "SHA-256: " << s3 << std::endl;
std::cout << "SHA-512: " << s4 << std::endl;
}
文档:
- https://www.cryptopp.com/docs/ref/index.html
- https://www.cryptopp.com/wiki/Main_Page
- https://www.cryptopp.com/wiki/Special:AllPages
- https://www.cryptopp.com/wiki/Category:Class
-https://www.cryptopp.com/wiki/Category:Sample - https://www.cryptopp.com/wiki/ChannelSwitch
- https://www.cryptopp.com/wiki/Recommended_Books