Pregunta

I have been trying to find a replacement string function that would allow me to use std::string like behaviour in C#. I just need it for some code I ported across from C++ to C# that had std::strings in them. I've read about converting the strings to byte array and then working it out from there although I am unable to do so. Any possible suggestion of doing this with an example code? Please note the below code was written in C++ using std::strings instead of C# Unicode string.

C++ Code

std::string DeMangleCode(const std::string& argMangledCode) const
{
   std::string unencrypted;
   for (uint32_t temp = 0; temp < argMangledCode.size(); temp++)
   {
      unencrypted += argMangledCode[temp] ^ (434 + temp) % 255;
   }
   return unencrypted;
}

Mangled Input: ‚‡…ƒ

Output: 1305

¿Fue útil?

Solución

The following code will return "1305" for input "‚‡…ƒ". The trick was to figure out which code page was used when the string was mangled. It was code page 1252.

static public string DeMangleCode(string argMangledCode)
{
    Encoding enc = Encoding.GetEncoding(1252);
    byte[] argMangledCodeBytes = enc.GetBytes(argMangledCode);
    List<byte> unencrypted = new List<byte>();
    for (int temp = 0; temp < argMangledCodeBytes.Length; temp++)
    {
        unencrypted.Add((byte)(argMangledCodeBytes[temp] ^ (434 + temp) % 255));
    }
    return enc.GetString(unencrypted.ToArray());
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top