C++ C++;正确写入/读取十六进制值(CryptoPP)
我正在尝试运行一个程序,该程序使用AES进行加密和解密 (来自)C++ C++;正确写入/读取十六进制值(CryptoPP),c++,console-application,crypto++,C++,Console Application,Crypto++,我正在尝试运行一个程序,该程序使用AES进行加密和解密 (来自) //来自aestest1.cpp //运行时包括 #包括 #包括 #包括 #包括 #包括 #包括“stdafx.h” //Crypto++包括 #包括“cryptlib.h” #包括“aes.h”//aes #包括“modes.h”//CBC_Mode #包括“filters.h”//StringSource 使用名称空间std; int main(int argc,char*argv[]){ //钥匙和IV设置 字节键[Cryp
//来自aestest1.cpp
//运行时包括
#包括
#包括
#包括
#包括
#包括
#包括“stdafx.h”
//Crypto++包括
#包括“cryptlib.h”
#包括“aes.h”//aes
#包括“modes.h”//CBC_Mode<>
#包括“filters.h”//StringSource
使用名称空间std;
int main(int argc,char*argv[]){
//钥匙和IV设置
字节键[CryptoPP::AES::DEFAULT_KEYLENGTH],
iv[CryptoPP::AES::BLOCKSIZE];
::memset(key,0x01,CryptoPP::AES::DEFAULT_KEYLENGTH);
::memset(iv,0x01,CryptoPP::AES::BLOCKSIZE);
//消息M
string PlainText=“Hello AES World”;
//调试
cout您需要禁用文本处理,这会弄乱换行符
试一试
及
这是你唯一遇到的问题吗?哦,我对写密文很满意,只有二进制模式才能很好地工作。现在我似乎在写和读密钥[]和iv[]时遇到了问题.也许使用for循环和默认的_keydlength/BLOCKSIZE来写和读会有帮助吗?我自己写不了那段代码。提前谢谢。我建议在密文之前写键
和iv
,因为它们的长度是固定的。你可以使用stream write的;write.write(iv,sizeof iv/sizeof*iv)
和匹配函数读取
,以准确地传输正确的字符数。这样就不难找出密文的开始和结束位置。谢谢,您能告诉我编辑的代码有什么问题吗?当然是读/写部分。(在代码的这一部分中使用了名称空间std;,我刚刚为这个问题合并了两个文件,所以不要认为std::丢失了)对于key
和iv
来说很好,但是你不能将std::string
视为byte[]
。尤其是sizeof(密文)
没有告诉你什么(这是字符串元数据的大小,而不是内容的大小),请尝试使用CipherText.length()
。
// From aestest1.cpp
// Runtime Includes
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include "stdafx.h"
// Crypto++ Includes
#include "cryptlib.h"
#include "aes.h" // AES
#include "modes.h" // CBC_Mode< >
#include "filters.h" // StringSource
using namespace std;
int main(int argc, char* argv[]) {
// Key and IV setup
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ],
iv[ CryptoPP::AES::BLOCKSIZE ];
::memset( key, 0x01, CryptoPP::AES::DEFAULT_KEYLENGTH );
::memset( iv, 0x01, CryptoPP::AES::BLOCKSIZE );
// Message M
string PlainText = "Hello AES World";
// Debug
cout << "Plain Text:" << endl;
cout << " '" << PlainText << "'" << endl;
cout << endl;
// Cipher Text Sink
string CipherText;
// Encryption
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption
Encryptor( key, sizeof(key), iv );
CryptoPP::StringSource( PlainText, true,
new CryptoPP::StreamTransformationFilter( Encryptor,
new CryptoPP::StringSink( CipherText )
) // StreamTransformationFilter
); // StringSource
///////////////////////////////////////
// DMZ //
///////////////////////////////////////
//Write data
ofstream write ("file.txt", ios::out | ios::binary);
write.write((char*)key,sizeof(key));
write.write((char*)iv,sizeof(iv));
int at = CipherText.length();
write.write(CipherText.c_str(),at);
write.close();
CipherText.erase();
//Using new key and iv later;
byte key1[ CryptoPP::AES::DEFAULT_KEYLENGTH ],
iv1[ CryptoPP::AES::BLOCKSIZE ];
//Read data
ifstream read ("file.txt", ios::in | ios::binary);
read.seekg (0, ios::end);
int fsize = read.tellg();
read.seekg (0, ios::beg);
read.read((char*)key1,sizeof(key));
read.read((char*)iv1,sizeof(iv));
int toRead = fsize - sizeof(key) - sizeof(iv);
vector<char> bData(toRead);
read.read(&bData[0],toRead);
read.close();
// Recovered Text Sink
string RecoveredText;
// Decryption
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption
Decryptor( key1, sizeof(key1), iv1 );
CryptoPP::StringSource( &bData[0], true,
new CryptoPP::StreamTransformationFilter( Decryptor,
new CryptoPP::StringSink( RecoveredText )
) // StreamTransformationFilter
); // StringSink
// Debug
cout << "Recovered Text:" << endl;
cout << " '" << RecoveredText << "'" << endl;
cout << endl;
system("pause");
return 0;
ofstream write ("text.txt", ios::out | ios__binary);
ifstream read ("text.txt", ios::in | ios::binary);