2009-11-13 4 views
1

Je suis en train de convertir ce code de PHP en C#. Cela fait partie d'un portail captif. Quelqu'un pourrait-il expliquer ce qu'il fait?Convertir le code de cryptage PHP en C#

$hexchal = pack ("H32", $challenge); 
    if ($uamsecret) { 
    $newchal = pack ("H*", md5($hexchal . $uamsecret)); 
    } else { 
    $newchal = $hexchal; 
    } 
    $response = md5("\0" . $password . $newchal); 
    $newpwd = pack("a32", $password); 
    $pappassword = implode ("", unpack("H32", ($newpwd^$newchal))); 
+0

Je ne vois pas Crypter ion ici ... il n'y a aucun moyen de le décrypter, à cause de l'étape de hachage –

+2

il ne veut pas le décrypter, il suffit de porter le code à C# – RageZ

+2

Je sais, je soulignais juste une utilisation incorrecte du terme "cryptage "... –

Répondre

2

Eduardo,

si vous jetez un oeil au manuel pack, pack est utilisé pour convertir une chaîne en (hexadécimal, octal, binaire) à sa représentation numérique.

si

$hexcal = pack('H32', $challenge); 

pourrait convertir une chaîne comme 'cca86bc64ec5889345c4c3d8dfc7ade9' à la 0xcca réelle ... DE9

si uamsecret $ existent les mêmes choses avec le MD5 de hexchal concacteate avec le uamsecret. if ($ uamsecret) { $ newchal = pack ("H *", md5 ($ hexchal. $ Uamsecret)); } else { $ newchal = $ hexchal; }

$response = md5("\0" . $password . $newchal); 

MD% '\ 0' + mot de passe $ + newchal $

$newpwd = pack("a32", $password); 

pad password-32 octet

$pappassword = implode ("", unpack("H32", ($newpwd^$newchal))); 

faire un XOR newpwd et newchal et le convertir en une chaîne hexadécimale, je ne reçois pas le implode() peut-être c'est de convertir en chaîne à un tableau de caractères.

+0

Merci RageZ. Pourriez-vous poster un exemple d'entrée/sortie de ces résultats de pack? – Eduardo

1

J'ai également rencontré le besoin de fonctions pack-unpack de php en C# mais je n'ai pas eu de bonne ressource.

Alors j'ai pensé à le faire moi-même. J'ai vérifié l'entrée de la fonction avec les méthodes pack/unpack/md5 trouvées sur onlinephpfunctions.com. Depuis que j'ai fait le code seulement selon mes exigences. Cela peut être étendu pour d'autres formats

Paquet

private static string pack(string input) 
    { 
     //only for H32 & H* 
     return Encoding.Default.GetString(FromHex(input)); 
    } 
    public static byte[] FromHex(string hex) 
    { 
     hex = hex.Replace("-", ""); 
     byte[] raw = new byte[hex.Length/2]; 
     for (int i = 0; i < raw.Length; i++) 
     { 
      raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); 
     } 
     return raw; 
    } 

MD5

private static string md5(string input) 
    { 
     byte[] asciiBytes = Encoding.Default.GetBytes(input); 
     byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes); 
     string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); 
     return hashedString; 
    } 

Déballez

private static string unpack(string p1, string input) 
    { 
     StringBuilder output = new StringBuilder(); 

     for (int i = 0; i < input.Length; i++) 
     { 
      string a = Convert.ToInt32(input[i]).ToString("X"); 
      output.Append(a); 
     } 

     return output.ToString(); 
    }