2011-12-15 112 views
2

由於字符串,我遇到了最後一行代碼的問題。即使我是 使用Microsoft.VisualBasic命名空間它給我錯誤​​。system.linq.strings由於其保護級別而無法訪問

private byte[] CreateKey(string strPassword) 
{ 
    //Convert strPassword to an array and store in chrData. 
    char[] chrData = strPassword.ToCharArray(); 
    //Use intLength to get strPassword size. 
    int intLength = chrData.GetUpperBound(0); 
    //Declare bytDataToHash and make it the same size as chrData. 
    byte[] bytDataToHash = new byte[intLength + 1]; 

    //Use For Next to convert and store chrData into bytDataToHash. 
    for (int i = 0; i <= chrData.GetUpperBound(0); i++) { 
     bytDataToHash[i] = Convert.ToByte(Strings.Asc(chrData[i])); 
    } 
} 

回答

3

該行bytDataToHash[i] = Convert.ToByte(Strings.Asc(chrData[i]));可能不會做你想做的事。

你可能希望你的代碼做這樣的事情:

bytDataToHash = Encoding.Unicode.GetBytes(strPassword); 

,將讓你的密碼的字節。

但是您正在嘗試使用ASCII? (Asc呼叫提示)。如果你真的不想unicode的,你可以這樣做:

bytDataToHash = Encoding.ASCII.GetBytes(strPassword); 

但更好的翻譯壞行是:

Convert.ToByte(chrData[i]); // Do not use! Will cause some data loss!!! 

我不知道爲什麼你會想中間人物的ascii值。

相關問題