Question

I'm using the Botan library for encryption within Qt. I have it working to where I can encrypt and decrypt from one file to another, but I'm trying to alter it to encrypt from a file to a QDomDocument (encrypted file will just be an XML file), and decrypt back from a QDomDocument to a file.

This is what I have so far for the actual encryption (filePlainText/fileEnc are just txt file paths).

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 accepts a ofsteam or ostream. So I figure I need to use an ostream when decrypting from file to variable. But how can I store the contents of the ostream into something I can feed into a QDomDocument?

Then for encrypting back into a file, use an istream into an ofstream, but how can I take feed the QDomDocument content into an istream?

Was it helpful?

Solution

QDomDocument can be read from and written to a QByteArray and you can read from / write to a std::string with std::ostringstream / std::istringstream.

So if you combine these, you would have something like:

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

And for the decoding part:

// before the decoding
std::ostringstream out;
... // decoding
// after the decoding
const std::string & buffer = out.str();
document.setContent(QByteArray(buffer.c_str(), buffer.size()));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top