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. Please help me with my decryption it almost works

Please help me with my decryption it almost works

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 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 Offline
    M Offline
    monsieur_jj
    wrote on last edited by
    #1

    This is my code: It gives me true when I perform the function crypt decrypt its just that I am expecting this output : aHR0cDovL2xvY2FsaG9zdDo4MDgwL015V2ViL21hbmFnZXBvaW50L21hbmFnZXBvaW50MzQxLnppcA== Then when I decode it Ill should get a link but instead cryptdecrypt gives me trash which I cant convert to the right link

    LPTSTR Base64Decode(LPCTSTR lpData, DWORD dwSize)
    {
    DWORD dwResult = 0;
    if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult,NULL,NULL))
    {
    LPTSTR lpszBase64Decoded = new TCHAR[dwResult+(sizeof(TCHAR) * 2)];
    memset(lpszBase64Decoded,0,dwResult);
    if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64,(BYTE *)lpszBase64Decoded, &dwResult,NULL,NULL))
    {
    *(LPWORD)(lpszBase64Decoded + (dwResult / sizeof(TCHAR))) = 0;

    		return lpszBase64Decoded;
    	}
    }
    return NULL;
    

    }

    LPTSTR Base64Encode(LPCBYTE 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;
    }

    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)(

    CPalliniC L L 3 Replies Last reply
    0
    • M monsieur_jj

      This is my code: It gives me true when I perform the function crypt decrypt its just that I am expecting this output : aHR0cDovL2xvY2FsaG9zdDo4MDgwL015V2ViL21hbmFnZXBvaW50L21hbmFnZXBvaW50MzQxLnppcA== Then when I decode it Ill should get a link but instead cryptdecrypt gives me trash which I cant convert to the right link

      LPTSTR Base64Decode(LPCTSTR lpData, DWORD dwSize)
      {
      DWORD dwResult = 0;
      if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult,NULL,NULL))
      {
      LPTSTR lpszBase64Decoded = new TCHAR[dwResult+(sizeof(TCHAR) * 2)];
      memset(lpszBase64Decoded,0,dwResult);
      if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64,(BYTE *)lpszBase64Decoded, &dwResult,NULL,NULL))
      {
      *(LPWORD)(lpszBase64Decoded + (dwResult / sizeof(TCHAR))) = 0;

      		return lpszBase64Decoded;
      	}
      }
      return NULL;
      

      }

      LPTSTR Base64Encode(LPCBYTE 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;
      }

      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)(

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

      Actually your code is failing (at least) in the following line:

      monsieur_jj wrote:

      pszBase64Decoded = Base64Decode(psz3DESDecoded,_tcslen(psz3DESDecoded) * sizeof(TCHAR));

      (CryptStringToBinary returns 0. BTW: are you using the debugger?). Moreover the following comparison

      monsieur_jj wrote:

      _tcscmp(pszBase64Decoded,szUnencrypted)

      makes no sense, since szUnencrypted is a buffer never initialised. :)

      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?

      M 1 Reply Last reply
      0
      • M monsieur_jj

        This is my code: It gives me true when I perform the function crypt decrypt its just that I am expecting this output : aHR0cDovL2xvY2FsaG9zdDo4MDgwL015V2ViL21hbmFnZXBvaW50L21hbmFnZXBvaW50MzQxLnppcA== Then when I decode it Ill should get a link but instead cryptdecrypt gives me trash which I cant convert to the right link

        LPTSTR Base64Decode(LPCTSTR lpData, DWORD dwSize)
        {
        DWORD dwResult = 0;
        if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult,NULL,NULL))
        {
        LPTSTR lpszBase64Decoded = new TCHAR[dwResult+(sizeof(TCHAR) * 2)];
        memset(lpszBase64Decoded,0,dwResult);
        if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64,(BYTE *)lpszBase64Decoded, &dwResult,NULL,NULL))
        {
        *(LPWORD)(lpszBase64Decoded + (dwResult / sizeof(TCHAR))) = 0;

        		return lpszBase64Decoded;
        	}
        }
        return NULL;
        

        }

        LPTSTR Base64Encode(LPCBYTE 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;
        }

        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)(

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, FWIW the encoded string is probably OK, since it decodes to http://localhost:8080/MyWeb/managepoint/managepoint341.zip it took 2 lines of C# code to get that. Not sure what is wrong in your code though. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Fixturized forever. :confused:


        1 Reply Last reply
        0
        • CPalliniC CPallini

          Actually your code is failing (at least) in the following line:

          monsieur_jj wrote:

          pszBase64Decoded = Base64Decode(psz3DESDecoded,_tcslen(psz3DESDecoded) * sizeof(TCHAR));

          (CryptStringToBinary returns 0. BTW: are you using the debugger?). Moreover the following comparison

          monsieur_jj wrote:

          _tcscmp(pszBase64Decoded,szUnencrypted)

          makes no sense, since szUnencrypted is a buffer never initialised. :)

          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]

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

          Hi Pallini, Sorry never mind the message box output i forgot to comment that. Can anyone tell me the difference between the C# code and mine? Im pretty sure pkcs5 is the same at pkcs7 and i got all inputs the same. Thanks, Jayjay

          1 Reply Last reply
          0
          • M monsieur_jj

            This is my code: It gives me true when I perform the function crypt decrypt its just that I am expecting this output : aHR0cDovL2xvY2FsaG9zdDo4MDgwL015V2ViL21hbmFnZXBvaW50L21hbmFnZXBvaW50MzQxLnppcA== Then when I decode it Ill should get a link but instead cryptdecrypt gives me trash which I cant convert to the right link

            LPTSTR Base64Decode(LPCTSTR lpData, DWORD dwSize)
            {
            DWORD dwResult = 0;
            if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult,NULL,NULL))
            {
            LPTSTR lpszBase64Decoded = new TCHAR[dwResult+(sizeof(TCHAR) * 2)];
            memset(lpszBase64Decoded,0,dwResult);
            if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64,(BYTE *)lpszBase64Decoded, &dwResult,NULL,NULL))
            {
            *(LPWORD)(lpszBase64Decoded + (dwResult / sizeof(TCHAR))) = 0;

            		return lpszBase64Decoded;
            	}
            }
            return NULL;
            

            }

            LPTSTR Base64Encode(LPCBYTE 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;
            }

            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)(

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

            Hi JayJay, As I stated in this post[^] I suspect your default Microsoft cryptographic service provider does not support Triple ECB. I am only speculating as I have not spent much time researching this. I suggest that you enumerate all supported algorithms on your development box. You can use the CryptEnumProviders Function[^] to see all the CSP. You can use the CryptEnumProviderTypes Function[^] to see all supported algorithms. If 3DES ECB is not listed then your CSP may not support it. Example C Program: Enumerating CSP Providers and Provider Types[^] You have spent a large amount of time trying to make this work with Microsoft API, perhaps you should begin looking at third party 3DES implementations. Here are a few I have found using Google. http://www.codeguru.com/cpp/misc/misc/cryptoapi/print.php/c8195[^] http://www.cs.auckland.ac.nz/~pgut001/cryptlib/[^] Best Wishes, -David Delaune

            M 1 Reply Last reply
            0
            • L Lost User

              Hi JayJay, As I stated in this post[^] I suspect your default Microsoft cryptographic service provider does not support Triple ECB. I am only speculating as I have not spent much time researching this. I suggest that you enumerate all supported algorithms on your development box. You can use the CryptEnumProviders Function[^] to see all the CSP. You can use the CryptEnumProviderTypes Function[^] to see all supported algorithms. If 3DES ECB is not listed then your CSP may not support it. Example C Program: Enumerating CSP Providers and Provider Types[^] You have spent a large amount of time trying to make this work with Microsoft API, perhaps you should begin looking at third party 3DES implementations. Here are a few I have found using Google. http://www.codeguru.com/cpp/misc/misc/cryptoapi/print.php/c8195[^] http://www.cs.auckland.ac.nz/~pgut001/cryptlib/[^] Best Wishes, -David Delaune

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

              Hi David, These are the list of what is supported:

              Listing Available Providers:
              Provider type Provider Name
              _____________ _____________________________________
              1 Gemplus GemSAFE Card CSP v1.0
              1 Infineon SICRYPT Base Smart Card CSP
              1 Microsoft Base Cryptographic Provider v1.0
              13 Microsoft Base DSS and Diffie-Hellman Cryptographic Provider
              3 Microsoft Base DSS Cryptographic Provider
              18 Microsoft DH SChannel Cryptographic Provider
              1 Microsoft Enhanced Cryptographic Provider v1.0
              13 Microsoft Enhanced DSS and Diffie-Hellman Cryptographic Provider

                 24       Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype
              

              )
              5 Microsoft Exchange Cryptographic Provider v1.0
              12 Microsoft RSA SChannel Cryptographic Provider
              1 Microsoft Strong Cryptographic Provider
              1 Schlumberger Cryptographic Service Provider

              Provider types and provider names have been listed.
              Listing Available Provider Types:
              Provider type Provider Type Name
              _____________ _____________________________________
              1 RSA Full (Signature and Key Exchange)
              3 DSS Signature
              5 Microsoft Office Outlook/Exchange Cryptographic Service Provider

                 12       RSA SChannel
                 13       DSS Signature with Diffie-Hellman Key Exchange
                 18       Diffie-Hellman SChannel
                 24       RSA Full and AES
              

              I read at msdn that Microsoft Enhanced Cryptographic Provider v1.0 supports triple DES now I am confused to what the problem really is. 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