protected void Submit(object sender, EventArgs e)
    {
        int docid = 0;

        try
        {
            docid = Convert.ToInt32(Request.QueryString["docid"]);
            string key = txtseckey.Text;

            DocKeysGenerator gen = new DocKeysGenerator();
            gen.GetDocUserKey(docid);
            string secretekey = gen.DocUserKey;

            if (key == secretekey)
            {
                value             = 1;
                Session["seckey"] = secretekey;
            }
            else
            {
                value = 0;
            }
        }
        catch (Exception ex) { value = 0; }

        if (value == 1)
        {
            Response.Redirect("Downloading.aspx?docid=" + docid);
        }
        else if (value == 0)
        {
            message = "Sorry.!! Secrete Key Authentication Failed.. Try again..";
            title   = "Failure Report";
        }

        string script = "window.onload = function(){ alert('";

        script += message;
        script += "')};";
        ClientScript.RegisterStartupScript(this.GetType(), title, script, true);
        ClearInputs(Page.Controls);
    }
    protected void Process(object source, DataListCommandEventArgs e)
    {
        int docid = 0;

        try
        {
            string commandname = e.CommandName.ToString();
            docid = Convert.ToInt32(Convert.ToString(e.CommandArgument));

            if (commandname == "download")
            {
                value = 2;
            }
            if (commandname == "askkey")
            {
                string           userid = Convert.ToString(Session["userid"]);
                GetClientDetails cdtl   = new GetClientDetails(userid);
                string           name   = cdtl.ClientName;
                string           email  = cdtl.ClientEmailId;

                DocKeysGenerator gen = new DocKeysGenerator();
                gen.GetDocUserKey(docid);
                string seckey = gen.DocUserKey;

                GetDocumentDetails dtl      = new GetDocumentDetails(docid);
                string             doctitle = dtl.DocTitle;
                string             docowner = dtl.DocOwner;
                string             size     = dtl.DocSize + " bytes";

                EmailService mail = new EmailService();
                mail.sendSecKey(email, seckey, name, doctitle, docowner, size);

                value = 1;

                RecordUsage rec = new RecordUsage();
                rec.record(userid, "askkey");
            }
        }
        catch (Exception ex)
        {
            value = 0;
        }

        if (value == 2)
        {
            Response.Redirect("SecKeyAuthentication.aspx?docid=" + docid);
        }

        else if (value == 1)
        {
            message = "Secrete key generated successfully. Key is sent on your registered emailid.";
            title   = "Success Report";
        }
        else if (value == 0)
        {
            message = "Sorry.!! Secrete key generation failed.. Try again..";
            title   = "Failure Report";
        }

        string script = "window.onload = function(){ alert('";

        script += message;
        script += "')};";
        ClientScript.RegisterStartupScript(this.GetType(), title, script, true);
    }
    protected Boolean FileToDownload(string remotepath, string docname, string filePath, string docowner)
    {
        Boolean flag = false;

        try
        {
            int    docid   = Convert.ToInt32(Request.QueryString["docid"]);
            string userkey = Convert.ToString(Session["seckey"]);

            DocKeysGenerator gen = new DocKeysGenerator();
            gen.GetDocSecreteKey(docid, userkey);
            string docseckey = gen.DocSecreteKey;

            string outfilepath = Server.MapPath("UserDocs/") + docowner + "/temp/";
            if (!System.IO.Directory.Exists(outfilepath))
            {
                System.IO.Directory.CreateDirectory(outfilepath);
            }

            WebClient oclient = new WebClient();
            oclient.DownloadFile(remotepath + "/" + docname, outfilepath + "/" + docname);

            string decfile = outfilepath + "dec_" + docname;

            Cryptography cobj = new Cryptography();
            cobj.DecryptAES(outfilepath + "/" + docname, decfile, docseckey);

            byte[] myfile = System.IO.File.ReadAllBytes(decfile);
            // reverse and store file bytes into another byte array
            int filelength = myfile.Length;

            int    index    = filelength - 1;
            byte[] rev_file = new byte[filelength];
            foreach (byte b in myfile)
            {
                rev_file[index] = b;
                index--;
            }

            // create a file from reversed bytes and store on local server
            string rev_file_path = filePath + "/" + docname;

            try
            {
                if (File.Exists(rev_file_path))
                {
                    FileStream fs = new FileStream(rev_file_path, FileMode.Open, FileAccess.Read, FileShare.None);
                    fs.Close();
                    File.Delete(rev_file_path);
                }
            }
            catch (Exception ex) { }

            File.WriteAllBytes(rev_file_path, rev_file);
            // file is saved on server

            // check its hash value
            SHATracker track    = new SHATracker();
            string     shavalue = track.GetSHA1Hash(rev_file_path);

            // compare with hash value from xml file
            string  metaxml = filePath + "/" + docid + "_xml.xml";
            DataSet dsxml   = new DataSet();
            dsxml.ReadXml(metaxml);

            string original_hash = Convert.ToString(dsxml.Tables[0].Rows[0]["dochash"]);

            // if matches let it get downloaded

            if (shavalue == original_hash)
            {
                flag = true;
                //  Global.DocDownloadPath = rev_file_path;
            }
        }
        catch (Exception ex) { }
        return(flag);
    }