TripleDESCryptoService -“数据错误”

本文关键字:数据错误 错误 数据 TripleDESCryptoService | 更新日期: 2023-09-27 18:13:13

当我尝试解密我的加密密钥时,我收到一个错误,说它是错误的数据/不准确的数据。试着到处找了一会儿,没有任何结果。这是加密代码和解密代码。indata是用于加密的mac地址,用于解密的indata是从文本文件中读取的。

public string encryptMAC(string indata)
    {
        byte[] resultCrypt;
        UTF8Encoding utf8 = new UTF8Encoding();
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Mode = CipherMode.ECB;
        tdes.Padding = PaddingMode.PKCS7;
        byte[] encrypt = utf8.GetBytes(indata);
        try
        {
            ICryptoTransform encryptor = tdes.CreateEncryptor();
            resultCrypt = encryptor.TransformFinalBlock(encrypt, 0, encrypt.Length);
        }
        finally
        {
            tdes.Clear();
        }
        return Convert.ToBase64String(resultCrypt);
    }
        public string decryptMAC(string indata)
    {
        byte[] resultDecrypt;
        UTF8Encoding utf8 = new UTF8Encoding();
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Mode = CipherMode.ECB;
        tdes.Padding = PaddingMode.PKCS7;
        byte[] decrypt = Convert.FromBase64String(indata);
        try
        {
            ICryptoTransform decryptor = tdes.CreateDecryptor();
            resultDecrypt = decryptor.TransformFinalBlock(decrypt, 0, decrypt.Length);
        }
        catch (CryptographicException ex)
        {
            Console.WriteLine(ex);
        }
        finally
        {
            tdes.Clear();
        }
        return utf8.GetString(decrypt);
    }

TripleDESCryptoService -“数据错误”

TripleDESCryptoServiceProvider将为您生成一个密钥。如果您不自己设置它或在加密期间检索它,那么您将能够解密密文。

我建议您在您的方法中添加一个密钥,并使用相同的密钥进行加密和解密:

public string encryptMAC(string indata, byte[] key)
{
    byte[] resultCrypt;
    UTF8Encoding utf8 = new UTF8Encoding();
    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;
    tdes.Key = key;
    byte[] encrypt = utf8.GetBytes(indata);
    try
    {
        ICryptoTransform encryptor = tdes.CreateEncryptor();
        resultCrypt = encryptor.TransformFinalBlock(encrypt, 0, encrypt.Length);
    }
    finally
    {
        tdes.Clear();
    }
    return Convert.ToBase64String(resultCrypt);
}
public string decryptMAC(string indata, byte[] key)
{
    byte[] resultDecrypt = new byte[0];
    UTF8Encoding utf8 = new UTF8Encoding();
    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;
    tdes.Key = key;
    byte[] decrypt = Convert.FromBase64String(indata);
    try
    {
        ICryptoTransform decryptor = tdes.CreateDecryptor();
        resultDecrypt = decryptor.TransformFinalBlock(decrypt, 0, decrypt.Length);
    }
    catch (CryptographicException ex)
    {
        Console.WriteLine(ex);
    }
    finally
    {
        tdes.Clear();
    }
    return utf8.GetString(resultDecrypt);
}

请记住,DES有一小组弱密钥,因此您应该使用TripleDESCryptoServiceProvider为您生成一个密钥,该密钥将过滤掉这些弱密钥。

可以通过(new TripleDESCryptoServiceProvider()).Key生成一个有效的密钥。