I am encrypting data using CryptProtectData function and I am getting encrypted data in LPBYTE format, I want to save that data into a file and then read back for decryption.
In order to write string in file, I used following one to convert LPBYTE data to CString:
CString strEncrUName = (wchar_t *)encryptedUN;
I even tried this one How to convert from BYTE array to CString in MFC? but still it's not working.
Character set used is unicode.
Thanks in advance
The encrypted data is a buffer of raw bytes, not characters. If you want to convert it to a string, you'll have to encode it somehow, such as by converting it to Hex chars.
eg. byte 0xd5 becomes 2 chars: "D5"
Looping through each byte and converting it to hex chars is an easy exercice left up to the reader.
Of course, you'll have to convert it back to binary after you read the file.
Are you sure you want to save it to a text file. Your other option is to save the binary encrypted data to a binary file: no need to convert to/from string.
If your pointer represents zero terminated string
LPBYTE pByte;
CString str(LPCSTR(pByte));
Related
I am trying to extract a (UTF-8) text file from a zip file:
TZipFile *zFile = new TZipFile;
zFile->Open(L"C:\\test.zip", zmRead);
TByteDynArray bda;
zFile->Read(L"test.txt", bda);
zFile->Close();
ShowMessage(WideStringOf(bda));
This doesn't work. I get a string, but with weird content.
If I use zFile->Extract() it works fine, but I don't want to use the disk (performance).
Is there a way to use the read function on a UTF-8 file?
The problem is not with TZipFile itself, the real problem is actually with WideStringOf() instead.
TZipFile::Read() returns the raw bytes of the specified archived file (decompressing if needed), so your bda variable is a UTF-8 encoded byte array. However, WideStringOf() expects a byte array that is encoded as UTF-16LE instead. That is why you are seeing incorrect results.
To decode the byte array as UTF-8, use this instead:
ShowMessage(TEncoding::UTF8->GetString(bda));
I saved all the contents from an exe file to char buffer.
When I tried:
string bufferStr=(string)buffer;
cout<<bufferStr.length();
I got that bufferStr is much smaller than buffer, so I thought since i was reading an exe file that somewhere in there I had read an escape character "\0" or something.
How can I use buffer to cout or even write to a file, without buffer escaping any characters?
Thanks
stirng constructor doesn't know anything about length of your data and assumes that it is 0-terminated string. You should use
string bufferStr=string(buffer, bufferSize);
cout<<bufferStr.length();
with such constructor string will save also \0 bytes.
I am working on a HTTP Client module which receives information from server in a character buffer and is UTF-8 encoded. I wanted to create a std::string object from this character buffer.
Can I create a string object directly by passing the character buffer like this ?
std::string receivedstring(receievedbuffer,bufferlength);
here receievedbuffer is char[] array which contains data received from TCP/IP connection and bufferlength contains the number of bytes received. I am really confused with the term UTF-8 , I understood that its a unicode encoding , do I need to take any steps before the conversion.
std::string receivedstring(receievedbuffer,bufferlength);
It does not do any conversion, it just copies from receievedbuffer to receivedstring.
If your receievedbuffer was UTF-8 encoded then the the exact same bytes will be stored into receivedstring.
std::string is just a storage format and does not reflect the encoding of the data stored in it.
This question already has answers here:
UTF8 to/from wide char conversion in STL
(8 answers)
Closed 9 years ago.
I need to convert unsigned hex values to corresponding unicode characters which should be written to file using c++
so far I have tried this
unsigned short array[2]={0x20ac,0x20ab};
this should be converted to corresponding character in a file using c++
It depends on what encoding you have choosen.
If you are using UTF-8 encoding, you need to first convert each Unicode character to corresponding UTF-8 bytes sequence and then write that byte sequence to the file.
Its pseudo code will be like
EncodeCharToUTF8(charin, charout, &numbytes); //EncodeCharToUTF8(short,char*, int*);
WriteToFile(charout, numchar);
If you are using UTF-16 encoding, you need to first write BOM at the beginning of the file and then encoding each character into UTF-16 byte sequence (byte order matters here whether it is little-endian or big-endian depending on your BOM).
WriteToFile("\xFF\xFE", 2); //Write BOM
EncodeCharToUTF16(charin, charout, &numbytes); //EncodeCharToUTF16(short,char*, int*);
//Write the character.
WriteToFile(charout, numchar);
UTF-32 is not recommended although, step is similar to UTF-16.
I think this should help you to start.
From your array, it seems that you are going to use UTF-16.
Write UTF-16 BOM 0xFFFE for little endian and 0xFEFF for big endian. After that write each character as per byte order of your machine.
I have given here pseudo code which you can white-boxed. Search more on encoding conversion.
Actually you are facing two problems:
1. How to convert buffer from UTF-8 encoding to UTF-16 encoding?
I suggest you use boost locale library ,
sample codes can be like this:
std::string ansi = "This is what we want to convert";
try
{
std::string utf8 = boost::locale::conv::to_utf<char>(ansi, "ISO-8859-1");
std::wstring utf16 = boost::locale::conv::to_utf<wchar_t>(ansi, "ISO-8859-1");
std::wstring utf16_2 = boost::locale::conv::utf_to_utf<wchar_t, char>(utf8);
}
catch (boost::locale::conv::conversion_error e)
{
std::cout << "Fail to convert to unicode!" << std::endl;
}
2. How to save buffer to a file as UTF-16 encoding?
This involves writting a BOM (ByteOrderMark) at the beginning of the file manually, you can find reference here
That means if you want to save a buffer encodes as UTF-8 to a UNICODE file, you should first write 3 bytes "EF BB BF" in the beginning of the output file."FE FF" for Big-Endian UTF-16, "FF FE" for Little-Endian UTF-16.
I you still don't understand how BOM works, just open a Notepad, and write some words, save it with different "Encoding" options, and then open the saved file with a hex editor, you can see the BOM.
Hope it helps you!
I did a little testing with Blowfish encoding and noticed something. The encoded string is not always as long as the source string. it is shorter sometimes.
If i want to decode a encoded string i need the length to decode in the openssl function:
BF_cfb64_encrypt(encoded_input,decoded_output,length_to_decode,&key,iv,&num,BF_DECRYPT);
The problem here is that i don't have the length_to_decode if i don't know the length of the source string. if i use the length of the decoded string as length_to_decode then this may be too short.
If i use a bigger length then the decoded string is not correct. So do i need to know the length to decode with blowfish encoding?
in all the example code on the internet encoding and decoding always takes place in one function and the decoding example use hard coded length for decoding. but in real life i don't know the length of the encoded string and what then?
here is an example:
source string: sdcfgssssssss
source length: 13
encryption key: s
encrypted output: :‹(
encrypted length: 4
I init my key like this:
BF_KEY key;
const char * keyStr = "s";
BF_set_key(&key, strlen(keyStr), (const unsigned char *)keyStr);
The length of the output is identical to the length of the input. Do note that the output data may contain NUL characters, so you should not use strlen on the data.