New SimpleCrypt page

Page  
2

March 23, 2011

Pau Pau
Lab Rat
25 posts

So I’ve played a bit around I’ve come to the conclusion that I’m using QDataStream incorrectly.

My Test Code: main.cpp

  1. #include <QString>
  2. #include <QDebug>
  3. #include <QBuffer>
  4. #include <QDataStream>
  5. int main(){
  6.  QString txStr = "Hello world!";
  7.  QString rxStr;
  8.  
  9.  QBuffer txB;
  10.  txB.open(QIODevice::ReadWrite);
  11.  QDataStream s(&txB);
  12.  qDebug() << txB.size();
  13.  s << txStr;
  14.  qDebug() << txB.size();
  15.  s >> rxStr;
  16.  qDebug() << rxStr;
  17.  return 0;
  18. }

My Output:
0
28
“”

So the buffer is being filled but not correctly being read out.. why not?

March 23, 2011

Gerolf Gerolf
Robot Herder
3286 posts

That’s an easy question. Your problem is the stream position:

you write to the stream, then start reading after the written bytes :-)

try out the following:

  1. #include <QString>
  2. #include <QDebug>
  3. #include <QBuffer>
  4. #include <QDataStream>
  5. int main()
  6. {
  7.     QString txStr = "Hello world!";
  8.     QString rxStr;
  9.  
  10.     QBuffer txB;
  11.     txB.open(QIODevice::WriteOnly);
  12.     QDataStream s(&txB);
  13.     qDebug() << txB.size();
  14.     s << txStr;
  15.     qDebug() << txB.size();
  16.     txB.close(); // sets stream position to 0 !!!
  17.    
  18.     txB.open(QIODevice::ReadOnly);
  19.     QDataStream s2(&txB);
  20.     s2 >> rxStr;
  21.     qDebug() << rxStr;
  22.     return 0;
  23. }

or use a seek in between

  1. #include <QString>
  2. #include <QDebug>
  3. #include <QBuffer>
  4. #include <QDataStream>
  5. int main()
  6. {
  7.     QString txStr = "Hello world!";
  8.     QString rxStr;
  9.  
  10.     QBuffer txB;
  11.     txB.open(QIODevice::WriteOnly);
  12.     QDataStream s(&txB);
  13.     qDebug() << txB.size();
  14.     s << txStr;
  15.     qDebug() << txB.size();
  16.     txB.seek(0); // sets stream position to 0 !!!
  17.     s >> rxStr;
  18.     qDebug() << rxStr;
  19.     return 0;
  20. }

I did not compile the code, so it might contain typos

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

March 23, 2011

Pau Pau
Lab Rat
25 posts

Aha, got it all sorted out, thanks

I had my suspicions on that, but thought that the “<<” and “>>” operators may use seperate index variables.

Andre: it would be great if you could incorporate in the Example usage code the following so it works out of the box.

  1. buffer.open(QIODevice::WriteOnly);
  2. buffer.close();
  3. buffer.open(QIODevice::ReadOnly);

Because newbies like myself:

  • copy + paste + compile
    • if it works -> adapt to needs
    • if it doesn’t -> get lost in the simplest things

Although I must admit that I am learning loads of things through all these problems I get myself into.

Gerolf: Once again thanks for your quick and effective response.

March 23, 2011

Andre Andre
Robot Herder
6417 posts

paucoma, thank you for your comments and suggestions. I have changed the wiki page accordingly. I am glad you solved the problem, and that it turned out the class itself was not at fault ;-)

I must admit that I wrote the examples directly in the wiki page, and thats when errors like these happen. Sorry about that! I hope the current version is more managable? Note that I was talking about “at the other end”, implying that the decryption code was not meant to be used in the same function (and thus, implicitly, with the same buffer).

March 23, 2011

Pau Pau
Lab Rat
25 posts

Thanks Andre for making the modifications.

I have corrected myself a few other details I found.

P.S. I didn’t know that I had such a priviledge.

March 23, 2011

Andre Andre
Robot Herder
6417 posts

Thanks for your effort, I really appreciate it. If you want, you’re welcome to update it yourself in the wiki article :-)

March 24, 2011

Gerolf Gerolf
Robot Herder
3286 posts

Hi Andre,

one suggestion to the wiki page:
In the section SimpleCrypt in use it would be nice to have a list of the encryp/decrypt functions. You state thete are two times 4, but not the names :-)
this means searching inside the code :-)

The rest sound really god. Thanks for the article.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

March 24, 2011

Andre Andre
Robot Herder
6417 posts

Gerolf wrote:
Hi Andre,

one suggestion to the wiki page:
In the section SimpleCrypt in use it would be nice to have a list of the encryp/decrypt functions. You state thete are two times 4, but not the names :-)
this means searching inside the code :-)

The rest sound really god. Thanks for the article.

Fair enough, done. :-)

Thanks for the praise.

March 24, 2011

Gerolf Gerolf
Robot Herder
3286 posts

Hi Andre,

I’m currently reading your second article, and found the following here [developer.qt.nokia.com] :

The payload data block’s contents are encrypted with a four byte (quint64) key.

quint64 is a 64 bit integer, which means 8 byte.

so also in the snipet page, you say:

  1.     SimpleCrypt crypto(Q_UINT64_C(0x7F29B208)); //some random number

this is just a 32 bit number, not 64 bit.

  1. void SimpleCrypt::splitKey()
  2. {
  3.     m_keyParts.clear();
  4.     m_keyParts.resize(4);
  5.     for (int i=0;i<4;i++) {
  6.         quint64 part = m_key;
  7.         for (int j=i; j>0; j--)
  8.             part = part >> 8;
  9.         part = part & 0xff;
  10.         m_keyParts[i] = static_cast<char>(part);
  11.     }
  12. }

this also only uses 4 bytes, not 8.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

March 24, 2011

Gerolf Gerolf
Robot Herder
3286 posts

some additional topics:

in Decrypted payload [developer.qt.nokia.com]

SH1 and CRC have the same description:

In case the Protection Checksum flag has been set, the layout of the decrypted payload looks like this:

I think this is just a copy paste error.

As I’m through now, no more things :-)
Very good article, interesting to read. Thanks.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

March 24, 2011

Andre Andre
Robot Herder
6417 posts

Thank you very much for this catch, Gerolf! You are absolutely right, and this is a big mistake in the algorithm as well. I will take care of fixing this ASAP.

March 24, 2011

Andre Andre
Robot Herder
6417 posts

OK, fixed on both pages. Very embarrassing, I must say. :(

March 24, 2011

Gerolf Gerolf
Robot Herder
3286 posts

Now it looks good, thanks :-)

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

May 29, 2011

Luca Luca
Ant Farmer
639 posts

I tried today SimpleCrypt class and it was very useful.

Thanks!

May 29, 2011

Andre Andre
Robot Herder
6417 posts

Glad you like it!

Page  
2

  ‹‹ New Qt for Embedded Linux Series started      new DocumentWindow wiki page - How to do this on Linux / Mac? ››

You must log in to post a reply. Not a member yet? Register here!