Take the 2-minute tour ×

So 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));
in >> pipe;

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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()));
share|improve this answer
Perfect! That's what I was missing - converting to and from a QByteArray to the QDom/stream. Thanks! –  giraffee Mar 12 '12 at 0:37

Your Answer


