C ++ Écriture / lecture Valeurs hexagonales correctement (Cryptopp)
-
12-09-2020 - |
Question
J'essaie d'exécuter un programme, qui crypte et déchiffre en utilisant des AES.
(de http://www.codeproject.com/kb/security/aesproductekeykey.aspx )
// 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;
} Donc, je ne pouvais donc pas parvenir à écrire de code, cela fera correctement les affaires, cela mentionné dans les commentaires (après DMZ, où commence Sstream).Merci d'avance.
La solution
Vous devez désactiver le traitement du texte, qui décède des caractèresWLINE.
essayer
ofstream write ("text.txt", ios::out | ios__binary);
et
ifstream read ("text.txt", ios::in | ios::binary);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow