2012-09-16 37 views
0

我正在嘗試將QT-C++ 中的密碼加密算法移植到C#中。c#中NX客戶端中的密碼加擾算法

來源: http://www.nomachine.com/ar/view.php?ar_id=AR01C00125

我當前的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace mynamespace 
{ 
class NxScramble 
{ 
    string ToScramble = ""; 
    int numValidCharList = 85; 
    String dummyString = "{{{{"; 

    char[] validCharList = new char[] 
    { 
     '!', '#', '$', '%', '&', '(', ')', '*', '+', '-', 
     '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', 
     '9', ':', ';', '<', '>', '?', '@', 'A', 'B', 'C', 
     'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
     'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 
     'X', 'Y', 'Z', '[', ']', '_', 'a', 'b', 'c', 'd', 
     'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
     'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
     'y', 'z', '{', '|', '}' 
    }; 

    public NxScramble(string s) 
    { 
     ToScramble = s; 
    } 

    public string scrambleString() 
    { 
     string sRet = ""; 

     if (ToScramble == null || ToScramble.Equals("")) 
     { 
      return ToScramble; 
     } 

     string str = encodePassword(ToScramble); 

     if (str.Length < 32) 
     { 
      str += dummyString; 
     } 

     for (int iR = (str.Length - 1); iR >= 0; iR--) 
     { 
      // 
      // Reverse string 
      // 
      sRet += str.ElementAt(iR); 
     } 

     if (sRet.Length < 32) 
     { 
      sRet += dummyString; 
     } 

     int k = getRandomValidCharFromList(); 
     int l = k + sRet.Length - 2; 

     sRet.Insert(0, k.ToString()); 

     string retStr = ""; 

     for (int i1 = 1; i1 < sRet.Length; i1++) 
     { 
      int j = findCharInList(sRet.ElementAt(i1)); 

      if (j == -1) 
      { 
       return ToScramble; 
      } 
      int i = (j + l * (i1 + 1)) % validCharList.Length; 
      /* 
      * sRet.ref(i1) = validCharList[i]; 
      */ 
      retStr += validCharList[i]; 
     } 

     char c = (char)(getRandomValidCharFromList() + 2); 
     sRet += c; 

     retStr = retStr.Replace("&", @"&amp;"); 
     retStr = retStr.Replace("\"", @"&quot;"); 
     retStr = retStr.Replace("'", @"&apos;"); 
     retStr = retStr.Replace("<", @"&lt;"); 
     retStr = retStr.Replace(">", @"&gt;"); 

     return retStr; 
    } 

    private string encodePassword(string p) 
    { 
     string sPass = ":"; 
     string sTmp = ""; 

     if (p.Equals("")) 
     { 
      return ""; 
     } 

     for (int i = 0; i < p.Length; i++) 
     { 
      char c = (char)p.ElementAt(i); 
      sTmp = String.Format("{0:d}:", (c + i + 1)); 
      sPass += sTmp; 
      sTmp = ""; 
     } 

     return sPass; 
    } 

    private int findCharInList(char c) 
    { 
     int i = -1; 

     for (int j = 0; j < numValidCharList; j++) 
     { 
      if (validCharList[j] == c) 
      { 
       i = j; 
       return i; 
      } 
     } 
     return i; 
    } 

    private char getRandomValidCharFromList() 
    { 
     int k = DateTime.Now.Second; 
     return validCharList[k]; 
    } 

} 
} 

它的產生從給定的密碼的字符串,我把它添加到nxclient的 XML-config文件:

 NxScramble nxs = new NxScramble(passPhrase); 
     string ScambledPass = nxs.scrambleString(); 

     string nxconfig = @" 
     .... 
     .... 
     <group name='Login' > 
     <option key='Auth' value='"+ ScambledPass + @"' /> 
     <option key='Guest Mode' value='false' /> 
     <option key='Guest password' value='' /> 
     <option key='Guest username' value='' /> 
     <option key='Login Method' value='nx' /> 
     <option key='Public Key' value=' 
     ......... 
     ......... 
     ' /> 
     <option key='User' value='" + username + @"' /> 
     </group> 
     "; 

但NXClient一直說「驗證失敗」。所以我很確定 在C++代碼的C#端口中一定有錯誤。 特別是我不確定原始代碼的這一行: sRet.ref(i1)= validCharList [i];

我不知道ref(i1)在做什麼。如果有人發現我的錯誤:)提前

感謝

將是很好

回答

0

scrambleString()上的代碼部分不初始化和sRet, 工作和retStr其下部最後返回使得根本不使用 sRet的工作。你或許應該結合retStr/sRet,也許只是 使用一個字符串...

sRet.ref(i1) = validCharList[i] 

上面的代碼只是一個簡單的字符分配,在pos i1sRet與焦炭從字符串validCharList在pos i替換字符。