+ Reply to Thread
Results 1 to 2 of 2

Thread: Encrypt/Decrypt files using RijndaelManaged

  1. #1

    Encrypt/Decrypt files using RijndaelManaged

    I have an assignment about encrypting and decrypting with RijndaelManaged and i don't know much about encrypting and security.
    I have read about RijndaelManaged , but i need help and thanks.
    Last edited by csharpmaster; 11-08-2012 at 10:53 PM.

  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    36
    RijndaelManaged is a two-way Encryption algorithm.
    Two-Way Encryption means that you can do both Encryption and Decryption unlike the one-way Encryption like [MD5]

    And it works by providing a password on Encryption and obviously the same password on Decryption

    So lets start on coding,

    Encryption :
    Code:
            private void EncryptAlgorithm(string inputFile, string outputFile)
            {
                try
                {
                    string password = @"encrypt9"; // Your Encryption Key Here
                    UnicodeEncoding UE = new UnicodeEncoding();
                    byte[] key = UE.GetBytes(password);
    
                    string cryptFile = outputFile;
                    FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
    
                    RijndaelManaged RMCrypto = new RijndaelManaged();
    
                    CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
    
                    FileStream fsIn = new FileStream(inputFile, FileMode.Open);
    
                    int data;
                    while ((data = fsIn.ReadByte()) != -1)
                    {
                        cs.WriteByte((byte)data);
                    }
    
                    fsIn.Close();
                    cs.Close();
                    fsCrypt.Close();
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                }
            }
    Decryption :
    Code:
            private void DecryptAlgorithm(string inputFile, string outputFile)
            {
                try
                {
                    string password = @"encrypt9"; // Your Decryption Key Here
    
                    UnicodeEncoding UE = new UnicodeEncoding();
                    byte[] key = UE.GetBytes(password);
    
                    FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
    
                    RijndaelManaged RMCrypto = new RijndaelManaged();
    
                    CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read);
    
                    FileStream fsOut = new FileStream(outputFile, FileMode.Create);
    
                    int data;
                    while ((data = cs.ReadByte()) != -1)
                    {
                        fsOut.WriteByte((byte)data);
                    }
    
                    fsOut.Close();
                    cs.Close();
                    fsCrypt.Close();
    
                }
                catch
                {
                    MessageBox.Show("Decryption failed!");
                }
            }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts