I need to save some information (text) into a file. However, I need to obfuscate the contents to prevent tampering, or at least make it harder. The easiest solution that came to mind was to convert the QString data to CString, stream it to QByteArray, convert it to Base64 using QByteArray::toBase64(), then save it to a file using QDataStream. I don't have problem with saving.
Reading the information from the file, however, hasn't been successful. Upon reading the information, only a single word is retrieved, not the whole text. I need some advice on this one. Here's a simplified version of my code:
QString strTest
= "This is a string.$";
QString strTest2
= "Another string.$";
QFile testFile
( "./Test.ini" );
if( testFile.exists() ) {
QByteArray byteStrTest2
( strTest2.
toStdString().
c_str() );
qDebug() << "strTest2.toStdString().c_str() : "
<< strTest2.toStdString().c_str();
writer << byteStrTest2.toBase64();
testFile.close();
}
if( !testFile.exists() ) {
QByteArray byteStrTest
( strTest.
toStdString().
c_str() );
qDebug() << "strTest.toStdString().c_str() : "
<< strTest.toStdString().c_str();
writer << byteStrTest.toBase64();
testFile.close();
}
readFromByte( &testFile );
}
void readFromByte
(QFile *file) { reader >> byteFileContents;
qDebug() << "byteFileContents : " << byteFileContents;
qDebug() << "byteFileContents.fromBase64( byteFileContents ) : "
<< byteFileContents.fromBase64( byteFileContents );
byteFileContents = byteFileContents.fromBase64( byteFileContents );
qDebug() << "byteFileContents.size() : " << byteFileContents.size();
char *chrFileContents = new char[ byteFileContents.size() + 1 ];
strcpy( chrFileContents, byteFileContents.data() );
QString strFileContents
= QString::fromAscii( chrFileContents, byteFileContents.
size() );
QStringList strings
= strFileContents.
split( '$',
QString::KeepEmptyParts, Qt
::CaseSensitive );
foreach(strDisplay, strings) {
qDebug() << "Value : " << strDisplay; // Output is always "Value : This is a string."
}
file->close();
qDebug() << "sizeof( chrFileContents ) : " << sizeof( chrFileContents );
qDebug() << "sizeof( char ) : " << sizeof( char );
}
QString strTest = "This is a string.$";
QString strTest2 = "Another string.$";
QFile testFile( "./Test.ini" );
if( testFile.exists() ) {
testFile.open( QIODevice::Append );
QByteArray byteStrTest2( strTest2.toStdString().c_str() );
qDebug() << "strTest2.toStdString().c_str() : "
<< strTest2.toStdString().c_str();
QDataStream writer( &testFile );
writer << byteStrTest2.toBase64();
testFile.close();
}
if( !testFile.exists() ) {
testFile.open( QIODevice::WriteOnly );
QByteArray byteStrTest( strTest.toStdString().c_str() );
qDebug() << "strTest.toStdString().c_str() : "
<< strTest.toStdString().c_str();
QDataStream writer( &testFile );
writer << byteStrTest.toBase64();
testFile.close();
}
readFromByte( &testFile );
}
void readFromByte(QFile *file) {
file->open( QIODevice::ReadOnly );
QDataStream reader( file );
QByteArray byteFileContents;
reader >> byteFileContents;
qDebug() << "byteFileContents : " << byteFileContents;
qDebug() << "byteFileContents.fromBase64( byteFileContents ) : "
<< byteFileContents.fromBase64( byteFileContents );
byteFileContents = byteFileContents.fromBase64( byteFileContents );
qDebug() << "byteFileContents.size() : " << byteFileContents.size();
char *chrFileContents = new char[ byteFileContents.size() + 1 ];
strcpy( chrFileContents, byteFileContents.data() );
QString strFileContents = QString::fromAscii( chrFileContents, byteFileContents.size() );
QStringList strings = strFileContents.split( '$', QString::KeepEmptyParts, Qt::CaseSensitive );
QString strDisplay;
foreach(strDisplay, strings) {
qDebug() << "Value : " << strDisplay; // Output is always "Value : This is a string."
}
file->close();
qDebug() << "sizeof( chrFileContents ) : " << sizeof( chrFileContents );
qDebug() << "sizeof( char ) : " << sizeof( char );
}
To copy to clipboard, switch view to plain text mode
Thanks in advance.
Bookmarks