Home > Net >  How do you convert a operation and byte array to a hexadecimal string?
How do you convert a operation and byte array to a hexadecimal string?

Time:01-18

I try to reverse that code.

var val = FormatFingerOp(BitConverter.ToString(Convert.FromBase64String("EcE4Zodu3wRTBXntCdUvifj /GggA3DnG6zEKRZGpcY=")));

FormatFingerOp do that thing:

var fp = "";
string[] keys = val.Split('-');
for (int i = 1; i <= keys.Length; i  )
{
    fp  = keys[i - 1];
    if (i % 2 == 0)
        fp  = " ";
}

I try that but it doestn work. I try to reverse this steps like that:

string xsonuc = "";

for (int i = 1; i <= fp.Length; i  )
{
    xsonuc  = fp[i - 1];
    if (i % 2 == 0)
        xsonuc  = "-";
}


int NumberChars = val.Length;
byte[] ysonuc = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i  = 2)
    ysonuc[i / 2] = Convert.ToByte(val.Substring(i, 2), 16);

CodePudding user response:

I would argue that in general case this is not reversible, FormatFingerOp will concatenate pairs of keys into one entry, i.e. "one-three" will become "onethree" so if you don't have any prior knowledge of key structure (like fixed length or something) - you can't reverse it.

As for your code - note that FormatFingerOp iterates array of strings, while it seems that your are trying to iterate the string itself (for starters you need to "reverse" new separator " "- so fp.Split(" ")).

UPD

Try the following:

var original = "EcE4Zodu3wRTBXntCdUvifj /GggA3DnG6zEKRZGpcY=";
var val = BitConverter.ToString(Convert.FromBase64String(original));
var fp = "";
string[] keys = val.Split('-');
for (int i = 1; i <= keys.Length; i  )
{
    fp  = keys[i - 1];
    if (i % 2 == 0)
        fp  = " ";
}

// Reverse
var sb = new StringBuilder();
foreach(var pair in fp.Split(" ", StringSplitOptions.RemoveEmptyEntries))
{
    sb.Append(new []{pair[0], pair[1]});
    sb.Append(new []{pair[2], pair[3]});
}

var toEncode = sb.ToString();
var base64String = Convert.ToBase64String(Convert.FromHexString(toEncode));

Console.WriteLine(base64String == original);

UPD2

Implementation which should be compatible with older .NET verions:

// Reverse
var split = fp.Split(new []{' '},  StringSplitOptions.RemoveEmptyEntries);
var bytes = new byte[split.Length * 2];
for (var index = 0; index < split.Length; index  )
{
    var pair = split[index];
    bytes[index * 2] = Convert.ToByte(pair.Substring(0, 2), 16);
    bytes[index * 2   1] = Convert.ToByte(pair.Substring(2, 2), 16);
}

var base64String = Convert.ToBase64String(bytes);
Console.WriteLine(base64String == original);
  •  Tags:  
  • c#
  • Related