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

后端 未结 8 1357
时光说笑
时光说笑 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:07

    Instead of using MACTripleDES with the DES key repeated to fake a single DES CBC-MAC, you could just implement CBC-MAC yourself on top of DESCryptoServiceProvider.

    <1111111111111111> is not a weak DES key.

    This will calculate a DES CBC-MAC:

    public static byte[] CalcDesMac(byte[] key, byte[] data){
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Key = key;
            des.IV = new byte[8];
            des.Padding = PaddingMode.Zeros;
            MemoryStream ms = new MemoryStream();
            using(CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)){
              cs.Write(data, 0, data.Length);
            }
            byte[] encryption = ms.ToArray();
            byte[] mac = new byte[8];
            Array.Copy(encryption, encryption.Length-8, mac, 0, 8);
            PrintByteArray(encryption);
            return mac;
        }
    

提交回复
热议问题