我正在使用 Crypto++ 加密和解密文件。在加密中,key
随机IV
生成和hexencoded
文件中的文本在哪里被加密。IV
和text都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>