Crypto++ を使用してファイルの暗号化と復号化に取り組んでいます。暗号化ではkey
、ランダムIV
が生成さhexencoded
れ、ファイルからのテキストが暗号化されます。IV
とcipher
テキストの両方が同じファイルに書き込まれます。
復号化でkey
は、暗号化と同じ基準を使用して生成IV
され、ファイルからランダムに抽出され、hexdecoded
. 長さの後のテキストiv
は文字列に格納され、復号化されます。
何が起こるかというと、元のファイルを見ることができるので、それが機能していることはわかっていますがcipher
、元のファイルのテキストの後にテキストも表示されます。それを解決する方法はありますか?
//some code to declare variables, read from file and so on
unsigned char * inputContent = (unsigned char *) malloc(fileSize * sizeof(char)); //create char array of same size as file content
//inputContent is for storing file data
string rawString(reinterpret_cast<char*>(inputContent), fileSize); //convert char array to string
//extract iv, key and cipher from rawString
string rawIV;
rawIV = rawString.substr(0, 32);
//code to hexdecode iv
string cipher;
cipher = rawString.substr(32, fileSize - 32);
string recovered;
CBC_Mode< AES >::Decryption d;
d.SetKeyWithIV(key, sizeof(key), iv);
StringSource s_recover(cipher, true,
new StreamTransformationFilter(d,
new StringSink(recovered)
)
);
const char * writeContent = recovered.c_str();
if(pwrite(fd, writeContent, recovered.length(), 0) <= 0)
{
return -1; //error
}
前もって感謝します。☺</p>