【发布时间】:2018-03-09 02:39:07
【问题描述】:
所以我正在使用 Crypto++ 库来加密文件。我需要保存密钥和 iv 以备将来使用。我正在关注this 教程。这是我的功能:
void AESUtil::encrypt(string filename,bool savekeys,string savefilename){
AutoSeededRandomPool rnd;
// Generate a random key
byte key[AES::DEFAULT_KEYLENGTH];
rnd.GenerateBlock(key, AES::DEFAULT_KEYLENGTH);
// Generate a random IV
byte iv[AES::BLOCKSIZE];
rnd.GenerateBlock(iv, AES::BLOCKSIZE);
Binary b;
string plaintext = b.decoder(filename);
unsigned char *ciphertext= new unsigned char[plaintext.size()+1];
ciphertext[plaintext.size()]='\0';
if(savekeys){
ofstream("key.bin", ios::binary).write((char*)key, sizeof(key));
}
CFB_Mode<AES>::Encryption cfbEncryption(key, AES::DEFAULT_KEYLENGTH, iv);
cfbEncryption.ProcessData(ciphertext,reinterpret_cast<const unsigned char*>(plaintext.c_str()),plaintext.size()+1);
ofstream outfile(savefilename.c_str());
outfile.write((char*)ciphertext,sizeof(ciphertext));
}
文件包含 �/���� 格式的数据。我想知道以编程方式保存密钥和 iv 的最佳方法,它们是字节数组到文件,密文是 unsigned char* 到单独的文件。
【问题讨论】:
-
如果有任何混淆,我深表歉意,但主要是我想知道一种将字节数组(只不过是生成 Key 和 IV 的格式)存储到文件的方法。为了解决这个问题,我将它们转换为 HEX 并存储它们。