0

To start off: I have an app that takes a byte array and loads assembly from it.

My idea, to prevent (easy)piracy, was to have an encrypted string on server, download it on client, decrypt it to get for example: std::string decrypted = "0x4D, 0x5A, 0x90, 0x0, 0x3, 0x0, 0x0, 0x0, 0x4";

Then to convert from string to binary(byte array) so it would be

uint8_t binary[] = { 0x4D, 0x5A, 0x90, 0x0, 0x3, 0x0, 0x0, 0x0, 0x4 }; 

And then continue as it was before, but after lots of googling I couldn't find much info on such direct conversion between regular string and byte array. Thank you for any help! -Sarah

2
  • Is the binary string turned into a byte array intended to be executed? Commented Aug 16, 2021 at 13:01
  • Sorry for late answer @Eljay, the binary is actually a DLL file for embedding mono! Commented Aug 16, 2021 at 15:16

3 Answers 3

2

You could use std::stoi in a loop.

It gives you the ending position of the number, which you can then use to check if the string is at its end, or if it's a comma. If it's a comma skip it. Then call std::stoi again using the position as the string to parse.

It's not the most effective, but should work fine.

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting idea. Although I assume I'd have to make it load assembly from a signed integer, instead of a byte array, as stoi appears to be a function for converting strings into signed integers? Unless you can somehow create a byte array out of a signed integer..
@xSarah Use a std::vector<std::byte> (or std::vector<uint8_t>) and append each number to it. Use the vector as your "byte array".
1

Use std::stoul to interpret a string as an unsigned integer. The unsigned integer can then be cast to a uint8_t type.

One method of parsing the entire string is by using a stringstream.

Code example:

#include <cstdint> #include <iostream> #include <sstream> #include <string> #include <vector> int main() { // Input string and output vector std::string const decrypted{"0x4D, 0x5A, 0x90, 0x0, 0x3, 0x0, 0x0, 0x0, 0x4"}; std::vector<std::uint8_t> bytes; // Parse the string and fill the output vector std::istringstream decryptedStringStream{decrypted}; std::string decryptedElement; while (getline(decryptedStringStream, decryptedElement, ',')) { auto const byte = static_cast<std::uint8_t>(std::stoul(decryptedElement, nullptr, 16)); bytes.push_back(byte); } // Print the results (in base 10) for (auto const &e : bytes) std::cout << static_cast<int>(e) << '\n'; } 

Comments

0

First of all, you should get rid of ", ". Then you can parse char by char, doing bitwise leftshift on every second char and saving as byte

char firstchar = HexCharToByte('5'); char secondchar = HexCharToByte('D'); char result = firstchar | (secondchar << 4); printf("%%hhu", result); //93 

Where HexCharToByte is (upper chars only):

char HexCharToByte(char ch) => ch > 57 ? (ch - 55) : (ch - 48); 

This is fast enough method of parsing hex chars.

2 Comments

I seem to be misunderstanding on how to define your provided function, I'm also not sure how fast it would be as it's a very big string I am dealing with.
@xSarah, it'll be faster than std::stoi or anything else, i suppose. I've suggested method of fast parsing of hex chars (uppercase only). Then you just should do some basic work like removing ' ', ',' and "0x" to get only needed characters..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.