0

i need to decode a string in C#. Algorithm peformed in HEX values. So in C# i think i need to convert to byte array?am i right?. So I did a byte array from a string:

string Encoded = "ENCODEDSTRINGSOMETHING";
            byte[] ba = Encoding.Default.GetBytes (Encoded);

Now i need to modify each byte in byte array first starting from summing hex value (0x20) to first byte and for the each next byte in array i should substitute 0x01 hex from starting 0x20 hex value and sum it with following bytes in my ba array. Then i need to convert my byte array result to string again and print. In Python this is very easy:

def decode ():
    strEncoded = "ENCODEDSTRINGSOMETHING"
    strDecoded = ""
    counter = 0x20
    for ch in strEncoded:
        ch_mod = ord(ch) + counter
        counter -= 1
        strDecoded += chr(ch_mod)
    print ("%s" % strDecoded)

if __name__ == '__main__':
    decode()

How can i do it in C#? Thank you very much.

Falconsl
  • 3
  • 1

2 Answers2

1

Here's a rough outline of how to do what you are trying to do. Might need to change it a bit to fit your problem/solution.

public string Encode(string input, int initialOffset = 0x20)
{
     string result = "";
     foreach(var c in input)
     {
          result += (char)(c + (initialOffset --));
     }
     return result;
}
Ben Abraham
  • 482
  • 5
  • 10
0

Try this code:

string Encoded = "ENCODEDSTRINGSOMETHING";
byte[] ba = Encoding.Default.GetBytes(Encoded);

string strDecoded = "";
int counter = 0x20;
foreach (char c in Encoded)
{
    int ch_mod = (int)c+counter;
    counter -= 1;
    strDecoded += (char)ch_mod;
}
Pikoh
  • 7,582
  • 28
  • 53
  • @Falconsl if any of the answers helped you,you should mark any of them as accepted. That way any other looking for the same answer would know what to do. – Pikoh Dec 07 '16 at 15:04