简体   繁体   English

LPBYTE 数据到 MFC 中的 CString

[英]LPBYTE data to CString in MFC

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.我正在使用 CryptProtectData function 加密数据,并且我正在获取 LPBYTE 格式的加密数据,我想将该数据保存到一个文件中,然后读回以进行解密。

In order to write string in file, I used following one to convert LPBYTE data to CString:为了在文件中写入字符串,我使用以下方法将 LPBYTE 数据转换为 CString:

CString strEncrUName = (wchar_t *)encryptedUN; CString strEncrUName = (wchar_t *)encryptedUN;

I even tried this one How to convert from BYTE array to CString in MFC?我什至试过这个How to convert from BYTE array to CString in MFC? but still it's not working.但它仍然无法正常工作。

Character set used is unicode.使用的字符集是 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"字节 0xd5 变为 2 个字符:“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; LPBYTE pByte;

CString str(LPCSTR(pByte)); CString str(LPCSTR(pByte));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM