0

Hi All

I have decrypted the file using memorystream.. and store into a file
when i m executing pading error occur.. below i was sending my code
any body help me ...

private void DecryptFile(MemoryStream ms, string outputFile)
        {

            {
                string password = @"myKey123"; // Your Key Here

                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);

                //FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);


                RijndaelManaged RMCrypto = new RijndaelManaged();
                  
                MessageBox.Show("before seek"+ms.Length.ToString());  
                ms.Seek(0, SeekOrigin.Begin);
                //ms.Position = 0; 
                MessageBox.Show("after seek" + ms.Length.ToString());

                using (CryptoStream cs = new CryptoStream(ms,
                       RMCrypto.CreateDecryptor(key, key),
                       CryptoStreamMode.Read))
                {

                    FileStream fsOut = new FileStream(outputFile, FileMode.Create);
                    //CopyStream(cs, fsOut);  
                    int data;
                    while ((data = cs.ReadByte()) != -1)
                        fsOut.WriteByte((byte)data);
              
               fsOut.Close();
               // cs.Close();
                ms.Close();
            }
            }
        }

Edited by Nick Evan: Added code-tags

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by sknake
0

Please use code tags when posting code on daniweb:

Here is one way of going about it:

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

namespace daniweb
{
  public static class CryptoStuff
  {
    //Make these whatever you want
    private static readonly byte[] _k = { 0xA1, 0xF5, 0xA2, 0xBB, 0xA9, 0xBA, 0x37, 0x6F, 0x21, 0x2E, 0x17, 0xAC, 0x12, 0x8A, 0x03, 0x63 };
    private static readonly byte[] _iv = { 0x12, 0x22, 0x7A, 0xA3, 0xF3, 0xD3, 0x7B, 0x63, 0x51, 0x86, 0x7A, 0xD5, 0xB9, 0x4A, 0x1F, 0xAC };
    const string crypto_file_suffix = ".enc";
    const int buffer_size = 1024;

    public static void EncryptFile(string InFile)
    {
      EncryptFile(InFile, InFile + crypto_file_suffix);
    }
    public static void EncryptFile(string InFile, string OutFile)
    {
      SymmetricAlgorithm mCSP = new RijndaelManaged();
      mCSP.Key = _k;
      mCSP.IV = _iv;

      using (ICryptoTransform ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV))
      {
        using (FileStream fsOut = new FileStream(OutFile, FileMode.Create))
        {
          using (CryptoStream cs = new CryptoStream(fsOut, ct, CryptoStreamMode.Write))
          {
            using (FileStream fsIn = new FileStream(InFile, FileMode.Open))
            {
              int bytesRead;
              do
              {
                byte[] buffer = new byte[buffer_size];
                bytesRead = fsIn.Read(buffer, 0, buffer_size);
                cs.Write(buffer, 0, bytesRead);
              }
              while (bytesRead > 0);
            }
          }
        }
      }
    }

    public static void DecryptFile(string InFile)
    {
      DecryptFile(InFile, Path.ChangeExtension(InFile, null));
    }
    public static void DecryptFile(string InFile, string OutFile)
    {
      if (string.Compare(Path.GetExtension(InFile), crypto_file_suffix, true) != 0)
        throw new ArgumentException("InFile is not a crypto file", "InFile");

      SymmetricAlgorithm mCSP = new RijndaelManaged();
      mCSP.Key = _k;
      mCSP.IV = _iv;

      using (ICryptoTransform ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV))
      {
        using (FileStream fsOut = new FileStream(OutFile, FileMode.Create))
        {
          using (CryptoStream cs = new CryptoStream(fsOut, ct, CryptoStreamMode.Write))
          {
            using (FileStream fsIn = new FileStream(InFile, FileMode.Open))
            {
              int bytesRead;
              do
              {
                byte[] buffer = new byte[buffer_size];
                bytesRead = fsIn.Read(buffer, 0, buffer_size);
                cs.Write(buffer, 0, bytesRead);
              }
              while (bytesRead > 0);
            }
          }
        }
      }
    }

  }
}

Calling it:

private void button1_Click(object sender, EventArgs e)
    {
      CryptoStuff.EncryptFile(@"C:\data\test.txt");
    }

    private void button2_Click(object sender, EventArgs e)
    {
      CryptoStuff.DecryptFile(@"C:\data\test.txt.enc");
    }

Edited by happygeek: fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.