无法使用TripleDESCryptoServiceProvider解密plainText

时间:2015-10-02 12:22:53

标签: c# security encryption tripledes

我正在尝试使用TripleDESCryptoServiceProvider加密和解密文本。

我的要求是相同的明文,密文不应该相同,为此我每次都生成了不同的vector

这是加密和解密文本的代码。

public string EncryptString(string PlainText)
        {
            GenerateIV();
            GenerateKey();
            if (PlainText == null || PlainText.Length <= 0)
            {
                throw new ArgumentNullException("Invalid Plaintext.");
            }

            if (Key == null || Key.Length <= 0)
            {
                throw new ArgumentNullException("Key Can Not Be Null Or Empty.");
            }

            if (IV == null || IV.Length <= 0)
            {
                throw new ArgumentNullException("Vector Can Not Be Null Or Empty.");
            }

            byte[] encryptedText;

            using (TripleDESCryptoServiceProvider tdsObj = new TripleDESCryptoServiceProvider())
            {
                if (!isKeyStrengthChecked)
                {
                    bool isWeekKey = TripleDESCryptoServiceProvider.IsWeakKey(Key);
                    if (isWeekKey)
                    {
                        throw new Exception("Weak Key.");
                    }
                    else
                    {
                        isKeyStrengthChecked = true;
                    }
                }
                tdsObj.Key = Key;
                tdsObj.IV = IV;
                tdsObj.Mode = CipherMode.CBC;
                tdsObj.Padding = PaddingMode.PKCS7;
                ICryptoTransform encryptor = tdsObj.CreateEncryptor(tdsObj.Key, tdsObj.IV);
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter srEncrypt = new StreamWriter(csEncrypt))
                        {
                            srEncrypt.Write(PlainText);
                        }
                        encryptedText = msEncrypt.ToArray();
                    }
                }
            }
            return Convert.ToBase64String(encryptedText);
        }

        public string DecryptString(string cipherText)
        {
            GenerateIV();
            GenerateKey();
            if (cipherText == null || cipherText.Length <= 0)
            {
                throw new ArgumentNullException("Invalid CipherText.");
            }

            if (Key == null || Key.Length <= 0)
            {
                throw new ArgumentNullException("Key Can Not Be Null Or Empty.");
            }

            if (IV == null || IV.Length <= 0)
            {
                throw new ArgumentNullException("Vector Can Not Be Null Or Empty.");
            }

            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            string PlainText = null;
            using (TripleDESCryptoServiceProvider tdsDecrypt = new TripleDESCryptoServiceProvider())
            {
                tdsDecrypt.Key = Key;
                tdsDecrypt.IV = IV;
                tdsDecrypt.Mode = CipherMode.CBC;
                tdsDecrypt.Padding = PaddingMode.PKCS7;
                ICryptoTransform decrytor = tdsDecrypt.CreateDecryptor(Key, IV);
                using (MemoryStream msDecrypt = new MemoryStream(cipherBytes))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decrytor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            PlainText = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return PlainText;
        }

我的完整代码在这里 .Net Fiddle

但是当我解密Text时,我没有得到相同的纯文本。我做错了什么。

2 个答案:

答案 0 :(得分:2)

您使用解密通话

生成新的 IV
public string DecryptString(string cipherText)
    {
        GenerateIV();
        GenerateKey();
        if (cipherText == null || cipherText.Length <= 0)

您应该在加密和解密时使用相同的 IV 密钥

答案 1 :(得分:0)

试试这个

Dotnet Fiddle Demo

在EncryptString和DecryptString函数上注释// GenerateIV(); GenerateKey();

        string plainText1 = "Hello Amit";
        string plainText2 = "Hello Amit";
        Helper helper = new Helper();

        helper.GenerateIV();
        helper.GenerateKey();
        string encryptedData1 = helper.EncryptString(plainText1);
        string encryptedData2 = helper.EncryptString(plainText2);
        string getBackText = helper.DecryptString(encryptedData1);
        Console.WriteLine(getBackText);
        Console.ReadLine();