2010-11-26 12 views
0

Je suis un peu déconcerté quand il s'agit de convertir ce javascript en C# ...Conversion de JavaScript en C#

Toute aide serait appréciée!

Voici le javascript:

function d(strInput) { 
    strInput = decoder(strInput); 
    var strOutput = ""; 
    var intOffset = (key + 112)/12; 
    for (i = 4; i < strInput.length; i++) { 
     thisCharCode = strInput.charCodeAt(i); 
     newCharCode = thisCharCode - intOffset; 
     strOutput += String.fromCharCode(newCharCode) 
    } 
    document.write(strOutput) 
} 

Et voici ma tentative de convertir en C#. Il travaille de temps en temps, mais la plupart du temps pour les nombres négatifs comme la clé ...

public string decode(int key, string data) 
{ 
    int i; 
    string strInput = base64Decode(data); 
    StringBuilder strOutput = new StringBuilder(""); 

    int intOffset = (key + 112)/12; 
    for (i = 4; i < strInput.Length; i++) 
    { 

     int thisCharCode = strInput[i]; 
     char newCharCode = (char)(thisCharCode - intOffset); 
     strOutput.Append(newCharCode); 
    } 
    return strOutput.ToString(); 
} 

Actuellement, il produit les éléments suivants:

(int key = 212, string data = "U0lra36DfImFkImOkImCW4OKj4h8hIdJfoqI") 
Output = {c¬a¬¬¬¬¬¬¬¬@¬¬¬¬a¬¬.c¬¬} 


(int key = -88, string data = "T1RXYmV0cHFkZ3R1MzQ1Ng==") 
Output = {crnobers1234} 
+0

Quelle sortie est correcte et laquelle ne l'est pas? Est-ce que celui avec la clé négative est correct? Les données d'entrée pour le premier exemple ne semblent pas correctes. Quelle était la chaîne originale non codée? –

+0

Ceci est une version javascript fonctionnant: http://bypass.rd.to/decoder.php – E3pO

+0

Que fait la fonction "décodeur" appelée dans la version Javascript? Je suppose qu'il est moyen de décoder une chaîne Base64 basée sur votre version C#? – Carson63000

Répondre

2

Ce code donne les mêmes résultats que votre exemple , pour les deux entrées d'échantillon:

public string decoder(string data) 
    { 
     string b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/="; 
     char o1, o2, o3; 
     int h1, h2, h3, h4, bits, i = 0; 
     string enc = ""; 
     do 
     { 
      h1 = b64.IndexOf(data.Substring(i++, 1)); 
      h2 = b64.IndexOf(data.Substring(i++, 1)); 
      h3 = b64.IndexOf(data.Substring(i++, 1)); 
      h4 = b64.IndexOf(data.Substring(i++, 1)); 
      bits = h1 << 18 | h2 << 12 | h3 << 6 | h4; 
      o1 = (char)(bits >> 16 & 0xff); 
      o2 = (char)(bits >> 8 & 0xff); 
      o3 = (char)(bits & 0xff); 
      if (h3 == 64) enc += new string(new char[] { o1 }); 
      else if (h4 == 64) enc += new string(new char[] { o1, o2 }); 
      else enc += new string(new char[] { o1, o2, o3 }); 
     } while (i < data.Length); 
     return enc; 
    } 

    public string d(int key, string data) 
    { 
     int i; 
     string strInput = decoder(data); 
     StringBuilder strOutput = new StringBuilder(""); 

     int intOffset = (key + 112)/12; 
     for (i = 4; i < strInput.Length; i++) 
     { 

      int thisCharCode = strInput[i]; 
      char newCharCode = (char)(thisCharCode - intOffset); 
      strOutput.Append(newCharCode); 
     } 
     return strOutput.ToString(); 
    } 

Je suis sûr que cela pourrait faire un peu de nettoyage! C'est juste une traduction verbatim de votre fonction Javascript decoder() qui l'a fait.

+0

Merci. L'avez-vous converti vous-même ou avez-vous utilisé un programme? – E3pO

+0

Convertis moi-même, la syntaxe est assez proche entre Javascript et C#. :-) Juste un peu de jouer avec les types. – Carson63000