2016-11-10 36 views

回答

5

在UWP,它是Windows.Security.Cryptography namespaceWindows.Security.Cryptography.Core namespace

CryptographicBuffer class有示例顯示如何使用此類。

這是我關於得到的MD5哈希演示:

private string strAlgNameUsed; 

public string GetMD5Hash(String strMsg) 
{ 
    string strAlgName = HashAlgorithmNames.Md5; 
    IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(strMsg, BinaryStringEncoding.Utf8); 

    HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(strAlgName); 
    strAlgNameUsed = objAlgProv.AlgorithmName; 

    IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg); 

    if (buffHash.Length != objAlgProv.HashLength) 
    { 
     throw new Exception("There was an error creating the hash"); 
    } 

    string hex = CryptographicBuffer.EncodeToHexString(buffHash); 

    return hex; 
} 
+0

不要使用ECB模式,它是不安全的,請參閱[ECB模式](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_ .28ECB.29),向下滾動到企鵝。 取而代之的是將CBC模式與隨機IV一起使用,只是將加密數據與IV一起用於解密。進一步只對密碼進行哈希處理是不夠的,也不安全,因此需要密碼派生函數,例如PBKDF2('Rfc2898DeriveBytes'),其迭代時間大約爲100ms。但是這個問題並沒有要求加密,只是散列。 – zaph

+0

@zaph,是的,雖然OP沒有發佈任何代碼,但我只能提供這裏的文檔,我認爲這不是一個好的答案,所以我提供了一個簡單的演示來提供幫助。如果是關於歐洲央行模式,你是對的,它是不安全的,我編輯我的答案刪除加密部分。 –

+0

其實它更多的是關於使用MD5進行密鑰派生。關鍵攻擊不是對實際密鑰的強力破解,而是密鑰的密鑰來源於使用經常使用的密碼列表。由於使用此密鑰派生方法必須消耗大量CPU時間。 – zaph