Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. arrrruuugghhhhh cyrptoapi again

arrrruuugghhhhh cyrptoapi again

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomlinuxcryptographyhelp
14 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steven Hicks n 1
    wrote on last edited by
    #1

    (sorry for the repost but this is really bugging me still): The first time i call EncryptString (below) I pass "asdf" it encrypts to to be a length of 25 and encrypts it right. But when i try to encrypt a 2539 length string it returns a string of a length of 225 and only partically encrypted. Why is this happening and how do I fix it? CString CEncyptionClass::EncryptString(CString m_strUnencryptedString) { //variables //Date:11 22 03 CString m_strResultingText; HCRYPTPROV hProv = NULL; HCRYPTKEY hKey = NULL; HCRYPTHASH hHash = NULL; DWORD dwLength; BYTE * pbBuffer; LPTSTR m_tContents=ConvertCStringToChar(m_strUnencryptedString); TCHAR szLocalPassword[] = _T("alongrandompassword"); // // Get handle to user default provider. if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) { // Create hash object. if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) { // Hash password string. dwLength = _tcslen(szLocalPassword); if (CryptHashData(hHash, (BYTE *)szLocalPassword, dwLength, 0)) { // Create block cipher session key based on hash of the password. if (CryptDeriveKey(hProv, CALG_RC4, hHash, CRYPT_EXPORTABLE, &hKey)) { dwLength= _tcslen(m_tContents); pbBuffer=(BYTE*)malloc(dwLength); if (pbBuffer != NULL) { memcpy(pbBuffer, m_tContents, dwLength); if (CryptEncrypt(hKey, 0, true, 0, pbBuffer, &dwLength, dwLength)) { m_strResultingText.Format("%s",pbBuffer); CString strShow; strShow.Format("After: %s \r\n(%s)\r\n %d %d", m_strResultingText,m_tContents,strlen((const char*) pbBuffer) ,dwLength); MessageBox(strShow); } free(pbBuffer); } CryptDestroyKey(hKey); // Release provider handle. } } CryptDestroyHash(hHash); // Destroy session key. } CryptReleaseContext(hProv, 0); } return m_strResultingText; } -Steven Hicks

    CPA

    CodeProjectAddict

    Actual Linux Penguins were harmed in the creation of this message.

    More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

    I 1 Reply Last reply
    0
    • S Steven Hicks n 1

      (sorry for the repost but this is really bugging me still): The first time i call EncryptString (below) I pass "asdf" it encrypts to to be a length of 25 and encrypts it right. But when i try to encrypt a 2539 length string it returns a string of a length of 225 and only partically encrypted. Why is this happening and how do I fix it? CString CEncyptionClass::EncryptString(CString m_strUnencryptedString) { //variables //Date:11 22 03 CString m_strResultingText; HCRYPTPROV hProv = NULL; HCRYPTKEY hKey = NULL; HCRYPTHASH hHash = NULL; DWORD dwLength; BYTE * pbBuffer; LPTSTR m_tContents=ConvertCStringToChar(m_strUnencryptedString); TCHAR szLocalPassword[] = _T("alongrandompassword"); // // Get handle to user default provider. if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) { // Create hash object. if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) { // Hash password string. dwLength = _tcslen(szLocalPassword); if (CryptHashData(hHash, (BYTE *)szLocalPassword, dwLength, 0)) { // Create block cipher session key based on hash of the password. if (CryptDeriveKey(hProv, CALG_RC4, hHash, CRYPT_EXPORTABLE, &hKey)) { dwLength= _tcslen(m_tContents); pbBuffer=(BYTE*)malloc(dwLength); if (pbBuffer != NULL) { memcpy(pbBuffer, m_tContents, dwLength); if (CryptEncrypt(hKey, 0, true, 0, pbBuffer, &dwLength, dwLength)) { m_strResultingText.Format("%s",pbBuffer); CString strShow; strShow.Format("After: %s \r\n(%s)\r\n %d %d", m_strResultingText,m_tContents,strlen((const char*) pbBuffer) ,dwLength); MessageBox(strShow); } free(pbBuffer); } CryptDestroyKey(hKey); // Release provider handle. } } CryptDestroyHash(hHash); // Destroy session key. } CryptReleaseContext(hProv, 0); } return m_strResultingText; } -Steven Hicks

      CPA

      CodeProjectAddict

      Actual Linux Penguins were harmed in the creation of this message.

      More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

      I Offline
      I Offline
      igor1960
      wrote on last edited by
      #2

      You sure it's not MessageBox not showing full string?... I assume you may get not printable characters after encoding. "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

      S 2 Replies Last reply
      0
      • I igor1960

        You sure it's not MessageBox not showing full string?... I assume you may get not printable characters after encoding. "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

        S Offline
        S Offline
        Steven Hicks n 1
        wrote on last edited by
        #3

        The length of pbBuffer and m_strResultText is 225 .. the plain text string is 2539 -Steven Hicks

        CPA

        CodeProjectAddict

        Actual Linux Penguins were harmed in the creation of this message.

        More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

        I 1 Reply Last reply
        0
        • I igor1960

          You sure it's not MessageBox not showing full string?... I assume you may get not printable characters after encoding. "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

          S Offline
          S Offline
          Steven Hicks n 1
          wrote on last edited by
          #4

          The length of pbBuffer and m_strResultText is 225 .. the plain text string is 2539, also it encrypts: "something" fine but then when it gets called to encrypt the 2539 length string it messes up and encodes/returns only 225. -Steven Hicks

          CPA

          CodeProjectAddict

          Actual Linux Penguins were harmed in the creation of this message.

          More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

          1 Reply Last reply
          0
          • S Steven Hicks n 1

            The length of pbBuffer and m_strResultText is 225 .. the plain text string is 2539 -Steven Hicks

            CPA

            CodeProjectAddict

            Actual Linux Penguins were harmed in the creation of this message.

            More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

            I Offline
            I Offline
            igor1960
            wrote on last edited by
            #5

            That means according to your code: LPTSTR m_tContents=ConvertCStringToChar(m_strUnencryptedString); returns 225 length string... Check ConvertCStringToChar function. "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

            S 1 Reply Last reply
            0
            • I igor1960

              That means according to your code: LPTSTR m_tContents=ConvertCStringToChar(m_strUnencryptedString); returns 225 length string... Check ConvertCStringToChar function. "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

              S Offline
              S Offline
              Steven Hicks n 1
              wrote on last edited by
              #6

              It's string is 2539 also, and its right. -Steven Hicks

              CPA

              CodeProjectAddict

              Actual Linux Penguins were harmed in the creation of this message.

              More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

              I 1 Reply Last reply
              0
              • S Steven Hicks n 1

                It's string is 2539 also, and its right. -Steven Hicks

                CPA

                CodeProjectAddict

                Actual Linux Penguins were harmed in the creation of this message.

                More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

                I Offline
                I Offline
                igor1960
                wrote on last edited by
                #7

                Now I'm a little bit confused: You are saying: I pass "asdf" it encrypts to to be a length of 25 and encrypts it right. But according to your code: dwLength= _tcslen(m_tContents); pbBuffer=(BYTE*)malloc(dwLength); if (pbBuffer != NULL) { memcpy(pbBuffer, m_tContents, dwLength); if (CryptEncrypt(hKey, 0, true, 0, pbBuffer, &dwLength, dwLength)) You are only allocating 4 bytes in pbBuffer, as dwLength=4. Right? How you get 25? Maybe I missunderstanding... "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

                S 1 Reply Last reply
                0
                • I igor1960

                  Now I'm a little bit confused: You are saying: I pass "asdf" it encrypts to to be a length of 25 and encrypts it right. But according to your code: dwLength= _tcslen(m_tContents); pbBuffer=(BYTE*)malloc(dwLength); if (pbBuffer != NULL) { memcpy(pbBuffer, m_tContents, dwLength); if (CryptEncrypt(hKey, 0, true, 0, pbBuffer, &dwLength, dwLength)) You are only allocating 4 bytes in pbBuffer, as dwLength=4. Right? How you get 25? Maybe I missunderstanding... "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

                  S Offline
                  S Offline
                  Steven Hicks n 1
                  wrote on last edited by
                  #8

                  The first time that m_Contents is "asdf" it is encrypted to a 25 string (dwLength==25 for the orginal and after ) but then i pass a 2539 character long string to it and it encrypts only 255 (the dwLenght is also at 2539 when it goes to crypt encrypt -Steven Hicks

                  CPA

                  CodeProjectAddict

                  Actual Linux Penguins were harmed in the creation of this message.

                  More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

                  I 1 Reply Last reply
                  0
                  • S Steven Hicks n 1

                    The first time that m_Contents is "asdf" it is encrypted to a 25 string (dwLength==25 for the orginal and after ) but then i pass a 2539 character long string to it and it encrypts only 255 (the dwLenght is also at 2539 when it goes to crypt encrypt -Steven Hicks

                    CPA

                    CodeProjectAddict

                    Actual Linux Penguins were harmed in the creation of this message.

                    More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

                    I Offline
                    I Offline
                    igor1960
                    wrote on last edited by
                    #9

                    But according to your code with m_tContents="asdf": dwLength= _tcslen(m_tContents); // <= will give you 4 pbBuffer=(BYTE*)malloc(dwLength); // <= pbBuffer should be allocated with 4 bytes if (pbBuffer != NULL) { memcpy(pbBuffer, m_tContents, dwLength); if (CryptEncrypt(hKey, 0, true, 0, pbBuffer, &dwLength, dwLength)) // <= should encrypt just 4 bytes How you get 25, and if dwLength=25 after the call your pbBuffer is overrun! However you passed dwLength = 4, your CryptEncrypt either should fail or your pbBuffer should be overrun How are you getting 25?... Could you be more specific? "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

                    S 1 Reply Last reply
                    0
                    • I igor1960

                      But according to your code with m_tContents="asdf": dwLength= _tcslen(m_tContents); // <= will give you 4 pbBuffer=(BYTE*)malloc(dwLength); // <= pbBuffer should be allocated with 4 bytes if (pbBuffer != NULL) { memcpy(pbBuffer, m_tContents, dwLength); if (CryptEncrypt(hKey, 0, true, 0, pbBuffer, &dwLength, dwLength)) // <= should encrypt just 4 bytes How you get 25, and if dwLength=25 after the call your pbBuffer is overrun! However you passed dwLength = 4, your CryptEncrypt either should fail or your pbBuffer should be overrun How are you getting 25?... Could you be more specific? "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

                      S Offline
                      S Offline
                      Steven Hicks n 1
                      wrote on last edited by
                      #10

                      noooo... m_tContents would equal "<password>asdf</password>" sorry about that .... ugh -Steven Hicks

                      CPA

                      CodeProjectAddict

                      Actual Linux Penguins were harmed in the creation of this message.

                      More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

                      I 1 Reply Last reply
                      0
                      • S Steven Hicks n 1

                        noooo... m_tContents would equal "<password>asdf</password>" sorry about that .... ugh -Steven Hicks

                        CPA

                        CodeProjectAddict

                        Actual Linux Penguins were harmed in the creation of this message.

                        More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

                        I Offline
                        I Offline
                        igor1960
                        wrote on last edited by
                        #11

                        If interested: Please provide ConvertCStringToChar() implementation.... "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

                        S 1 Reply Last reply
                        0
                        • I igor1960

                          If interested: Please provide ConvertCStringToChar() implementation.... "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

                          S Offline
                          S Offline
                          Steven Hicks n 1
                          wrote on last edited by
                          #12

                          LPTSTR CEncyptionClass::ConvertCStringToChar(CString string_to_be_converted) { LPTSTR return_value=new TCHAR[string_to_be_converted.GetLength()+1]; _tcscpy(return_value,string_to_be_converted); return return_value; } -Steven Hicks

                          CPA

                          CodeProjectAddict

                          Actual Linux Penguins were harmed in the creation of this message.

                          More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

                          I 1 Reply Last reply
                          0
                          • S Steven Hicks n 1

                            LPTSTR CEncyptionClass::ConvertCStringToChar(CString string_to_be_converted) { LPTSTR return_value=new TCHAR[string_to_be_converted.GetLength()+1]; _tcscpy(return_value,string_to_be_converted); return return_value; } -Steven Hicks

                            CPA

                            CodeProjectAddict

                            Actual Linux Penguins were harmed in the creation of this message.

                            More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

                            I Offline
                            I Offline
                            igor1960
                            wrote on last edited by
                            #13

                            I'm almost giving up. But check this: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/security/cryptencrypt.asppadding A string, typically added when the last plaintext block is short. For example, if the block length is 64 bits and the last block contains only 40 bits, then 24 bits of padding must be added to the last block. The padding string may contain zeros, alternating zeros and ones, or some other pattern. Applications that use CryptoAPI need not add padding to their plaintext before it is encrypted, nor do they have to remove it after decrypting. This is all handled automatically So, maybe you need to increase dwLength and make it even with 8 bytes length. However, why CryptEncrypt is not failing then? Or maybe it does? "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

                            S 1 Reply Last reply
                            0
                            • I igor1960

                              I'm almost giving up. But check this: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/security/cryptencrypt.asppadding A string, typically added when the last plaintext block is short. For example, if the block length is 64 bits and the last block contains only 40 bits, then 24 bits of padding must be added to the last block. The padding string may contain zeros, alternating zeros and ones, or some other pattern. Applications that use CryptoAPI need not add padding to their plaintext before it is encrypted, nor do they have to remove it after decrypting. This is all handled automatically So, maybe you need to increase dwLength and make it even with 8 bytes length. However, why CryptEncrypt is not failing then? Or maybe it does? "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

                              S Offline
                              S Offline
                              Steven Hicks n 1
                              wrote on last edited by
                              #14

                              GetLastError doesn't return an error. The 2539 string is the only thing that messes up for some reason like i said it only encrypts 225 (thats whats returned from pbBuffer with dwLength set by strlen to 2539) and when passed through the decrypt function it returns the first 225 plaintext characters nothing after that -Steven Hicks

                              CPA

                              CodeProjectAddict

                              Actual Linux Penguins were harmed in the creation of this message.

                              More tutorials: Ltpb.8m.com: Tutorials |404Browser.com (Download Link)

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups