Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am trying to implement Licencing in C#. I generated an encrypted licence file using following program:

C#
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;
        }
    }
}


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:
C#
val = sReader.ReadLine();


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.
Posted
Updated 1-Nov-11 4:06am
v2
Comments
Pandya Anil 1-Nov-11 10:12am    
what is the exception it throws?

1 solution

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

C#
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;
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900