converting encrypted data into hexidecimal
Author Message
vish2007

 Posted: Visual C++ Language, converting encrypted data into hexidecimal Top
 How can I convert each byte of encrypted data into two hexidecimal characters, so that a byte of the value of `'z'` becomes the string of two characters: `'7A'`, My encrypted data is of variable length. Also once converted I need to conver the hexidecimal back to the encrypted data So any suggestions please. My encrypted data looks like the following " [U | ￡ ](%IY X/2 A9  f Qv dyyyy itit" andn is obtained by doing the following PBYTE pBufPtr if(!Encrypt(hKey, 0, TRUE, 0,(BYTE *)pBufPtr, &dwCount, dwBufferLen)) I need to convert it into hexidecimal and display it to user so the user can read it out and at the other end we'll take the hexidecimal and convert it back into the encrypted data. So need help with both ways. Thanks. Visual C++11

einaros

 Posted: Visual C++ Language, converting encrypted data into hexidecimal Top
 Here's one way to do it #include #include #include std::string toHex(const std::string& data){ std::ostringstream hexStream; hexStream << std::setw(2) << std::setfill('0') << std::hex; for(std::string::const_iterator i = data.begin(); i != data.end(); ++i) { hexStream << "0x" << static_cast(static_cast(*i)) << " "; } return hexStream.str();} string myHex = toHex("123abc&/(");

RyanH314

 Posted: Visual C++ Language, converting encrypted data into hexidecimal Top
 One way would be to represent the encrypted array per each 4-bits, then doing a table lookup depending on the value of that. Then it's up to you on how you want to sequence that data. I used something similar to encode email attachments into Base64. Using shift operators and bit masking you can accomplish the bit separation. btw, that looks like a memory leak to me: "dyyyy itit". Padd your buffers, never use them up entirely. //Main calling code here: BYTE MyString[] = "Hello World"; BYTE *MyHex = ToHex(MyString); MessageBox(NULL, (char *)MyHex, NULL, MB_OK); free(MyHex); unsigned int GetRightBits(int ByteVal) { unsigned int uiRightSide; unsigned int uiVal; uiVal = 1; for(int i=0;i<4;i++) { uiRightSide += ((ByteVal >> i) & 1) * (uiVal << i); } return uiRightSide; } unsigned int GetLeftBits(int ByteVal) { unsigned int uiLeftSide; unsigned int uiVal; uiVal = 1; for(int i=4;i<8;i++) { uiLeftSide += ((ByteVal >> i) & 1) * (uiVal << (i-4)); } return uiLeftSide; } BYTE *ToHex(BYTE *Bytes) { BYTE chHex[] = "0123456789ABCDEF"; int iLen = lstrlen((char *)Bytes); BYTE *rHexBytes = (BYTE *)calloc((iLen*2)+2, sizeof(BYTE)); for(int i=0;i<(iLen*2);(i+=2)) { rHexBytes[ i ] = chHex[GetLeftBits((int)Bytes[i/2])]; rHexBytes[i+1] = chHex[GetRightBits((int)Bytes[i/2])]; } return rHexBytes; }  If you are using unicode you may choose to modify these functions. However, the principles are the same for any data conversion: Octal, Hex, Base64, etc.

Simple Samples

 Posted: Visual C++ Language, converting encrypted data into hexidecimal Top
 Obviously there are many ways to do that. Note that there is nothing special about encrypted data; it is binary data and there are very likely previous answers with samples of converting to hexadecimal. The stringstream solution is easy and the other solution can be very efficient. Between the two is another possibility that uses _ultoa_s. You will need to add leading zeros when necessary but otherwise there is not much more you need to do.

Simple Samples

 Posted: Visual C++ Language, converting encrypted data into hexidecimal Top

 a table lookup depending on the value
Another conversion technique is to add (arithmeticly) corresponding character codes to convert each 4-bit value. For values 0-9, add the character '0'. For values 10-15, add the character 'a' or 'A', then subtract one or something like that. Also see You may receive an "error C2593: 'operator <<' is ambiguous" error message when you try to pass an __int64 variable to the ostream operator << for an interesting alternative.

Simple Samples

 Posted: Visual C++ Language, converting encrypted data into hexidecimal Top
 The following are from a program I am working on. They are not exactly what you need but I hope they at least show the idea.```string ToHex(const DWORD dw) { char Buffer[16]; string s("0x"); _ultoa(dw, Buffer, 16); s += Buffer; return s; } ``````char FromHexDigit(char c) { if (c <= 'f' && c >= 'a') return c - 'a' + 10; else return c - '0'; } char FromHex(string &s) { char ch, cl; ch = FromHexDigit(s[0]); cl = FromHexDigit(s[1]); ch = ch << 4; return ch + cl; }```