C++正确写入/读取十六进制值 (CryptoPP)

发布于 2024-09-08 11:06:26 字数 2827 浏览 5 评论 0原文

我正在尝试运行一个使用 AES 加密和解密的程序。

(来自 http://www.codeproject.com/KB/security/AESProductKey.aspx< /a> )

// 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;

} 所以我无法编写一个代码来正确地完成注释中提到的事情(在 DMZ 之后,ofstream 开始的地方)。提前致谢。

I am trying to run a program, that encrypts and decrypts using AES.

(from http://www.codeproject.com/KB/security/AESProductKey.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;

}
So I couldn`t manage to write a code, that will correctly do the stuff, that mentioned in comments (after DMZ, where ofstream begins). Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没︽人懂的悲伤 2024-09-15 11:06:26

您需要禁用文本处理,这会弄乱换行符。

尝试

ofstream write ("text.txt", ios::out | ios__binary);

ifstream read ("text.txt", ios::in | ios::binary);

You need to disable text processing, which messes up newline characters.

Try

ofstream write ("text.txt", ios::out | ios__binary);

and

ifstream read ("text.txt", ios::in | ios::binary);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文