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. CryptDecrypt Failure please help

CryptDecrypt Failure please help

Scheduled Pinned Locked Moved C / C++ / MFC
help
14 Posts 4 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.
  • M monsieur_jj

    Ooops yep I did and I get a bad data error i forgot the number, i did put getlasterror i wasnt able to paste it here.

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #5

    Please post: (1) the name of the failing function. (2) the return value of GetLastError(). :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    In testa che avete, signor di Ceprano?

    1 Reply Last reply
    0
    • M monsieur_jj

      Hi all, First of all props to Randor for helping me and giving me a base code now I modified his code and this is it:

      #include <iostream>
      #include <tchar.h>
      #include <windows.h>
      #include <Wincrypt.h>
      #include <Xenroll.h>
      #include <atlenc.h>
      #include <atlcrypt.h>

      #pragma comment(lib, "crypt32.lib")//n7M41mbWvrA=
      BYTE* Base64Decode2(LPCTSTR lpData, DWORD dwSize, DWORD *dwBufSize)
      {
      DWORD dwResult = 0;
      if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult,NULL,NULL))
      {
      BYTE * decodedStr;
      decodedStr = new BYTE;
      LPTSTR lpszBase64Decoded = new TCHAR[dwResult+(sizeof(TCHAR) * 2)];
      memset(decodedStr,0,dwResult);
      if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64,decodedStr, &dwResult,NULL,NULL))
      {
      *dwBufSize = dwResult;
      return decodedStr;
      }
      }
      return NULL;
      }

      LPTSTR Base64Encode2(BYTE* lpData, DWORD dwSize, BOOL bStripCRLF, DWORD *dwbufSize)
      {
      DWORD dwResult = 0;
      if(CryptBinaryToString(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult))
      {
      LPTSTR lpszBase64 = new TCHAR[dwResult];
      if(CryptBinaryToString(lpData, dwSize, CRYPT_STRING_BASE64, lpszBase64, &dwResult))
      {
      TCHAR pByteLF = *(LPWORD)(lpszBase64 + dwResult -1);
      TCHAR pByteCR = *(LPWORD)(lpszBase64 + dwResult -2);
      if(pByteCR == 0x0D && pByteLF == 0x0A)
      {
      *(LPWORD)(lpszBase64 + dwResult -2) = 0;
      }
      *dwbufSize = dwResult;
      return lpszBase64;
      }
      }
      return NULL;
      }
      DWORD TripleDESdecrypt2(TCHAR *cyphertext,DWORD ctlen,TCHAR *passwd,DWORD pwlen,BYTE *plaintext,DWORD *ptlen)
      {
      HCRYPTPROV hProv;
      HCRYPTHASH hHash;
      HCRYPTKEY hKey;
      DWORD dwSizeNeeded =0;

      CryptAcquireContext(&hProv,NULL,MS\_STRONG\_PROV,PROV\_RSA\_FULL,0);
      CryptCreateHash(hProv,CALG\_MD5,0,0,&hHash);
      CryptHashData(hHash,(BYTE \*)passwd,pwlen,0);
      CryptDeriveKey(hProv,CALG\_3DES,hHash,0,&hKey);
      DWORD dMode = CRYPT\_MODE\_ECB;
          CryptSetKeyParam(hKey, PKCS5\_PADDING, reinterpret\_cast<const BYTE \*>(&dMode), 0);
      if(\*ptlen >= ctlen)
      {
      
      	memcpy(plaintext,cyphertext,\*ptlen);
      	BOOL result = CryptDecrypt(hKey,NULL,1,0,plaintext,&ctlen);
      	\*ptlen=ctlen;
      }
      else
      {
      	dwSizeNeeded = ctlen \* sizeof(TCHAR);
      }
      CryptDestroyKey(hKey);
      CryptDestroyHash(hHash);
      CryptReleaseContext(hProv,0);
      return dwSizeNeeded;
      

      }
      int main()
      {

      BYTE\* decodedStr;
      HCRYPTPROV hCryptProv;
      HCRYPTHASH hHash = 0;
      HCRYPTKEY hKey = 0;
      
      TCHAR szKey\[\] = \_T("h3bmull3r
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #6

      JayJay, The code I posted yesterday [^]works in both Unicode and ANSI builds. The only thing I did not give you is UTF8 conversion[^] because I do not know what codepage[^] your C# application is using. The Base64 string "n7M41mbWvrA=" does not appear to be standard Unicode or ANSI when converted from Base64 so you cannot convert it into a readable string until you match the codepage[^]. Best Wishes, -David Delaune

      M 1 Reply Last reply
      0
      • L Lost User

        JayJay, The code I posted yesterday [^]works in both Unicode and ANSI builds. The only thing I did not give you is UTF8 conversion[^] because I do not know what codepage[^] your C# application is using. The Base64 string "n7M41mbWvrA=" does not appear to be standard Unicode or ANSI when converted from Base64 so you cannot convert it into a readable string until you match the codepage[^]. Best Wishes, -David Delaune

        M Offline
        M Offline
        monsieur_jj
        wrote on last edited by
        #7

        Hi David , This is the whole solution of the c# application used for the encryption: Link[^] Yup I have tested it and kinda reversed engineered it to imitate the c# conversion, I just revised it a bit that both byte[] toEncryptArray = Convert.FromBase64String(toEncrypt); and return Convert.ToBase64String(resultArray, 0, resultArray.Length); are used instead of using the UTF8 conversion although it is still being used when converting the key. Please enlighten me Thanks, Jayjay

        L 1 Reply Last reply
        0
        • M monsieur_jj

          Hi David , This is the whole solution of the c# application used for the encryption: Link[^] Yup I have tested it and kinda reversed engineered it to imitate the c# conversion, I just revised it a bit that both byte[] toEncryptArray = Convert.FromBase64String(toEncrypt); and return Convert.ToBase64String(resultArray, 0, resultArray.Length); are used instead of using the UTF8 conversion although it is still being used when converting the key. Please enlighten me Thanks, Jayjay

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #8

          Hello JayJay, Now that I have looked at the C# project I think I know what the problem is. The C# code is using ECB (Electronic Code Book) and PKCS-7. They are using entirely different 3DES modes and padding. Its getting late here, I will look at it again tomorrow. If you want to experiment you can start with changing modes in the code I gave you:BYTE nMode = CRYPT_MODE_ECB; CryptSetKeyParam(hKey,KP_MODE,&nMode,0);
          You may also need to generate a temporary BLOBHEADER of type PLAINTEXTKEYBLOB for this mode. I'm not really sure. Good Luck, -David Delaune

          M 2 Replies Last reply
          0
          • L Lost User

            Hello JayJay, Now that I have looked at the C# project I think I know what the problem is. The C# code is using ECB (Electronic Code Book) and PKCS-7. They are using entirely different 3DES modes and padding. Its getting late here, I will look at it again tomorrow. If you want to experiment you can start with changing modes in the code I gave you:BYTE nMode = CRYPT_MODE_ECB; CryptSetKeyParam(hKey,KP_MODE,&nMode,0);
            You may also need to generate a temporary BLOBHEADER of type PLAINTEXTKEYBLOB for this mode. I'm not really sure. Good Luck, -David Delaune

            M Offline
            M Offline
            monsieur_jj
            wrote on last edited by
            #9

            Hi David, Thanks I have also tried that as you can see at the code I posted, where will I use the blobheader? Is it just a container of the modes and 3des algorithm? Thanks for the continuous help, Jayjay

            1 Reply Last reply
            0
            • L Lost User

              Hello JayJay, Now that I have looked at the C# project I think I know what the problem is. The C# code is using ECB (Electronic Code Book) and PKCS-7. They are using entirely different 3DES modes and padding. Its getting late here, I will look at it again tomorrow. If you want to experiment you can start with changing modes in the code I gave you:BYTE nMode = CRYPT_MODE_ECB; CryptSetKeyParam(hKey,KP_MODE,&nMode,0);
              You may also need to generate a temporary BLOBHEADER of type PLAINTEXTKEYBLOB for this mode. I'm not really sure. Good Luck, -David Delaune

              M Offline
              M Offline
              monsieur_jj
              wrote on last edited by
              #10

              Hi David, What do you think that can help me know? The c# developer said it is standard so it can be fully translated to c++ Thanks, Jayjay

              L 1 Reply Last reply
              0
              • M monsieur_jj

                Hi David, What do you think that can help me know? The c# developer said it is standard so it can be fully translated to c++ Thanks, Jayjay

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #11

                Hi Jayjay, I was unsuccessful with setting the mode to ECB and padding to PKCS #7. I'm afraid you will have to research why this is failing with error code NTE_BAD_ALGID which is to say "Invalid algorithm specified". Perhaps the default cryptographic service provider does not support Triple ECB or maybe I am doing something incorrect. Best Wishes, -David Delaune

                M 1 Reply Last reply
                0
                • L Lost User

                  Hi Jayjay, I was unsuccessful with setting the mode to ECB and padding to PKCS #7. I'm afraid you will have to research why this is failing with error code NTE_BAD_ALGID which is to say "Invalid algorithm specified". Perhaps the default cryptographic service provider does not support Triple ECB or maybe I am doing something incorrect. Best Wishes, -David Delaune

                  M Offline
                  M Offline
                  monsieur_jj
                  wrote on last edited by
                  #12

                  Hi David, Is PKCS5 of c++ win api the same ast PKCS 7 of C# or .net? I will research on this, or I have to ask to change the way of encrypting. Thanks, Jayjay

                  L 1 Reply Last reply
                  0
                  • M monsieur_jj

                    Hi David, Is PKCS5 of c++ win api the same ast PKCS 7 of C# or .net? I will research on this, or I have to ask to change the way of encrypting. Thanks, Jayjay

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #13

                    I looked over the RSA documentation and found that they are the same. The RSA standards documents are here: Look at PKCS #5: ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-5v2/pkcs5v2-0.doc The padding method is described in section 6.1.1 and it states that the padding values are equal to the length of the message modulo 8. So lets imagine the message you want to encrypt is 60 bytes long. (60 mod 8) is equal to 0x4 So your padding would be: 0x4 0x4 0x4 0x4 Pretty easy huh? If you take a look at the padding method described in section 10.3 in the PKCS #7 documents it outlines the same padding. You can verify it here: ftp://ftp.rsasecurity.com/pub/pkcs/doc/pkcs-7.doc So yes, PKCS #5 is exactly the same padding as PKCS #7. Best Wishes, -David Delaune

                    M 1 Reply Last reply
                    0
                    • L Lost User

                      I looked over the RSA documentation and found that they are the same. The RSA standards documents are here: Look at PKCS #5: ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-5v2/pkcs5v2-0.doc The padding method is described in section 6.1.1 and it states that the padding values are equal to the length of the message modulo 8. So lets imagine the message you want to encrypt is 60 bytes long. (60 mod 8) is equal to 0x4 So your padding would be: 0x4 0x4 0x4 0x4 Pretty easy huh? If you take a look at the padding method described in section 10.3 in the PKCS #7 documents it outlines the same padding. You can verify it here: ftp://ftp.rsasecurity.com/pub/pkcs/doc/pkcs-7.doc So yes, PKCS #5 is exactly the same padding as PKCS #7. Best Wishes, -David Delaune

                      M Offline
                      M Offline
                      monsieur_jj
                      wrote on last edited by
                      #14

                      Hi David, I am kinda confused about how the padding will be can you explain how will the code look like? Thanks, jayjay

                      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