Я много исследовал это в интернете, и никакого решения не было найдено.Поэтому я решил сделать это сам.
Я создал класс для этого!Приведенный ниже код объясняет сам по себе.Хорошо выглядишь!
#pragma once
#include <string>
#include "../../Include/Cryptopp562/aes.h"
using namespace CryptoPP;
//.h
class CryptoUtil
{
public:
CryptoUtil(std::wstring mac);
byte m_key[CryptoPP::AES::DEFAULT_KEYLENGTH];
byte m_iv[CryptoPP::AES::DEFAULT_KEYLENGTH];
char m_macInChar[12];
// decripty string encrypted in AES/CBC/PKCS5Padding mode
std::wstring DecryptStr(std::wstring str);
};
//.cpp
#include "stdafx.h"
#include "CryptoUtil.h"
#include <atlstr.h>
#include "../../Include/Cryptopp562/cryptlib.h"
#include "../../Include/Cryptopp562/filters.h"
#include "../../Include/Cryptopp562/modes.h"
#include "../../Include/Cryptopp562/modes.h"
#include "../../Include/Cryptopp562/aes.h"
#include "../../Include/Cryptopp562/filters.h"
#include "../../Include/Cryptopp562/aes.h"
#include "../../Include/Cryptopp562/base64.h"
#include "../../Include/Cryptopp562/md5.h"
using namespace CryptoPP;
CryptoUtil::CryptoUtil(std::wstring mac)
{
CryptoPP::MD5 md5;
byte ivString [] = { "blablabla!@#$&&&&&&&" };
md5.CalculateDigest(m_iv, (byte*) ivString, 20);
char macInChar[12 + 1];
size_t charsConverted = 0;
wcstombs_s(&charsConverted, macInChar, mac.c_str(), mac.length());
md5.CalculateDigest(m_key, (byte*) macInChar, 12);
}
std::wstring CryptoUtil::DecryptStr(std::wstring str)
{
std::string encoded, decoded, recovered;
//change wchar to char
size_t outputSize = str.length() + 1;
char* pBuffer = new char[outputSize];
size_t charsConverted = 0;
wcstombs_s(&charsConverted, pBuffer, outputSize, str.c_str(), str.length());
std::string plain(pBuffer);
delete [] pBuffer;
char * pFormattedStr = new char[outputSize];
int sizeOf = sizeof(pFormattedStr);
sprintf_s(pFormattedStr, outputSize, "%16s", plain.c_str());
encoded = pFormattedStr;
delete [] pFormattedStr;
try
{
StringSource ss(encoded, true,
new CryptoPP::Base64URLDecoder(
new StringSink(decoded)
) // Base64URLDecoder
); // StringSource
CBC_Mode< AES >::Decryption dec;
dec.SetKeyWithIV(m_key, sizeof(m_key), m_iv);
// The StreamTransformationFilter removes
// padding as required.
//StringSource s(cipher, true,
StringSource s(decoded, true,
new StreamTransformationFilter(dec,
new StringSink(recovered)
) // StreamTransformationFilter
); // StringSource
CStringW strTemp = CA2W((char*) recovered.c_str(), CP_UTF8);
strTemp.Trim();
std::wstring strRecovered(strTemp);
return strRecovered;
}
catch (const CryptoPP::Exception& e)
{
throw e;
}
}