TripleDES: Specified key is a known weak key for 'TripleDES' and cannot be used

后端 未结 8 1118
时光说笑
时光说笑 2020-12-29 10:45

I\'m using the .NET 3.0 class System.Security.Cryptography.MACTripleDES class to generate a MAC value. Unfortunately, I am working with a hardware device that

8条回答
  •  孤城傲影
    2020-12-29 11:06

    The reflection based solutions get you around the problem, but they are dirty and evil. Nobody has yet mentioned a very useful method: TripleDES.IsWeakKey

    I have had this problem and solved it with a very simple utility that I use immediately before I set the Key on my CryptoServiceProvider:

    private void MakeSecureKey(byte[] key)
    {
        while(TripleDES.IsWeakKey(key))
        {
            var sha = SHA256Managed.Create().ComputeHash(key);
            Array.Copy(sha,key,key.Length);
        }
    }
    

    If you call it anytime you make an encryptor or decryptor, it should prevent the crash and always give you a secure key.

提交回复
热议问题