2015-10-07 142 views
0

我正在加載器/客戶端上工作,我的論壇用戶將使用他們的myBB信息登錄到我的應用程序。我知道在應用程序中建立數據庫連接並不好。但是我也將他們的hwid存儲在數據庫中,所以我需要連接到它。myBB密碼c#客戶端

然而,它們存儲密碼是這樣的:

$hashedpsw = md5(md5($salt).md5($plainpassword)); 

而我試圖重現口令是這樣的:

string salt = "D4UFUd6U"; // get salt from db 
string password = "test!";// get password from user 
MD5 md5 = new MD5CryptoServiceProvider(); 

// Create md5 hash of salt 
byte[] saltBytes = Encoding.Default.GetBytes(salt); 
byte[] saltHashBytes = md5.ComputeHash(salt); 
string saltHash = System.BitConverter.ToString(saltHashBytes); 

// Create your md5(password + md5(salt)) hash 
byte[] passwordBytes = Encoding.Default.GetBytes(password + saltHash); 
byte[] passwordHashBytes = md5.ComputeHash(salt); 
string passwordHash = BitConverter.ToString(passwordHashBytes); 

,但我得到了以下錯誤:

cannot convert from 'string' to 'System.IO.Stream'

+0

您在哪一行發生錯誤? – Tobbe

+0

byte [] saltHashBytes = md5.ComputeHash(salt);''''byte [] passwordHashBytes = md5.ComputeHash(salt);' –

回答

2

ComputeHash想要一個IO.StreamByte[]作爲輸入,並且如錯誤指定的那樣,不能從您的字符串隱式轉換爲IO.Stream

以下是(從this answer被盜)的你怎麼能一個字符串轉換成流的示例:

public Stream GenerateStreamFromString(string s) 
{ 
    MemoryStream stream = new MemoryStream(); 
    StreamWriter writer = new StreamWriter(stream); 
    writer.Write(s); 
    writer.Flush(); 
    stream.Position = 0; 
    return stream; 
} 

這將改變你的代碼如下:

string salt = "D4UFUd6U"; // get salt from db 
string password = "test!";// get password from user 
MD5 md5 = new MD5CryptoServiceProvider(); 

// Create md5 hash of salt 
byte[] saltBytes = Encoding.Default.GetBytes(salt); 
byte[] saltHashBytes; 
using(Stream saltStream = GenerateStreamFromString(salt)) 
{ 
    salteHashBytes = md5.ComputeHash(saltStream); 
} 
string saltHash = System.BitConverter.ToString(saltHashBytes); 

// Create your md5(password + md5(salt)) hash 
byte[] passwordBytes = Encoding.Default.GetBytes(password + saltHash); 
byte[] passwordHashBytes; 
using(Stream saltStream = GenerateStreamFromString(salt)) 
{ 
    passwordHashBytes = md5.ComputeHash(saltStream); 
} 
string passwordHash = BitConverter.ToString(passwordHashBytes); 
+0

任何時候你使用流時,總是建議你用using語句包裝它,這樣即使拋出異常,它在完成時也會正確關閉。 byte [] saltHashBytes;使用(Stream saltStream = GenerateStreamFromString(salt)){saltHashBytes = md5.ComputeHash(saltStream); } –

+0

偉大的提示!隨意編輯我的答案:) – Tobbe

+0

已編輯。雖然我沒有在代碼中檢查* HashBytes數組變量在傳遞到BitConverter.ToString()方法之前是否爲空。 –

1

您使用使用md5散列算法對MD5CryptoServiceProvider類進行加密。首先添加以下命名空間:

using System.Text; 
using System.Security.Cryptography; 

其次,嘗試這樣的功能。

public static string Encrypt(string content) 
{ 
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 
    byte[] bytes = Encoding.ASCII.GetBytes(content); 
    bytes = md5.ComputeHash(data); 
    string result = Encoding.ASCII.GetString(bytes); 
    return result; 
}