我在Qt中使用了Crypto++库,因为使用模式中的方法加密字符串并使用 of和定义字符串参数。AES
CBC
StringSource
StringSink
input
output
首先,我从文件中读取所有字节(“unicode”或“ASCII”编码),然后将其设置为函数中的input
参数StringSource
,然后将参数设置为string
(数据类型)用于输出(密文)。只是我想得到一个字符串并用“aes-cbc”加密它并显示输出。
另外,我知道FileSource
并且是用于将数据写入文件FileSink
的两个函数(consistinput
和流参数)!output
但我想将文件内容作为输入字符串读取。
我的代码:
void Widget::onEncryptButton()
{
QByteArray key = "qwertyuiopasdfgh";
QByteArray iv = "wertyuiopasdfghj"
QByteArray plain;
string cipher;
QFile fi("/home/msi/Desktop/input.txt");
QFile fo("/home/msi/Desktop/output.enc");
fi.open(QFile::ReadOnly);
fo.open(QFile::WriteOnly);
plain = fi.readAll();
AESEncryption aese((byte*)key.constData(), AES::DEFAULT_KEYLENGTH); // default is 16
CBC_Mode_ExternalCipher::Encryption encryptor(aese, (byte*)iv.constData());
StringSource(plain, true, new StreamTransformationFilter(encryptor, new StringSink(cipher)));
QMessageBox::information(this, "", QString("%1, %2").arg(strlen(cipher.c_str())).arg(cipher.size())); // just for viewing cipher length
fo.write(cipher.c_str());
fi.close();
fo.close();
}
现在我有以下问题:
当我读取压缩文件内容(例如 900 字节)并将其设置为输入时
StringSource
,生成的密码将不完整(例如 320 字节)“QMessageBox”中的输出
strlen(cipher.c_str())
不同cipher.size()
我的代码真正工作当我阅读一些文件(“unicode”或“ASCII”,“大”或“小”大小)并且有时工作不正确时。我不明白是哪个原因导致了这个问题?
- 甚至,我直接设置了一些输入字符串(不是从文件中读取)并再次失败!
问候!