Results 1 to 2 of 2

Thread: Microsoft PasswordDeriveBytes implementation in Qt5/C++

  1. #1
    Join Date
    Nov 2012
    Location
    Poland/UK
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Microsoft PasswordDeriveBytes implementation in Qt5/C++

    Hi, we are trying to rewrite the function PasswordDeriveBytes from C#, but Microsoft function is not standard implementation of PKCS... it's extended...
    We need this implementation to decode 3DES strings in new version of our application

    C#:
    Qt Code:
    1. String crc...
    2. PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(crc, b);
    3.  
    4. byte[] u = passwordDeriveBytes.GetBytes(24);
    5. byte[] iv = passwordDeriveBytes.GetBytes(8);
    To copy to clipboard, switch view to plain text mode 

    Qt:
    Qt Code:
    1. QString crc..
    2. Tools::PasswordDeriveBytes passwordDeriveBytes((byte *)crc.toUtf8().data(), b);
    3. byte *key = passwordDeriveBytes.GetBytes(24);
    4. byte *iv = passwordDeriveBytes.GetBytes(8);
    To copy to clipboard, switch view to plain text mode 

    PasswordDeriveBytes C++ Implementation found on web...

    Qt Code:
    1. std::string Tools::PasswordDeriveBytes::convertInt(int number)
    2. {
    3. if (number == 0)
    4. return "0";
    5. std::string temp="";
    6. std::string returnvalue="";
    7. while (number>0)
    8. {
    9. temp+=number%10+48;
    10. number/=10;
    11. }
    12. for (unsigned int i=0; i<temp.length(); i++)
    13. returnvalue+=temp[temp.length()-i-1];
    14. return returnvalue;
    15. }
    16.  
    17. void Tools::PasswordDeriveBytes::Prepare(unsigned char *password, unsigned char *salt, int iterations)
    18. {
    19. if (password == NULL)
    20. return;
    21.  
    22. Prepare(password, strlen((const char*)password), salt, strlen((const char*)salt), iterations);
    23. }
    24.  
    25.  
    26. void Tools::PasswordDeriveBytes::Prepare(unsigned char* password, int pass_len, unsigned char* salt, int salt_len, int iterations)
    27. {
    28. if (password == NULL)
    29. return;
    30.  
    31. this->password = new unsigned char[pass_len];
    32. memcpy(this->password,password,pass_len);
    33. //memcpy((char *)this->password, (const char*)password, pass_len);
    34. this->pass_len = pass_len;
    35. //(unsigned char*)password.Clone();
    36.  
    37. this->salt = new unsigned char[salt_len];
    38. //strncpy((char *)this->salt, (const char*)salt, salt_len);
    39. memcpy(this->salt,salt,salt_len);
    40. this->salt_len = salt_len;
    41.  
    42. this->IterationCount = iterations;
    43. state = 0;
    44. }
    45.  
    46. unsigned char *Tools::PasswordDeriveBytes::GetBytes(int cb)
    47. {
    48. if (cb < 1)
    49. return NULL;
    50.  
    51. if (state == 0)
    52. {
    53. // it's now impossible to change the HashName, Salt
    54. // and IterationCount
    55. Reset();
    56. state = 1;
    57. }
    58.  
    59. unsigned char* result = new unsigned char[cb];
    60. int cpos = 0;
    61. // the initial hash (in reset) + at least one iteration
    62. int iter = IterationCount-1;
    63. if (iter < 1)
    64. {
    65. iter = 1;
    66. }
    67.  
    68. // start with the PKCS5 key
    69. if (this->output == NULL)
    70. {
    71. // calculate the PKCS5 key
    72. this->output = initial;
    73. this->output_len = SHA1_BYTES_LEN;
    74.  
    75. // generate new key material
    76. for (int i = 0; i < iter - 1; i++)
    77. {
    78. SHA1((const unsigned char*)this->output,this->output_len,this->output);
    79. this->output_len = SHA1_BYTES_LEN;
    80. }
    81. }
    82.  
    83. while (cpos < cb)
    84. {
    85. unsigned char* output2 = new unsigned char[SHA1_BYTES_LEN];
    86. unsigned int output2_len = SHA1_BYTES_LEN;
    87. if (hashnumber == 0)
    88. {
    89. SHA1((const unsigned char*)this->output,this->output_len,output2);
    90. output2_len = SHA1_BYTES_LEN;
    91. }
    92. else if (hashnumber < 1000)
    93. {
    94. std::string n = convertInt(hashnumber);
    95. output2 = new unsigned char[this->output_len + n.length()];
    96. output2_len = this->output_len + n.length();
    97. for (unsigned int j = 0; j < n.length(); j++)
    98. output2[j] = (unsigned char)(n[j]);
    99.  
    100. memcpy(output2 + n.length(),this->output,this->output_len);
    101. SHA1((const unsigned char*)output2,output2_len,output2);
    102. output2_len = SHA1_BYTES_LEN;
    103. }
    104. else
    105. {
    106. return NULL;
    107. }
    108.  
    109. int rem = this->output_len - this->position;
    110. int l = cb - cpos;
    111. if (l > rem)
    112. {
    113. l = rem;
    114. }
    115. memcpy(result + cpos, output2 + this->position, l);
    116. cpos += l;
    117. this->position += l;
    118. while (this->position >= output2_len)
    119. {
    120. this->position -= output2_len;
    121. this->hashnumber++;
    122. }
    123. }
    124. return result;
    125. }
    126.  
    127. void Tools::PasswordDeriveBytes::Reset()
    128. {
    129. this->state = 0;
    130. this->position = 0;
    131. this->hashnumber = 0;
    132. this->initial = new unsigned char[SHA1_BYTES_LEN];
    133. this->output = NULL;
    134. this->output_len = 0;
    135. if (this->salt != NULL)
    136. {
    137. unsigned char* rv = new unsigned char[this->pass_len + this->salt_len];
    138. memcpy(rv,this->password, this->pass_len);
    139. memcpy(rv + this->pass_len, this->salt, this->salt_len);
    140. SHA1((const unsigned char*)rv,this->pass_len + this->salt_len, initial);
    141.  
    142. }
    143. else
    144. {
    145. SHA1((const unsigned char*)this->password,this->pass_len,initial);
    146. }
    147. }
    To copy to clipboard, switch view to plain text mode 

    header
    Qt Code:
    1. class PasswordDeriveBytes {
    2. private:
    3. unsigned char* password;
    4. int pass_len;
    5. unsigned char* salt;
    6. int salt_len;
    7. int IterationCount;
    8. int state;
    9. unsigned char* initial;
    10. unsigned char* output;
    11. unsigned int output_len;
    12. unsigned int position;
    13. int hashnumber;
    14.  
    15. public:
    16. PasswordDeriveBytes(unsigned char* password, unsigned char* salt, int iterations = 100)
    17. {
    18. Prepare(password, salt, iterations);
    19. }
    20.  
    21. private:
    22. std::string convertInt(int number);
    23. void Prepare(unsigned char* password, unsigned char* salt, int iterations);
    24. void Prepare(unsigned char* password, int pass_len, unsigned char* salt, int salt_len, int iterations);
    25.  
    26. public:
    27. unsigned char* GetBytes(int cb);
    28. void Reset();
    29. };
    To copy to clipboard, switch view to plain text mode 

    and Mono version (C#)

    Qt Code:
    1. https://raw.githubusercontent.com/mono/mono/master/mcs/class/corlib/System.Security.Cryptography/PasswordDeriveBytes.cs
    To copy to clipboard, switch view to plain text mode 
    returned byte arrays are not identical in the two implementations
    We don't know how fix this problem..

    Regards and thanks for any reply
    Last edited by Coder5546; 31st July 2014 at 21:05.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Microsoft PasswordDeriveBytes implementation in Qt5/C++

    How is this a Qt question? You should ask this on StackOverflow or some other programming forum.

Similar Threads

  1. Replies: 10
    Last Post: 5th August 2013, 12:16
  2. I'm interested to buy/use QT. What I need from microsoft ?
    By peppe in forum Installation and Deployment
    Replies: 2
    Last Post: 20th October 2011, 21:42
  3. Qt is going to be slaughtered by Microsoft
    By genjix in forum General Discussion
    Replies: 1
    Last Post: 13th February 2011, 05:53
  4. How to use Microsoft COM interfaces in QT?
    By luochen601 in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2010, 04:22
  5. Qt4.3.2 and Microsoft SDK
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2008, 14:38

Tags for this Thread

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.