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. Converting encrypted data into string/decimal and vice versa

Converting encrypted data into string/decimal and vice versa

Scheduled Pinned Locked Moved C / C++ / MFC
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.
  • V vgandhi

    Hello, I am creating a challenge response application and in that making a call to encrypt a buffer that looks like PBYTE pBufPtr if(!Encrypt(hKey, 0, TRUE, 0,(BYTE *)pBufPtr, &dwCount, dwBufferLen)) In this case I want to take the encrypted data pBufPtr and display it as a string to the user. So any suggestions on how I can convert the pBufPtr into a decimal number or string that can be displayed to the user for typing purposes. Also the decimal or string value obtained by converting the pBufPtr needs to be converted back into the encrypted data for the response side of the application. So I need to convert encrypted data into string/decimal/BCD and take that string/decimal/BCD and convert it back into the encrypted data buffer. So please let me know how is this possible. Thank you. vg

    M Offline
    M Offline
    Michael Dunn
    wrote on last edited by
    #4

    If your app will be run on XP+, you can use CryptBinaryToString() and CryptStringToBinary()

    --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ");

    V 2 Replies Last reply
    0
    • R Rage

      Are you asking for a encryption/decription routine, or how to convert a byte pointer to something displayable ?

      V Offline
      V Offline
      vgandhi
      wrote on last edited by
      #5

      I am asking to convert the encrypted byte data to something displayable. The encrypted data looks like "Œ[Ü‹¼| £](%Î݇X/2A9›Žf˜Qvdýýýý««««««««îþîþ" so it has symbols such as ««««««« %  how do I convert the this binary data into something displayable so that it can be displayed on the screen for the user to read it. The idea is to create a challenge by encrypting the data. 2) This encrypted data user reads it out from the a dialog in some displayable form(this part I need help on) 3) That converted encrypted data is converted back to its binary form and used to create the response.

      vg

      1 Reply Last reply
      0
      • J James R Twine

        vgandhi wrote:

        In this case I want to take the encrypted data pBufPtr and display it as a string to the user. So any suggestions on how I can convert the pBufPtr into a decimal number or string that can be displayed to the user for typing purposes. Also the decimal or string value obtained by converting the pBufPtr needs to be converted back into the encrypted data for the response side of the application

        The simplest thing to do is to convert each byte of the encrypted data into two hexidecimal characters, so that a byte of the value of 'z' becomes the string of two characters: '7A', and you can extend this to each byte of encrypted data.  I.e 'ijk123' becomes '696A6B313233'.    If the encrypted data is of fixed length, for example, it will always be 10 characters of encrypted data, you can render it to hexidecimal and back again using functions like sprintf(...) and sscanf(...).

        ::sprintf( "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            btaEncData[ 0 ], btaEncData[ 1 ], btaEncData[ 2 ], btaEncData[ 3 ],
            btaEncData[ 4 ], btaEncData[ 5 ], btaEncData[ 6 ],
            btaEncData[ 7 ], btaEncData[ 8 ], btaEncData[ 9 ] );

        // ....

        ::sscanf( cpHexData, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            &btaEncData[ 0 ], &btaEncData[ 1 ], &btaEncData[ 2 ], &btaEncData[ 3 ],
            &btaEncData[ 4 ], &btaEncData[ 5 ], &btaEncData[ 6 ],
            &btaEncData[ 7 ], &btaEncData[ 8 ], &btaEncData[ 9 ] );

        That is a simple example, and not the most optimal, but it should give you the general idea.    You can reduce the amount of data the user has to type by converting the encrypted data buffer into blocks of 32-bit values (DWORDs) or 64-bit values (hypers), and using a function like ultoa(...) or _ui64toa(...) with a higher radix to reduce number of characters but use a wider range of them, and use the corresponding ato*(...) functions to convert them back.

        ultoa( 0xFFFFFFFF, caBuffer, 10 ); // = "4294967295"
        ultoa( 0xFFFFFFFF, caBuffer, 16 ); // = "ffffffff"
        ultoa( 0xFFFFFFFF, caBuffer, 36 ); // = "1z141z3"

        Peace!

        -=- James
        Please rate this message - let me know if I helpe

        V Offline
        V Offline
        vgandhi
        wrote on last edited by
        #6

        Dear James, Say I use the following ultoa( 0xFFFFFFFF, caBuffer, 10 ); and the value I get is "4294967295" Now how would I conver the 429496... number back into the original caBuffer form after making the above call. Please let me know. Thanks

        vg

        J V 2 Replies Last reply
        0
        • M Michael Dunn

          If your app will be run on XP+, you can use CryptBinaryToString() and CryptStringToBinary()

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ");

          V Offline
          V Offline
          vgandhi
          wrote on last edited by
          #7

          Yes but that gives me a long string how do I make that smaller ? Thanks.

          vg

          1 Reply Last reply
          0
          • V vgandhi

            Dear James, Say I use the following ultoa( 0xFFFFFFFF, caBuffer, 10 ); and the value I get is "4294967295" Now how would I conver the 429496... number back into the original caBuffer form after making the above call. Please let me know. Thanks

            vg

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #8

            vgandhi wrote:

            Now how would I conver the 429496... number back into the original caBuffer form after making the above call.

            As i said, the ato*(...) functions - for example, if you use ultoa(...) to convert to string (ltoa == long to ascii/ANSI), you can use the atol(...) to get it back to a number (atol == ASCII/ANSI to long).    If you look up those functions, you will see that there is no ato**u**l(...) function - but you will find the strto*(...) functions.  In this example, strotul(...) will convert it back.    Peace!

            -=- James
            Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            See DeleteFXPFiles

            V 1 Reply Last reply
            0
            • V vgandhi

              Dear James, Say I use the following ultoa( 0xFFFFFFFF, caBuffer, 10 ); and the value I get is "4294967295" Now how would I conver the 429496... number back into the original caBuffer form after making the above call. Please let me know. Thanks

              vg

              V Offline
              V Offline
              vgandhi
              wrote on last edited by
              #9

              Dear James, In the message above you said we can reduce the buffer by using the function ultoa. But my question is I have to first convert the buffer into ulong and then use the function ultoa. So how do I convert the buffer to ulong because the function ultoa takes in a ulong as its first parameter. So please let me know. Thanks.

              vg

              J 1 Reply Last reply
              0
              • M Michael Dunn

                If your app will be run on XP+, you can use CryptBinaryToString() and CryptStringToBinary()

                --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ");

                V Offline
                V Offline
                vgandhi
                wrote on last edited by
                #10

                Dear James, In the message above you said we can reduce the buffer by using the function ultoa. But my question is I have to first convert the buffer into ulong and then use the function ultoa. So how do I convert the buffer to ulong because the function ultoa takes in a ulong as its first parameter. So please let me know. Thanks.

                vg

                1 Reply Last reply
                0
                • J James R Twine

                  vgandhi wrote:

                  Now how would I conver the 429496... number back into the original caBuffer form after making the above call.

                  As i said, the ato*(...) functions - for example, if you use ultoa(...) to convert to string (ltoa == long to ascii/ANSI), you can use the atol(...) to get it back to a number (atol == ASCII/ANSI to long).    If you look up those functions, you will see that there is no ato**u**l(...) function - but you will find the strto*(...) functions.  In this example, strotul(...) will convert it back.    Peace!

                  -=- James
                  Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                  Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                  See DeleteFXPFiles

                  V Offline
                  V Offline
                  vgandhi
                  wrote on last edited by
                  #11

                  Hey guys, I am using this code to convert the data into string CryptBinaryToString((BYTE *)pBufPtr,len,1,msg2,&lul_buflen); The msg2 looks like "KBeMW9yLvHwMow5dKCXO3ddKLzIGQTmbtNVmmFF2FmT9/f39q6urq6urq6vu/u7+" and I am using the following functions to convert the string to long so that I can reduce the size to be displayed on the string char *p; long l = strtol(msg2, &p, 10); long temp1 = atoi(msg2); But both of them return 0 as the value. Please help. Thanks.

                  vg

                  1 Reply Last reply
                  0
                  • V vgandhi

                    Dear James, In the message above you said we can reduce the buffer by using the function ultoa. But my question is I have to first convert the buffer into ulong and then use the function ultoa. So how do I convert the buffer to ulong because the function ultoa takes in a ulong as its first parameter. So please let me know. Thanks.

                    vg

                    J Offline
                    J Offline
                    James R Twine
                    wrote on last edited by
                    #12

                    The way to do that is to pad the data buffer (if required) to a 4-byte boundary, and then take 4 byte "chunks" of them, cast the address of the chunk to an unsigned long* (or DWORD*), and then dereference the pointer to get a DWORD value to convert.    But since you are already converting the binary data to a string (as shown in a later example you posted), you will gain little if nothing by using my suggestion - your converting the binary data to a Base64 encoded string actually expands it (each three binary bytes becomes 4 ASCII characters using Base64 encoding).    You may reduce the raw binary data a bit, but the worst case you can get using my idea is 7 alphanumeric digits per 4 binary bytes, as opposed to hexidecimal's which is 8 alphanumeric per 4 bytes of raw data.    You can see an example of this here:

                    TCHAR	caBuffer\[ 32 + 1 \];
                    BYTE	btaBinary\[\] = { 0x00, 0x04, 0xE4, 0xF0 };
                    BYTE	btaBinary2\[\] = { 'A', 'b', '1', '2' };
                    BYTE	btaBinary3\[\] = { 0xFF, 0xFF, 0xFF, 0xFF };
                    
                    ::\_ultot( \*(DWORD\*)btaBinary, caBuffer, 36 );
                    ::\_ultot( \*(DWORD\*)btaBinary2, caBuffer, 36 );
                    ::\_ultot( \*(DWORD\*)btaBinary3, caBuffer, 36 );
                    

                    - And by reading the contents of caBuffer after each call to ::_ultot(...)    If you can somehow compress the data, or reduce the encryption such that the length of the cyphertext is closer to the length of the plaintext, that will also go a long way.    Peace!

                    -=- James
                    Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                    See DeleteFXPFiles

                    1 Reply Last reply
                    0
                    • J James R Twine

                      vgandhi wrote:

                      In this case I want to take the encrypted data pBufPtr and display it as a string to the user. So any suggestions on how I can convert the pBufPtr into a decimal number or string that can be displayed to the user for typing purposes. Also the decimal or string value obtained by converting the pBufPtr needs to be converted back into the encrypted data for the response side of the application

                      The simplest thing to do is to convert each byte of the encrypted data into two hexidecimal characters, so that a byte of the value of 'z' becomes the string of two characters: '7A', and you can extend this to each byte of encrypted data.  I.e 'ijk123' becomes '696A6B313233'.    If the encrypted data is of fixed length, for example, it will always be 10 characters of encrypted data, you can render it to hexidecimal and back again using functions like sprintf(...) and sscanf(...).

                      ::sprintf( "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                          btaEncData[ 0 ], btaEncData[ 1 ], btaEncData[ 2 ], btaEncData[ 3 ],
                          btaEncData[ 4 ], btaEncData[ 5 ], btaEncData[ 6 ],
                          btaEncData[ 7 ], btaEncData[ 8 ], btaEncData[ 9 ] );

                      // ....

                      ::sscanf( cpHexData, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                          &btaEncData[ 0 ], &btaEncData[ 1 ], &btaEncData[ 2 ], &btaEncData[ 3 ],
                          &btaEncData[ 4 ], &btaEncData[ 5 ], &btaEncData[ 6 ],
                          &btaEncData[ 7 ], &btaEncData[ 8 ], &btaEncData[ 9 ] );

                      That is a simple example, and not the most optimal, but it should give you the general idea.    You can reduce the amount of data the user has to type by converting the encrypted data buffer into blocks of 32-bit values (DWORDs) or 64-bit values (hypers), and using a function like ultoa(...) or _ui64toa(...) with a higher radix to reduce number of characters but use a wider range of them, and use the corresponding ato*(...) functions to convert them back.

                      ultoa( 0xFFFFFFFF, caBuffer, 10 ); // = "4294967295"
                      ultoa( 0xFFFFFFFF, caBuffer, 16 ); // = "ffffffff"
                      ultoa( 0xFFFFFFFF, caBuffer, 36 ); // = "1z141z3"

                      Peace!

                      -=- James
                      Please rate this message - let me know if I helpe

                      V Offline
                      V Offline
                      vgandhi
                      wrote on last edited by
                      #13

                      Dear James, In your above example how would I convert the encrypted data if it is not of fixed length using your solution above. Also once everything is converted to hexidecimal now how would I convert that back into the encrypted data again? Please let me know. Thanks.

                      vg

                      J 1 Reply Last reply
                      0
                      • V vgandhi

                        Dear James, In your above example how would I convert the encrypted data if it is not of fixed length using your solution above. Also once everything is converted to hexidecimal now how would I convert that back into the encrypted data again? Please let me know. Thanks.

                        vg

                        J Offline
                        J Offline
                        James R Twine
                        wrote on last edited by
                        #14

                        You would pad the data out to the DWORD boundary as required.  Using a function like ::strtoul(...) would convert the data back into DWORDs, from which you could get your original data from.    Peace!

                        -=- James
                        Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                        Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                        See DeleteFXPFiles

                        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