gpt4 book ai didi

c++ - 使用 Crypto++ 解密时原始文本末尾的车库冗余字符

转载 作者:行者123 更新时间:2023-11-28 01:57:39 25 4
gpt4 key购买 nike

我正在使用 Crypto++,CTR 模式,在 C++ 中加密和解密文本。一切似乎都有效 99%。加密成功,解密也返回原始文本,但我在解密文本的末尾给出了一些额外的随机垃圾冗余文本,如“ð”。这个额外的部分是每次我运行代码时随机生成的。我的代码有问题吗?


将字符串加密为字符串

string  encryptString(string plain, byte key[], int sizeKey, byte iv[], int sizeIV){
string cipher;
try{
CTR_Mode< AES >::Encryption e;
e.SetKeyWithIV(key, sizeKey, iv, sizeIV);

// The StreamTransformationFilter removes
// padding as required.
StringSource s(plain, true,
new StreamTransformationFilter(e,
new StringSink(cipher)
)
);

#if 0
StreamTransformationFilter filter(e);
filter.Put((const byte*)plain.data(), plain.size());
filter.MessageEnd();

const size_t ret = filter.MaxRetrievable();
cipher.resize(ret);
filter.Get((byte*)cipher.data(), cipher.size());
#endif
return cipher;
}
catch (const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
return NULL;
}
}

将加密后的字符串解密为字符串

string  decryptString(string cipher, byte key[], int sizeKey, byte iv[], int sizeIV){
string reco ="";
try{
CTR_Mode< AES >::Decryption d;
d.SetKeyWithIV(key, sizeKey, iv, sizeIV);

StringSource s(cipher, true,
new StreamTransformationFilter(d,
new StringSink(reco)
)
);

}
catch (const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
}
return reco;

}

包装上面的 encryptString 函数。

char* encrypt(char * plainText, byte key[], int sizeKey, byte iv[], int sizeIV, long &len){
string cipher = encryptString(plainText, key, sizeKey, iv, sizeIV);
len = cipher.size() + 1;
char * writable = new char[len];
std::copy(cipher.begin(), cipher.end(), writable);
writable[len] = '\0'; // don't forget the terminating 0
return writable;
}

包装上面的 decryptString 函数。

char* decrypt(char * cipher,  byte key[], int sizeKey, byte iv[], int sizeIV, long len){
string ss(cipher, len);
long lengSS = ss.length();
string recovered = decryptString(ss, key, sizeKey, iv, sizeIV);
char * writable = new char[recovered.size() + 1];
std::copy(recovered.begin(), recovered.end(), writable);
writable[recovered.size()] = '\0'; // don't forget the terminating 0
return writable;
}

我的测试脚本很简单。读取some.txt内容(“我爱你”),写入s1.txt,检查读取是否正确。加密、解密,然后将恢复的文本写入另一个文件 (d1.txt)。

int main(int argc, char* argv[])
{
AutoSeededRandomPool prng;

byte key[AES::DEFAULT_KEYLENGTH] = { '1', '2', '3', '4', '5', '6', '7', '8', '1', '2', '3', '4', '5', '6', '7', '8' };
//prng.GenerateBlock(key, sizeof(key));

byte iv[AES::BLOCKSIZE] = { '8', '7', '6', '5', '4', '3', '2', '1', '8', '7', '6', '5', '4', '3', '2', '1' };
prng.GenerateBlock(iv, sizeof(iv));
long size = 0;
char * s1 = FileUtil::readAllByte("some.txt");
//Result: s1.txt content is "I love you"

long len = 0;
char* result1 = encrypt(s1, key, sizeof(key), iv, sizeof(iv), len);
//Result: result1 is a bunch of ciphered characters

cout << "desc" << endl;
char* recovered1 = decrypt(result1, key, sizeof(key), iv, sizeof(iv), len);
//Result: recovered1="I love youð". Generally, it has form of "I love youX"
//X can be any garbage chatacter, and each time I run the code, X is one different
//character.
}

根据接受的答案,解决方案是:像这样更新我的 encrypt():

char* encrypt(char * plainText, byte key[], int sizeKey, byte iv[], int sizeIV, long &len){
string cipher = encryptString(plainText, key, sizeKey, iv, sizeIV);
FileUtil::writeFile("ss1.txt", cipher, cipher.length());
len = cipher.size() ;
char * writable = new char[len];
std::copy(cipher.begin(), cipher.end(), writable);
writable[len] = '\0'; // don't forget the terminating 0
FileUtil::writeFile("w1.txt",writable, len);

return writable;
}

只需分配可写的长度 = 密码的长度。将终止符设置为 writeble[len]

最佳答案

当您遇到缓冲区溢出和未终止的字符串时,往往会发生这种情况。如果我们查看您的 encrypt 函数,我们会看到缓冲区溢出:

len = cipher.size() + 1;
char * writable = new char[len];
std::copy(cipher.begin(), cipher.end(), writable);
writable[len] = '\0';

看到这里你分配了 len 个字节,其中 lencipher 大一个。但是,当您终止字符串时,您正在使用 len 来索引越界。

您应该使用 len-1cipher.size() 作为终止符索引。

关于c++ - 使用 Crypto++ 解密时原始文本末尾的车库冗余字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40601847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com