如何编辑我的代码,以便解密时的异常可以结束

时间:2017-07-05 07:34:48

标签: c# encryption cryptography tripledes

例外情况:

  发生了

System.Security.Cryptography.CryptographicException'   mscorlib.dll其他详细信息::要解密的数据长度   无效。

public string Encrypt(string toEncrypt, bool useHashing)
    {
        toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

        System.Configuration.AppSettingsReader settingsReader = new System.Configuration.AppSettingsReader();
        // Get the key from config file

        //string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
        //string key = UTF8Encoding.UTF8.GetString(keyArray);
        ////System.Windows.Forms.MessageBox.Show(key);
        ////If hashing use get hashcode regards to your key

        //keyArray = UTF8Encoding.UTF8.GetBytes(key);

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        //set the secret key for the tripleDES algorithm
        try
        {
            tdes.Key = keyArray;
            //mode of operation. there are other 4 modes. We choose ECB(Electronic code Book)
            tdes.Mode = CipherMode.ECB;
            //padding mode(if any extra byte added)
            tdes.Padding = PaddingMode.PKCS7;
            iv = tdes.IV;
            ICryptoTransform cTransform = tdes.CreateEncryptor();
            //transform the specified region of bytes array to resultArray
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            //Release resources held by TripleDes Encryptor
            tdes.Clear();

            //Return the encrypted data into unreadable string format
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        catch (Exception  ae)
        {
            MessageBox.Show(ae.Message);
            return ae.Message;
        }


        }
public string Decrypt(string cipherString, bool useHashing)
    {
        //get the byte code of the string

        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(cipherString);

        System.Configuration.AppSettingsReader settingsReader = new System.Configuration.AppSettingsReader();
        //Get your key from config file to open the lock!
        //string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
        string key = UTF8Encoding.UTF8.GetString(keyArray);
        if (useHashing)
        {
            //if hashing was used get the hash code with regards to your key
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            //release any resource held by the MD5CryptoServiceProvider

            hashmd5.Clear();
        }
        else
        {
            //if hashing was not implemented get the byte code of the key
            keyArray = UTF8Encoding.UTF8.GetBytes(key);
        }

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        //set the secret key for the tripleDES algorithm
        tdes.Key = keyArray;
        tdes.IV=iv;
        //mode of operation. there are other 4 modes.
        //We choose ECB(Electronic code Book)

        tdes.Mode = CipherMode.ECB;
        //padding mode(if any extra byte added)
        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
        //Release resources held by TripleDes Encryptor

        //string s=resultArray.ToString("X2");

        tdes.Clear();
        //return the Clear decrypted TEXT
        return UTF8Encoding.UTF8.GetString(resultArray);
    }

1 个答案:

答案 0 :(得分:1)

您的Encrypt()方法返回转换为Base64字符串的加密字节。您的Decrypt()方法尝试直接解密Base64字符串:

byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(cipherString);

相反,您需要使用Base64转换将Base64字符串转换回字节:

byte[] toEncryptArray = Convert.FromBase64String(cipherString);