在C#中读取加密文件的问题

在C#中读取加密文件的问题

问题描述:

你好,
我正在尝试在C#中实现许可.我使用以下程序生成了加密的许可证文件:

Hello,
I am trying to implement Licencing in C#. I generated an encrypted licence file using following program:

using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

class RijndaelSample
{

    static void Main()
    {
        try
        {
            // Create a new Rijndael object to generate a key
            
            
             byte[] RijendaelKey = new byte[32];
             byte[] RijendaelVector = new byte[16];
             string key = "12345678912345678912345678912345";
             string IV= "1234567891234567";
           
            
            // Create a string to encrypt.
            string sData = "BFEBFBFF00020652#0008";
            string FileName = "system.dat";

            for (int l = 0; l < 32; l++)
            {
                RijendaelKey[l]=Convert.ToByte(key[l]);
            }

            for (int l = 0; l < 16; l++)
            {
                RijendaelVector[l] = Convert.ToByte(IV[l]);
            }

                // Encrypt text to a file using the file name, key, and IV.
                EncryptTextToFile(sData, FileName, RijendaelKey, RijendaelVector);

             Decrypt the text from a file using the file name, key, and IV.
            string Decrypted_text = DecryptTextFromFile(FileName, RijendaelKey, RijendaelVector);
                 
                // Display the decrypted string to the console.
           Console.WriteLine(Decrypted_text);

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

        Console.ReadLine();
    }

    public static void EncryptTextToFile(String Data, String FileName, byte[] Key, byte[] IV)
    {
        try
        {
            // Create or open the specified file.
            FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);

            // Create a new Rijndael object.
            Rijndael RijndaelAlg = Rijndael.Create();

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

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

            try
            {
                // Write the data to the stream 
                // to encrypt it.
                sWriter.WriteLine(Data);
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: {0}", e.Message);
            }
            finally
            {
                // Close the streams and
                // close the file.
                sWriter.Close();
                sWriter.Dispose();
                cStream.Close();
                cStream.Dispose();
                fStream.Close();
                fStream.Dispose();
            }
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
        }
        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine("A file error occurred: {0}", e.Message);
        }

    }


    public static string DecryptTextFromFile(String FileName, byte[] Key, byte[] IV)
    {
        try
        {
            // Create or open the specified file. 
            FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);

            // Create a new Rijndael object.
            Rijndael RijndaelAlg = Rijndael.Create();

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

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

            string val = null;

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


            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: {0}", e.Message);
            }
            finally
            {

                // Close the streams and
                // close the file.
                sReader.Close();
                sReader.Dispose();
                cStream.Close();
                cStream.Dispose();
                fStream.Close();
                fStream.Dispose();
            }
           
            // 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 error occurred: {0}", e.Message);
            return null;
        }
    }
}



使用此程序,我可以成功地可视化解密后的数据.

现在,我将加密的文件复制到另一个程序,并尝试使用相同的DecryptTextFromFile()函数读取该文件,但该文件到达以下行以从文件读取数据:



With this program I was able to successfully visualize my decrypted data.

Now I copied my encrypted file to another program and tried to read it with same DecryptTextFromFile() function but with it reaches the following line for reading data from file:

val = sReader.ReadLine();



程序引发异常,并且文件中没有红色数据.我还使用了与加密相同的Rijendael密钥和矢量进行解密.如果有人遇到此问题,请帮助我.

在此先感谢您.



Program throws an exception and no data is red from file. Also I used same Rijendael Key and vector for decryption as used for encryption. If anyone ever encountered this problem, kindly help me.

Thanks in advance.

我已使用您的代码创建了加密文件,并使用以下代码从另一个程序中解密了:

i''ve created encrypyted file with your code and decyrpted from another program with following code:

using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

class RijndaelSample
{

    static void Main()
    {
        try
        {
            // Create a new Rijndael object to generate a key
            
            
             byte[] RijendaelKey = new byte[32];
             byte[] RijendaelVector = new byte[16];
             string key = "12345678912345678912345678912345";
             string IV= "1234567891234567";
          
            string FileName = "system.dat";
 
            for (int l = 0; l < 32; l++)
            {
                RijendaelKey[l]=Convert.ToByte(key[l]);
            }
 
            for (int l = 0; l < 16; l++)
            {
                RijendaelVector[l] = Convert.ToByte(IV[l]);
            }
 

 
             //Decrypt the text from a file using the file name, key, and IV.
            string Decrypted_text = DecryptTextFromFile(FileName, RijendaelKey, RijendaelVector);
                 
                // Display the decrypted string to the console.
           Console.WriteLine(Decrypted_text);
 
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
 
        Console.ReadLine();
    }

   


    public static string DecryptTextFromFile(String FileName, byte[] Key, byte[] IV)
    {
        try
        {
            // Create or open the specified file. 
            FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);

            // Create a new Rijndael object.
            Rijndael RijndaelAlg = Rijndael.Create();

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

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

            string val = null;

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




            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: {0}", e.Message);
            }
            finally
            {

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

            // 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 error occurred: {0}", e.Message);
            return null;
        }
    }
}