3

I've got a PHP script which I have manually converted to C#. The output of the hexSalt and the hexFull variable do print the same but the ouput of variable data is different. I have discovered that the problem persists in converting the int variable to char. How can these be different?

I have tried in C# to convert them and to cast them in multiple ways like: Char.ConvertFromUtf32(int), Convert.ToChar(int), (char)int
but those are displaying the same incomplete C# output.

C#

string data = "";
string saltFull = "";
string hexFull = "";

char[] saltChars = salt.ToCharArray();
for (int i = 0; i < (salt.Length / 2); i++)
{
    string hexSalt = saltChars[i * 2].ToString() + saltChars[(i * 2) + 1].ToString();
    saltFull += hexSalt;

    int hex = int.Parse(hexSalt, NumberStyles.HexNumber);
    hexFull += hex.ToString();

    char chr = (char)hex;
    data += chr;
}

Response.Write(saltFull + "<br />"); // Same output
Response.Write(hexFull + "<br />"); // Same output
Response.Write(data); // Almost the same output...

// Output of variable data, the converted int to char.
// ¸:}P/C{_ëóCZÔfPüY£Y,Ö]·î~9ªuJb®é­I[p¢?0ZÖµz²ð4

PHP

$data = "";
$saltFull = "";
$hexFull = "";

for ($i = 0; $i < strlen($salt) / 2; $i++) {
    $hexSalt = $salt[$i * 2] . $salt[($i * 2) + 1];
    $saltFull .= $hexSalt;

    $hex = hexdec($hexSalt);
    $hexFull .= $hex;

    $chr = chr($hex);
    $data .= $chr;
}

echo $saltFull . "<br />"; // Same output
echo $hexFull . "<br />"; // Same output
echo $data; // Almost the same output...

// Output of variable data, the converted int to char.
// ¸ˆ:}Pž/CŽ”“{_ëóCZ‹ÔfP€üY£Y,Ö]·î~9ªuJb®é­I[p¢?0ZÖµz²ð…4

Additional information:

  • I am testing it in the same browser
  • I am running both codes on the same server.
  • I cannot edit the PHP code
  • I cannot change the salt
2
  • How do you compare the two salts? Commented Jan 14, 2014 at 10:06
  • I open the two pages in two tabs in the same browser, tested it with multiple browsers (IE, Chrome etc.). As you can see in the code I use echo and Response.Write(). Commented Jan 14, 2014 at 10:08

2 Answers 2

2

Problem solved!!

byte[] chr = BitConverter.GetBytes(hex);
data += Encoding.Default.GetString(chr);

I converted the int to a byte[] array and I used the GetString(byte[]) method to get the string.

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

Comments

0

Try to change data as a List of Character.

List<char> data = new List<char>();
string saltFull = "";
string hexFull = "";

char[] saltChars = salt.ToCharArray();
for (int i = 0; i < (salt.Length / 2); i++)
{
    string hexSalt = saltChars[i * 2].ToString() + saltChars[(i * 2) + 1].ToString();
    saltFull += hexSalt;

    int hex = int.Parse(hexSalt, NumberStyles.HexNumber);
    hexFull += hex.ToString();

    char chr = Char.ConvertFromUtf32(hex);
    data.Add(chr);
}

Response.Write(saltFull + "<br />"); // Same output
Response.Write(hexFull + "<br />"); // Same output

char[] dataArray = data.ToArray();
Response.Write(dataArray, 0, dataArray.Length);

Strings in C# are saved in UTF-16 encode and it could corrupt byte-strings. When you use binary strings I suggest you to use byte[] or char[] and not strings

2 Comments

Sorry, could you write an example salt?
The problem is solved, check my answer, I thank you for your effort :) !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.