2012-03-11 70 views
1

我在Qt中使用Botan庫進行加密。我有它的工作,我可以加密和解密從一個文件到另一個,但我試圖改變它從文件加密到QDomDocument(加密文件將只是一個XML文件),並從QDomDocument解密回來到一個文件。輸出Botan加密結果到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到一個文件流中,但我怎樣才能將QDomDocument內容提交到一個istream?

回答

2

QDomDocument可以讀取和寫入QByteArray,你可以讀取/寫入的std :: string與std::ostringstream/std::istringstream

所以,如果你把這些,你會是這樣的:

// 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())); 
+0

完美!這就是我所缺少的 - 從QByteArray轉換到QDom /流。謝謝! – giraffee 2012-03-12 00:37:30