Escolar Documentos
Profissional Documentos
Cultura Documentos
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace ASPEncryptDecryptFile
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateUploadedFiles();
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Download")
{
string filePath = e.CommandArgument.ToString();
// key for decryption
byte[] Key = Encoding.UTF8.GetBytes("asdf!@#$1234ASDF");
{
// Decrypt & Download Here
Response.ContentType = "application/octet-stream";
//Response.AddHeader("Content-Disposition","attachment; filename=" +
Path.GetFileName(filePath) + Path.GetExtension(filePath));
Response.AddHeader("Content-Disposition", "attachment; filename=myfile" +
Path.GetExtension(filePath));
int data;
while ((data = cs.ReadByte()) != -1)
{
Response.OutputStream.WriteByte((byte)data);
Response.Flush();
}
cs.Close();
fs.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
cs.Close();
fs.Close();
}
}
}
PopulateUploadedFiles();
}
catch
{
Response.Write("Encryption Failed! Please try again.");
}
}
DataList1.DataSource = uploadedFiles;
DataList1.DataBind();
}
private string GetIconPath(string fileExtention)
{
string Iconpath = "/Images";
string ext = fileExtention.ToLower();
switch (ext)
{
case ".txt":
Iconpath += "/txt.png";
break;
case ".doc":
case ".docx":
Iconpath += "/word.png";
break;
case ".xls":
case ".xlsx":
Iconpath += "/xls.png";
break;
case ".pdf":
Iconpath += "/pdf.png";
break;
case ".rar":
Iconpath += "/rar.png";
break;
case ".zip":
case ".7z":
Iconpath += "/zip.png";
break;
default:
break;
}
return Iconpath;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ASPEncryptDecryptFile
{
public class UploadFile
{
public string FileName { get; set; }
public string FileExtention { get; set; }
public long Size { get; set; }
public string FilePath { get; set; }
public string ICon { get; set; }
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="AspEncrptDecrpt1.WebForm1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>File Upload with encryption and Download with decryption using ASP.NET C#. </h3>
<div>
<table>
<tr>
<td>Select File : </td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
<td>
<asp:Button ID="btnUpload" runat="server" Text="Upload & Encrypt"
OnClick="btnUpload_Click" /></td>
</tr>
</table>
<div>
<%-- Add Datalist for Show Uploaded Files --%>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4"
RepeatDirection="Horizontal" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<table>
<tr>
<td>
<img src='<%#Eval("ICon") %>' width="60px" />
</td>
</tr>
<tr>
<td>
<%#Eval("FileName") %>
</td>
</tr>
<tr>
<td>
<%#Eval("Size","{0} KB") %>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="Download" CommandArgument='<%#Eval("FilePath")
%>'>Download</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</div>
</div>
</form>
</body>
</html>
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void Auto()
{
int r;
cmd1 = new SqlCommand("Select max(Clientid) from ClientDetails", con);
dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
string d = dr1[0].ToString();
if (d == "")
{
txtUid.Text = "1001";
}
else
{
r = Convert.ToInt32(dr1[0].ToString());
r = r + 1;
txtUid.Text = r.ToString();
}
}
}
}
protected void txtEmail_TextChanged(object sender, EventArgs e)
{
File Upload :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Text;
using System.Security.Cryptography;
String connectionString =
ConfigurationManager.ConnectionStrings["SECURE"].ConnectionString;
con = new SqlConnection(connectionString);
Auto();
}
private void PopulateUploadedFiles()
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/UploadedFiles"));
List<UploadFile> uploadedFiles = new List<UploadFile>();
foreach (var file in di.GetFiles())
{
uploadedFiles.Add
(
new UploadFile
{
FileName = file.Name,
FileExtention = Path.GetExtension(file.Name),
FilePath = file.FullName,
Size = (file.Length / 1024), // For get size in KB
Icon = GetIconPath(Path.GetExtension(file.FullName)) // Need to Get Icon...
}
);
}
//DataList1.DataSource = uploadedFiles;
//DataList1.DataBind();
}
public void uploadsave()
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("Files/" + filename));
con.Open();
SqlCommand cmd = new SqlCommand("insert into
FileInfo(Userid,Fileid,FileName,FileDesc,FilePath,Approved)
values(@Userid,@Fileid,@Name,@FileDesc,@Path,@Approved)", con);
cmd.Parameters.AddWithValue("@userid", txtuserid.Text);
cmd.Parameters.AddWithValue("@Fileid",txtfileid.Text);
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@FileDesc", txtFileDescription.Text);
cmd.Parameters.AddWithValue("@Path", "Files/" + filename);
cmd.Parameters.AddWithValue("@Approved","False");
cmd.ExecuteNonQuery();
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script
language='javascript'>alert('Upload success')</script>");
con.Close();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
}
public void Auto()
{
con.Open();
int r;
cmd = new SqlCommand("Select max(Fileid) from FileInfo", con);
dr = cmd.ExecuteReader();
if (dr.Read())
{
string d = dr[0].ToString();
if (d == "")
{
txtfileid.Text = "01";
}
else
{
r = Convert.ToInt32(dr[0].ToString());
r = r + 1;
txtfileid.Text = r.ToString();
}
}
con.Close();
}
protected void btnUpload_Click(object sender, EventArgs e)
{
PopulateUploadedFiles();
}
catch
{
Response.Write("Encryption Failed! Please try again.");
}
uploadsave();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
if (!IsPostBack)
{
BindGridviewData();
}
}
private void BindGridviewData()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from FileInfo ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
protected void chk_Click(object sender, EventArgs e)
{
CheckBox chkbox = sender as CheckBox;
GridViewRow gvrow = chkbox.NamingContainer as GridViewRow;
if (chkbox.Checked == true)
{
}
protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chkStatus = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
con.Open();
com.ExecuteNonQuery();
con.Close();
BindGridviewData();
}
}
Renewal Account:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
txtUid.Text = "";
txtUserName.Text = "";
txtPassword.Text = "";
txtTo.Text = "";
txtSecurityAns.Text = "";
lblSecurityKey.Text = "";
}
protected void Button2_Click1(object sender, EventArgs e)
{
txtUid.Text = "";
txtUserName.Text = "";
txtPassword.Text = "";
txtTo.Text = "";
txtSecurityAns.Text = "";
lblSecurityKey.Text = "";
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Send File:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.Net.Mail;
txtuserid.Text = "";
txtReceivemail.Text = "";
txtFileDesc.Text = "";
txtReceivemail.Text = string.Empty;
cmd1 = new SqlCommand("Select * From UserDetails WHERE Userid = " +
ddlreceiver.SelectedItem.Text, con);
dr1 = cmd1.ExecuteReader();
if (dr1.HasRows)
{
while (dr1.Read())
{
txtReceivemail.Text =dr1.GetString(4);
}
}
dr1.Close();
}
}
Receive File:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Text;
using System.Security.Cryptography;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
btnDecrypt.Visible = true;
lblEncryptedText.Text = string.Empty;
cmd1 = new SqlCommand("Select * From msgdetails WHERE securekey
='"+txtkey.Text+"' and receiverid = " + txtuserid.Text, con);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
lblEncryptedText.Text = dr.GetString(5);
dr.Close();
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script
language='javascript'>alert(' Access Denined , Invaild Security Key')</script>");
}
}
protected void Decrypt(object sender, EventArgs e)
{
lblDecryptedText.Text = this.Decrypt(lblEncryptedText.Text.Trim());
}
}
}