Converting encrypted data into string/decimal and vice versa
-
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
If your app will be run on XP+, you can use
CryptBinaryToString()
andCryptStringToBinary()
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ");
-
Are you asking for a encryption/decription routine, or how to convert a byte pointer to something displayable ?
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
-
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 likesprintf(...)
andsscanf(...)
.::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 (
DWORD
s) or 64-bit values (hyper
s), and using a function likeultoa(...)
or_ui64toa(...)
with a higher radix to reduce number of characters but use a wider range of them, and use the correspondingato*(...)
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
-
If your app will be run on XP+, you can use
CryptBinaryToString()
andCryptStringToBinary()
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ");
-
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
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 useultoa(...)
to convert to string (ltoa == long to ascii/ANSI), you can use theatol(...)
to get it back to a number (atol == ASCII/ANSI to long). If you look up those functions, you will see that there is noato**u**l(...)
function - but you will find thestrto*(...)
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 -
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
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
-
If your app will be run on XP+, you can use
CryptBinaryToString()
andCryptStringToBinary()
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ");
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
-
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 useultoa(...)
to convert to string (ltoa == long to ascii/ANSI), you can use theatol(...)
to get it back to a number (atol == ASCII/ANSI to long). If you look up those functions, you will see that there is noato**u**l(...)
function - but you will find thestrto*(...)
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 DeleteFXPFilesHey 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
-
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
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*
(orDWORD*
), 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 -
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 likesprintf(...)
andsscanf(...)
.::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 (
DWORD
s) or 64-bit values (hyper
s), and using a function likeultoa(...)
or_ui64toa(...)
with a higher radix to reduce number of characters but use a wider range of them, and use the correspondingato*(...)
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
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
-
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
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