解密时3DES C#错误

时间:2017-05-19 15:34:49

标签: c# cryptography 3des

我在这里使用了一个例子3DES。我稍微修改了一下,我无法理解错误。数据加密,但不想解密(数据来自文件)。我无法理解为什么DecryptTextFromFile()不想工作。事实上,通过这个例子,我没有改变任何东西

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Cryptography;


namespace _1._3_3DESC
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    // Create a new TripleDESCryptoServiceProvider object
                    // to generate a key and initialization vector (IV).
                    TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();


                    int n;
                    Console.WriteLine("1. Decipher file: ");
                    Console.WriteLine("2. Decode file: ");
                    Console.WriteLine("3  Выход: ");
                    n = int.Parse(Console.ReadLine());
                    switch (n)
                    {
                        case 1:
                            //Шифрование
                            EncryptTextToFile(tDESalg.Key, tDESalg.IV);
                            break;
                        case 2:
                            //Дешифрование
                            string Final = DecryptTextFromFile(tDESalg.Key, tDESalg.IV);
                            Console.WriteLine(Final);
                            break;
                        case 3:
                            Environment.Exit(0);
                            break;
                        default:
                            Console.WriteLine("Не правильный ввод");
                            break;
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

            }
        }


        public static void EncryptTextToFile(byte[] Key, byte[] IV)
        {
            try
            {

                Console.WriteLine("Введите имя файла");
                String FileName;
                String Data;

                FileName = Console.ReadLine();


                Console.WriteLine("Введите текст для шифрования ");
                Data = Console.ReadLine();


                // Create or open the specified file.
                FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);

                // Create a CryptoStream using the FileStream 
                // and the passed key and initialization vector (IV).
                CryptoStream cStream = new CryptoStream(fStream,
                    new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),
                    CryptoStreamMode.Write);

                // Create a StreamWriter using the CryptoStream.
                StreamWriter sWriter = new StreamWriter(cStream);

                // Write the data to the stream 
                // to encrypt it.
                sWriter.WriteLine(Data);

                // Close the streams and
                // close the file.
                sWriter.Close();
                cStream.Close();
                fStream.Close();
            }
            catch (CryptographicException e)
            {
                Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine("A file access error occurred: {0}", e.Message);
            }

        }

        public static string DecryptTextFromFile(byte[] Key, byte[] IV)
        {
            try
            {
                Console.WriteLine("Введите имя файла");
                String FileName;

                FileName = Console.ReadLine();

                // Create or open the specified file. 
                FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);

                // Create a CryptoStream using the FileStream 
                // and the passed key and initialization vector (IV).
                CryptoStream cStream = new CryptoStream(fStream,
                    new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV),
                    CryptoStreamMode.Read);

                // Create a StreamReader using the CryptoStream.
                StreamReader sReader = new StreamReader(cStream);

                // Read the data from the stream 
                // to decrypt it.
                string val = sReader.ReadLine();

                // Close the streams and
                // close the file.
                sReader.Close();
                cStream.Close();
                fStream.Close();

                // Return the string. 
                return val;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
                return null;
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine("A file access error occurred: {0}", e.Message);
                return null;
            }
        }

    }


}

导致错误

Вызвано исключение: "System.Security.Cryptography.CryptographicException" в mscorlib.dll

0 个答案:

没有答案