【问题标题】:Output Botan Encryption results to a QDomDocument and vice-versa将 Botan 加密结果输出到 QDomDocument,反之亦然
【发布时间】:2012-03-28 08:41:24
【问题描述】:

我在 Qt 中使用 Botan 库进行加密。我让它工作到可以从一个文件加密和解密到另一个文件的位置,但我正在尝试将其更改为从文件加密到 QDomDocument(加密文件将只是一个 XML 文件),然后从 QDomDocument 解密到一个文件。

这是我目前实际加密的内容(filePlainText/fileEnc 只是 txt 文件路径)。

std::ifstream in(filePlainText.c_str(),std::ios::binary);
std::ofstream out(fileEnc.c_str(),std::ios::binary);
Pipe pipe(get_cipher("AES-256/CBC",key,iv,ENCRYPTION),new DataSink_Stream(out));
pipe.start_msg();
in >> pipe;
pipe.end_msg();
out.flush();
out.close();
in.close();

DataSink_Stream 接受 ofsteam 或 ostream。所以我认为在从文件解密到变量时需要使用 ostream。但是如何将 ostream 的内容存储到可以输入 QDomDocument 的内容中?

然后为了加密回一个文件,使用 istream 到一个 ofstream 中,但是我怎样才能将 QDomDocument 内容输入到 istream 中?

【问题讨论】:

    标签: c++ qt encryption iostream botan


    【解决方案1】:

    QDomDocument 可以读取和写入QByteArray,您可以使用std::ostringstream / std::istringstream 读取/写入 std::string。

    所以如果你把这些结合起来,你会得到类似的东西:

    // before the encoding
    const QByteArray & buffer = document.toByteArray(-1);
    std::istringstream in(std::string(buffer.data(), buffer.size()));
    ... // encoding
    

    对于解码部分:

    // before the decoding
    std::ostringstream out;
    ... // decoding
    // after the decoding
    const std::string & buffer = out.str();
    document.setContent(QByteArray(buffer.c_str(), buffer.size()));
    

    【讨论】:

    • 完美!这就是我所缺少的——在 QByteArray 和 QDom/stream 之间进行转换。谢谢!
    猜你喜欢
    • 2019-02-02
    • 2021-11-10
    • 2012-04-03
    • 1970-01-01
    • 2013-11-25
    • 2019-02-27
    • 2014-11-08
    • 2012-02-16
    • 2013-08-16
    相关资源
    最近更新 更多