參考文章:
https://blog.csdn.net/tangcaijun/article/details/42110319ios
首先下載庫:
https://www.cryptopp.com/#download
使用vs2017打開cryptest.sln文件,解決方案選擇「重訂解決方案目標」,升級sdk。
編譯庫和dll文件
將生成的cryptopp.lib和cryptopp.dll放到項目文件夾,若是單獨運行須要將dll文件拷貝到debug文件夾和生成的exe文件放在一塊兒使用。
新建win32 c++控制檯程序,工程->配置屬性->vc++目錄->包含目錄,填寫cryptopp的目錄,須要使用其中的頭文件.
編碼:c++
#include "pch.h" #include <iostream> #include <fstream> #include <aes.h> #include <filters.h> #include <modes.h> #include <Windows.h> #pragma comment(lib, "cryptopp.lib") using namespace std; byte key[CryptoPP::AES::DEFAULT_KEYLENGTH], iv[CryptoPP::AES::BLOCKSIZE]; void initKV() { memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH); 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(); string cipherTextHex; for (int i = 0; i < cipherText.size(); i++) { char ch[3] = { 0 }; sprintf_s(ch, "%02x", static_cast<byte>(cipherText[i])); cipherTextHex += ch; } return cipherTextHex; } void writeCipher(string output) { ofstream out("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("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 main() { string text = "What's up dude!"; cout << "text : " << text << endl; initKV(); string cipherHex = encrypt(text); cout << "cipher : " << cipherHex << endl; writeCipher(cipherHex); string decrpt_text = readCipher(); cout << "text : " << decrpt_text << endl; return 0; }
運行結果:
web