2012-09-16 4 views
0

현재 NX의 암호 스크램블 알고리즘을 QT-C++ 에서 C#으로 이식하려고합니다.CX에서 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# -Port에 실수가 있어야합니다. 특히 원래 코드의이 줄에 대해 잘 모르겠습니다. sRet.ref (i1) = validCharList [i];

ref (i1)이 무엇을하고 있는지 전혀 알지 못합니다. 누군가가 내 실수 : 사전에

감사를 발견하는 경우

좋을 것이다

답변

0

초기화하고 sRet, 작업 않습니다 scrambleString()의 상부 코드 부분과 마지막에 반환 retStr에 하부 작업을 sRet에서 전혀 사용하지 않습니다. 당신은 아마 retStr/sRet, 어쩌면 그냥 사용하는 하나의 문자열을 ... 결합한다

sRet.ref(i1) = validCharList[i] 

위의 코드는 단순한 문자 할당, POS i에서 문자열 validCharList에서 문자와 sRet에 POS i1에서 문자를 교체 .