cryptoo sha256 hmac

// AES_LibGcrypt_Test_1.cpp : Defines the entry point for the console application.
//




#include "stdafx.h"
#include <stdio.h>  
#include "cryptopp\aes.h"
#include "cryptopp\filters.h"
#include "cryptopp\modes.h"
#include "cryptopp\cryptlib.h"
#include "cryptopp\secblock.h"
#include "cryptopp\osrng.h"
#include "cryptopp\files.h"
#include "cryptopp\cmac.h"
#include "cryptopp\aes.h"
#include "cryptopp\hex.h"
#include "cryptopp\sha.h"
#include "cryptopp\hmac.h"
#include "cryptopp\base64.h"
using namespace CryptoPP;
#include <iostream>
#include <string>


#pragma comment(lib, "E:\\VI Code\\code\\cryptopp\\x64\\Output\\Debug\\cryptlib.lib")


using namespace std;
string sign(string key, string plain) {
string mac, encoded;
try {
HMAC< SHA256 > hmac((byte*)key.c_str(), key.length());


StringSource(plain, true,
new HashFilter(hmac,
new StringSink(mac)
) // HashFilter      
); // StringSource
} catch (const CryptoPP::Exception& e) {
cerr << e.what() << endl;
}
const char *buf = mac.c_str();
int i;
for (i = 0; i < mac.size(); i++) {
printf("%02X,",(byte)buf[i]);
}
encoded.clear();
StringSource(mac, true,
new Base64Encoder(
new StringSink(encoded)
) // Base64Encoder
); // StringSource


return encoded;
}
int main(int argc, char* argv[]) {
string data = "0ZKDEC70010043";
string key = "qweasdzxcrfvtgbyhnujmiopkl";
string ret = sign(key, data);
cout << ret << endl;
return 0;
}


int main2(int argc, char* argv[]) {
//
//var key = new Buffer('f63834aac44c0e201d9c952f4c865db3', 'hex');
//var message = 'GPSDK200521111';
AutoSeededRandomPool prng;


SecByteBlock key(AES::DEFAULT_KEYLENGTH);
//prng.GenerateBlock(key, key.size());

int arrlen;
char arr[] = { 0xf6, 0x38, 0x34, 0xaa, 0xc4, 0x4c, 0x0e, 0x20, 0x1d, 0x9c, 0x95, 0x2f, 0x4c, 0x86, 0x5d, 0xb3 };
int i;
key.Assign((byte*)arr, 16);
string mac, plain = "GPSDK200521111";
HexEncoder encoder(new FileSink(cout));


/*********************************\
\*********************************/


// Pretty print key
cout << "key: ";
encoder.Put(key, key.size());
encoder.MessageEnd();
cout << endl;


cout << "plain text: ";
encoder.Put((const byte*)plain.data(), plain.size());
encoder.MessageEnd();
cout << endl;


/*********************************\
\*********************************/


try {
CMAC<AES> cmac(key.data(), key.size());
cmac.Update((const byte*)plain.data(), plain.size());


mac.resize(cmac.DigestSize());
cmac.Final((byte*)&mac[0]);
} catch (const CryptoPP::Exception& e) {
cerr << e.what() << endl;
exit(1);
}


/*********************************\
\*********************************/


// Pretty print
cout << "cmac: ";
encoder.Put((const byte*)mac.data(), mac.size());
encoder.MessageEnd();
cout << endl;


/*********************************\
\*********************************/


// Verify
try {
CMAC<AES> cmac(key.data(), key.size());
cmac.Update((const byte*)plain.data(), plain.size());


// Call Verify() instead of Final()
bool verified = cmac.Verify((byte*)&mac[0]);
if (!verified)
throw Exception(Exception::DATA_INTEGRITY_CHECK_FAILED, "CMAC: message MAC not valid");


cout << "Verified message MAC" << endl;
} catch (const CryptoPP::Exception& e) {
cerr << e.what() << endl;
exit(1);
}


return 0;
}






/*
byte key[CryptoPP::AES::DEFAULT_KEYLENGTH], iv[CryptoPP::AES::BLOCKSIZE];
void initKV() {
memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH);
int i;
for (i = 0; i < CryptoPP::AES::DEFAULT_KEYLENGTH; i++) {}
memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);


// 或者也可以  
/*
char tmpK[] = "1234567890123456";
char tmpIV[] = "1234567890123456";
for (int j = 0; j < CryptoPP::AES::DEFAULT_KEYLENGTH; ++j)
{
key[j] = tmpK[j];
}


for (int i = 0; i < CryptoPP::AES::BLOCKSIZE; ++i)
{
iv[i] = tmpIV[i];
}
*/
/*
}
string encrypt(string plainText) {
string cipherText;


//  
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(cipherText));
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plainText.c_str()), plainText.length() + 1);
stfEncryptor.MessageEnd();
cout << "cipher text: " << cipherText << endl;
string cipherTextHex;
for (int i = 0; i < cipherText.size(); i++) {
char ch[3] = { 0 };
sprintf_s(ch, 3, "%02x", static_cast<byte>(cipherText[i]));
cipherTextHex += ch;
}


return cipherTextHex;
}






void writeCipher(string output) {
ofstream out("E:\\cipher.data");
out.write(output.c_str(), output.length());
out.close();


cout << "writeCipher finish " << endl << endl;
}


string decrypt(string cipherTextHex) {
string cipherText;
string decryptedText;


int i = 0;
while (true) {
char c;
int x;
stringstream ss;
ss << hex << cipherTextHex.substr(i, 2).c_str();
ss >> x;
c = (char)x;
cipherText += c;
if (i >= cipherTextHex.length() - 2) break;
i += 2;
}


//  
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedText));
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(cipherText.c_str()), cipherText.size());


stfDecryptor.MessageEnd();


return decryptedText;
}


string readCipher() {
ifstream in("E:\\cipher.data");


string line;
string decryptedText;
while (getline(in, line)) {
if (line.length() > 1) {
decryptedText += decrypt(line) + "\n";
}
line.clear();
}


cout << "readCipher finish " << endl;
in.close();


return decryptedText;
}
int _tmain(int argc, _TCHAR* argv[]) {
string text = "hello zhuzhu dashen !";
cout << "text : " << text << endl;


initKV();
string cipherHex = encrypt(text);
cout << "cipher : " << cipherHex << endl;
writeCipher(cipherHex);


text = readCipher();
cout << "text2 : " << text << endl;
return 0;
}


*/
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值