Results 1 to 2 of 2

Thread: Help converting a function to Qt

  1. #1
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Help converting a function to Qt

    I would like to use encryption from the openssl library to encrypt saved user passwords

    So create a function that takes the user's password as a QString and returns the encrypted version I can save with QSettings, and then reverse it to load the password and use it to login on the server.

    I have an example function full implementation details can be found at
    http://tldp.org/LDP/LGNET/87/vinayak.html

    Qt Code:
    1. int encrypt (int infd, int outfd)
    2. {
    3. unsigned char outbuf[OP_SIZE];
    4. int olen, tlen, n;
    5. char inbuff[IP_SIZE];
    6. EVP_CIPHER_CTX ctx;
    7. EVP_CIPHER_CTX_init (& ctx);
    8. EVP_EncryptInit (& ctx, EVP_bf_cbc (), key, iv);
    9.  
    10. for (;;)
    11. {
    12. bzero (& inbuff, IP_SIZE);
    13.  
    14. if ((n = read (infd, inbuff, IP_SIZE)) == -1)
    15. {
    16. perror ("read error");
    17. break;
    18. }
    19. else if (n == 0)
    20. break;
    21.  
    22. if (EVP_EncryptUpdate (& ctx, outbuf, & olen, inbuff, n) != 1)
    23. {
    24. printf ("error in encrypt update\n");
    25. return 0;
    26. }
    27.  
    28. if (EVP_EncryptFinal (& ctx, outbuf + olen, & tlen) != 1)
    29. {
    30. printf ("error in encrypt final\n");
    31. return 0;
    32. }
    33. olen += tlen;
    34. if ((n = write (outfd, outbuf, olen)) == -1)
    35. perror ("write error");
    36. }
    37. EVP_CIPHER_CTX_cleanup (& ctx);
    38. return 1;
    39. }
    To copy to clipboard, switch view to plain text mode 

    which I am trying to make Qt compatible

    Qt Code:
    1. QString Encrypter::encrypt(const QString& s)
    2. {
    3. QString result;
    4. bool processing = true;
    5. bool success = true;
    6.  
    7. QBuffer source;
    8. source.setData(s.toUtf8());
    9. QBuffer final;
    10.  
    11. unsigned char outbuff[OP_SIZE];
    12. int olen, tlen;
    13. unsigned char inbuff[IP_SIZE];
    14.  
    15. quint64 chunkSize = IP_SIZE;
    16. quint64 readSize;
    17.  
    18. EVP_CIPHER_CTX ctx;
    19. EVP_CIPHER_CTX_init (&ctx);
    20. EVP_EncryptInit (&ctx, EVP_bf_cbc (), key, iv);
    21.  
    22. while(processing)
    23. {
    24. //zero out input buffer
    25. for(int i = 0; i < IP_SIZE; i++)
    26. {
    27. inbuff[i] = 0;
    28. }
    29.  
    30. //populate input buffer with first chunk
    31. readSize = source.read((char*)inbuff, chunkSize);
    32.  
    33. if(readSize == 0)
    34. {
    35. processing = false;
    36. }
    37.  
    38. if(readSize == -1)
    39. {
    40. qDebug() << "encrypt read error";
    41. success = false;
    42. processing = false;
    43. break;
    44. }
    45.  
    46. if(processing)
    47. {
    48. if (EVP_EncryptUpdate (&ctx, outbuff, &olen, inbuff, readSize) != 1)
    49. {
    50. printf ("error in encrypt update\n");
    51. success = false;
    52. processing = false;
    53. }
    54. else
    55. {
    56. if (EVP_EncryptFinal (&ctx, outbuff + olen, &tlen) != 1)
    57. {
    58. printf ("error in encrypt final\n");
    59. success = false;
    60. processing = false;
    61. }
    62. }
    63.  
    64. olen += tlen;
    65.  
    66. readSize = final.write((char*)outbuff, olen);
    67. if( readSize == -1 )
    68. {
    69. perror ("write error");
    70. success = false;
    71. processing = false;
    72. }
    73. }
    74. }
    75. EVP_CIPHER_CTX_cleanup (&ctx);
    76.  
    77. if(success)
    78. {
    79. result = QString(final.data());
    80. }
    81.  
    82. //The encrypted string
    83. return result;
    84. }
    To copy to clipboard, switch view to plain text mode 

    But my function is failing right away with "encrypt read error". I know my solution isn't the most elegant but I was just hoping to get it working and then clean it up. I have a feeling problems are being caused by all of the different type conversions.

    Help would be appreciated

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Help converting a function to Qt

    The chain of transformations is very straightforward. It would be hard to find a more natural and simple situation to run through the function in a debugger in order to see when things go wrong...

    (Have you initialized IP_SIZE to something larger than zero?)

Similar Threads

  1. Replies: 2
    Last Post: 13th September 2010, 20:03
  2. Replies: 3
    Last Post: 25th May 2010, 09:46
  3. Replies: 0
    Last Post: 10th March 2010, 08:13
  4. Converting UIC 2 .h & .Cpp
    By jibolso in forum Newbie
    Replies: 5
    Last Post: 5th September 2009, 13:28
  5. Converting my UI to Qt4
    By Honestmath in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2006, 23:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.