Question

var graphids = from o in db.OfflineCarts
                               join i in db.Graphs on o.ItemId equals i.ItemUserID
                               select i;

            GridView gv = new GridView();
            gv.DataSource = graphids.ToList();
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=GraphTable.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
            Encryption();


    public void Encryption()
    {

        string password = @"myKey123"; // Your Key Here
        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        string cryptFile = "/Users/Neeraj/Downloads/UserFilesEncrypt.xls";
        FileStream fsCrypt = new FileStream(cryptFile, FileMode.CreateNew);

       RijndaelManaged RMCrypto = new RijndaelManaged();
       CryptoStream rs = new CryptoStream(fsCrypt,
          RMCrypto.CreateEncryptor(key, key),
          CryptoStreamMode.Read);

        CryptoStream cs = new CryptoStream(fsCrypt,
            RMCrypto.CreateEncryptor(key, key),
            CryptoStreamMode.Write);

        string inputFile = "/Users/Neeraj/Downloads/GraphTable.xls";
        FileStream fsIn = new FileStream(inputFile, FileMode.Open);

        int data;
        while ((data = fsIn.ReadByte()) != -1)
            cs.WriteByte((byte)data);

        fsIn.Close();
        cs.Close();
        fsCrypt.Close();

    }

I am encrypting the file just instantaneously after it is downloaded by grid view in mvc5 but i am getting 0 byte encrypted file and when i am re downloading the file than i am getting proper encrypted file Can Anybody tell me what is wrong with the code or i have to delay encryption function after download for sometime

Was it helpful?

Solution

use System.Security

FileStream fsInput = new FileStream(sInputFilename, 
                FileMode.Open, 
                FileAccess.Read);

FileStream fsEncrypted = new FileStream(sOutputFilename, 
                FileMode.Create, 
                FileAccess.Write);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top