Hi
please take a look I explain here perhaps it will help to both of us:
I have some application wroten fully in Qt.
In the application I take input from user from line edits and receive QStrings ( ->text() function of the QLineEdit class ). Then I need to save the input ( QStrings ) encrypted to .txt file on user machine’s hdd.
For that purpose I decided to use simple XOR encryption. So I need to convert QString to QByteArray , after that make XOR between 2 byte arrays – input and encryption key and the result I need to save to .txt file so I convert the result vice versa to QString and put it to file.
Also I need to read from file the encrypted data and to check it in some place in the application. For that purpose I read encrypted from previous user input from .txt file to QString and convert it to QByteArray make decryption on QByteArray, convert the result to QString – and very sorrow I do not receive the same input as before encryption – right side of user input lost!!
Perhaps it is because of incorrect conversion QByteArray – QString and vice versa.
What is strangeous – that only for large strings data lost and for number.
I use UTF8 codec:
codec = QTextCodec::codecForName(“utf8â€);
To copy to clipboard, switch view to plain text mode
I get user input and write to file:
emailEntered = this->login->lineEdit_email->text();
keyEntered = this->login->lineEdit_code->text();
emailEntered = this->login->lineEdit_email->text();
keyEntered = this->login->lineEdit_code->text();
To copy to clipboard, switch view to plain text mode
I make conversion to QByteArray and encryption:
QString emailToSave
= QString::fromUtf8(calculateXor
(this
->emailEntered.
toUtf8(),encryptionKey
));
QString keyToSave
= QString::fromUtf8(calculateXor
(this
->keyEntered.
toUtf8(),encryptionKey
));
QString emailToSave = QString::fromUtf8(calculateXor(this->emailEntered.toUtf8(),encryptionKey));
QString keyToSave = QString::fromUtf8(calculateXor(this->keyEntered.toUtf8(),encryptionKey));
To copy to clipboard, switch view to plain text mode
I save to file:
stream.setCodec(this->codec); stream<<‘‘<<emailToSave<<’\n’<<’’<<keyToSave<<’\n’<<’*’<<macAdressToSave<<’\n’; file.close();
QTextStream stream(&file);
stream.setCodec(this->codec); stream<<‘‘<<emailToSave<<’\n’<<’’<<keyToSave<<’\n’<<’*’<<macAdressToSave<<’\n’; file.close();
To copy to clipboard, switch view to plain text mode
I read from file:
stream.setCodec(this->codec);
emailFromFile.clear();
keyFromFile.clear();
macFromFile.clear();
stream>>ch; // ‘*’
stream>>ch; // email from file:
while(ch!=’\n’)
{
emailFromFile.append(ch);
stream>>ch;
}
stream>>ch;
// ‘*’
stream>>ch; // first digit of the coded license key
while(ch!=’\n’)
{
keyFromFile.append(ch);
stream>>ch;
}
stream>>ch; // ‘*’ stream>>ch;
// first digit of mac from file
while(!stream.atEnd()) // another while(stream!=’\n’)
{
macFromFile.append(ch);
stream>>ch;
}
file.close();
QTextStream stream( &file );
stream.setCodec(this->codec);
emailFromFile.clear();
keyFromFile.clear();
macFromFile.clear();
stream>>ch; // ‘*’
stream>>ch; // email from file:
while(ch!=’\n’)
{
emailFromFile.append(ch);
stream>>ch;
}
stream>>ch;
// ‘*’
stream>>ch; // first digit of the coded license key
while(ch!=’\n’)
{
keyFromFile.append(ch);
stream>>ch;
}
stream>>ch; // ‘*’ stream>>ch;
// first digit of mac from file
while(!stream.atEnd()) // another while(stream!=’\n’)
{
macFromFile.append(ch);
stream>>ch;
}
file.close();
To copy to clipboard, switch view to plain text mode
I decrypt readed data from file:
QString result
= QString::fromUtf8(calculateXor
(this
->emailFromFile.
toAscii(),encryptionKey
));
QString result
= QString::fromUtf8(calculateXor
(this
->keyFromFile.
toAscii(),encryptionKey
));
QString result = QString::fromUtf8(calculateXor(this->emailFromFile.toAscii(),encryptionKey));
QString result = QString::fromUtf8(calculateXor(this->keyFromFile.toAscii(),encryptionKey));
To copy to clipboard, switch view to plain text mode
For email I get the same as before encryption, but for key ( another user input ) there is lost numbers at the right
What is the problem? I try to fix it this time…
Bookmarks