PDA

View Full Version : Convert QString into QByteArray and vice versa.



freely
14th March 2011, 10:30
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”);

I get user input and write to file:


emailEntered = this->login->lineEdit_email->text();

keyEntered = this->login->lineEdit_code->text();

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));

I save to file:


QTextStream stream(&file);
stream.setCodec(this->codec); stream<<‘‘<<emailToSave<<’\n’<<’’<<keyToSave<<’\n’<<’*’<<macAdressToSave<<’\n’; file.close();

I read from file:


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();

I decrypt readed data from file:


QString result = QString::fromUtf8(calculateXor(this->emailFromFile.toAscii(),encryptionKey));

QString result = QString::fromUtf8(calculateXor(this->keyFromFile.toAscii(),encryptionKey));

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…

high_flyer
14th March 2011, 11:09
Perhaps it is because of incorrect conversion QByteArray – QString and vice versa.
It is because of the conversion, but the conversion is not incorrect!
QString and QByteArray have many things in common, but they are not the same!
If you read the documentation of the various conversion functions both in QByteArray and QString, you will see notes and warnings about the conversions, and how they are done.
If you need to deal with non textual byte sequences, you will get problems when you convert from QByteArray to QString, since QString will either discard or replace non visible characters, changing the binary meaning of your original bytes!

freely
14th March 2011, 11:29
Yes of course it is one of the cases.
I understand Unicode,Asci,Latin,UTF and I have read the Qt Framework documentation
I need to convert QString to QByteArray and vice versa so as not to loose any data while converting

How to accomplish such conversion?

high_flyer
14th March 2011, 12:04
I don't think you can, from the simple fact, that QByteArray has non visible ASCII values (or can have).
What you need is not to be able to convert QByteArray to QString, but to apply the functionality you have in QStinrg for your QByteArray data.
As far as I understand it, you will have to implement that your self.

wysota
14th March 2011, 12:49
Yes of course it is one of the cases.
I understand Unicode,Asci,Latin,UTF and I have read the Qt Framework documentation
I need to convert QString to QByteArray and vice versa so as not to loose any data while converting

How to accomplish such conversion?

But why exactly do you want this conversion to take place? If you want to xor the string then just do it. Each QChar is a 16 bit value, just xor it with 16 bits of your key and you'll get two 8b values that you can store in the byte array.


QByteArray xorEncrypt(const QString &string, const QByteArray &key){
QByteArray result;
int keyIndex = 0;
for(int i=0;i<string.size();++i){
quint16 val = string.at(i).unicode();
char high = key.at(keyIndex++);
if(keyIndex==key.size()) keyIndex = 0;
char low = key.at(keyIndex++);
if(keyIndex==key.size()) keyIndex = 0;
quint16 keyPiece = ((high & 0xFF) << 8) | (low & 0xFF));
quint16 encoded = val ^ keyPiece;
result.append(encoded >> 8);
result.append(encoded & 0xFF);
}
return result;
}

Decoding is similar, you just need to reassemble the string.

Of course be aware that xor is a very lousy "cipher" that can be broken within a couple of seconds, especially when encoding data consisting mostly of 0x00:
0x00 xor SECRET = SECRET

The easiest way to break xor is to feed it with a message (either plaintext or ciphertext) consisting of zeroes. Then the key will reveal itself during encryption/decryption.

freely
14th March 2011, 17:17
The problem with data loss during encryption was solved my mine by
a next way:
I work with UTF8 codec always when I treat strings at any platform:
I added next static “methods” to define codecs:


QTextCodec::setCodecForLocale(QTextCodec::codecFor Name("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("UTF-8"));

when I convert from QByteArray to QString I use next static “method” with next arguments:


macEnEn = calculateXor(fromFile.append(this->macFromFile),encryptionKey);
QString result = QString::fromUtf8(macEnEn.data(),macEnEn.size());

The problel was – you should provide second argument an exact size of array to static “method” QString::fromUtf8(…)
it works!
You can take any character set, convert it, encode using bitwise manipulations, store or transmit
and decode to any character set using your symmetrical key!
I have my own xor method, I do not provide it here.

Added after 34 minutes:

sorry I am not sure about converting xor'ed QByteArray to utf8 coded QString and vice versa because of codec you can loose characters "meaning".
Somebody already knows what codec suitable for such conversion?
I will check and get back to here later

So what "cypher" * symmetrical to use ?

Added after 33 minutes:

Next link will help for simple and fast text data encryption:

http://wiki.forum.nokia.com/index.php/How_to_Encrypt_a_text_using_QCryptographicHash_in_ Qt_for_Symbian
Since XOR encryption is not so reliable.

wysota
14th March 2011, 18:57
The problem with data loss during encryption was solved my mine
Data loss can still occur as there are character sets with characters outside the utf-8 scope.


Somebody already knows what codec suitable for such conversion?
Yeah... Unicode...


So what "cypher" * symmetrical to use ?
Hmm?


Next link will help for simple and fast text data encryption:

http://wiki.forum.nokia.com/index.php/How_to_Encrypt_a_text_using_QCryptographicHash_in_ Qt_for_Symbian

True. Unfortunately it is not possible to decrypt such message.

freely
15th March 2011, 13:47
True. Unfortunately it is not possible to decrypt such message.
Why not if using hash on hash you won't receive the previous message ( using the same hashing algorithm )?

wysota
15th March 2011, 14:00
Hashes (such as SHA1) are one way functions: H(H(x)) !=x. Besides, they don't use a key (aka "password"). Since I know you are using OpenSSL for your mailing application I suggest you use one of encryption algorithms supported by OpenSSL:
Cipher commands (see the `enc' command for more details)
aes-128-cbc aes-128-ecb aes-192-cbc aes-192-ecb aes-256-cbc
aes-256-ecb base64 bf bf-cbc bf-cfb
bf-ecb bf-ofb cast cast-cbc cast5-cbc
cast5-cfb cast5-ecb cast5-ofb des des-cbc
des-cfb des-ecb des-ede des-ede-cbc des-ede-cfb
des-ede-ofb des-ede3 des-ede3-cbc des-ede3-cfb des-ede3-ofb
des-ofb des3 desx rc2 rc2-40-cbc
rc2-64-cbc rc2-cbc rc2-cfb rc2-ecb rc2-ofb
rc4 rc4-40

I'd suggest blowfish (bf) (e.g. in cipher block chaining mode - bf-cbc) or 3des (des3).

freely
15th March 2011, 14:05
Witold you are guru for me
Hhh:)
I saw your interview at QtDevDays highlights
How are you doing?

wysota
15th March 2011, 14:14
How are you doing?
Fine, thank you, although my PhD is lagging behind, as usual. But let's stick to the subject.