前几天在进行资料整理时无意间找到了一个多年前用C#实现的上传记事本文件,并对文本内容进行批量加密的小功能,今天来和大家分享下
简介:C#上传记事本文件 在线实现对记事本中的全部手机号码(当然也可以是任意内容)进行SHA256加密运算,加密完成之后自动下载加密后文件到用户本机保存。(本案例使用SHA256加密文本内容,如有需要也可以换成任意加密方式,更多功能请自行扩展)
先说说这个SHA256加密小案例的由来吧:
大概是在2017年7月份,一个本站粉丝找到我,让我帮他写一个上传记事本文件并对记事本中的上万个手机号码用SHA256加密方式批量加密,
当时也是比较清闲,随手帮他写了这个小功能,并放在线上让他免费使用了不知多长时间。(自夸:一个勤奋并热心的站长)
对于当时他使用这个加密功能的目的暂不做讨论,下面只来看下具体实现的代码部分。
HTML页面部分代码(使用Bootstrap简单整理了一下页面):
<!DOCTYPE html>
<html>
<head runat="server">
<title>SHA256批量加密</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</head>
<body>
<form id="form1" runat="server" style="text-align: center">
<div class="alert alert-success" role="alert" style="text-align: center; padding-left: 45%;">
<asp:FileUpload ID="FileUpload1" runat="server" /></div>
<br />
<div class="alert alert-warning" role="alert">
提示:单个文件数据最多不能超过5万行。数据较大时处理需要耗费时长,请耐心等待</div>
<br />
<asp:Button ID="Button1" runat="server" Text="立即加密" OnClick="Button1_Click" class="btn btn-success" />
</form>
</body>
</html>
C#后台逻辑处理代码,使用到部分C#帮助类,若要了解请到文章底部下载DEMO源码附件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EncryHelper;
using System.Net;
using System.IO;
using System.Configuration;
namespace Encryption
{
/// <summary>
/// SH256在线加密案例演示
/// 作者:www.yunjson.com
/// </summary>
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string errStr = "";
string upPath = "/upload/"; //上传文件路径
int upLength = 80; //上传文件大小
string fileContentType = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName); //文件类型
if (fileContentType == ".txt")
{
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
FileInfo file = new FileInfo(name);
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") file.Extension; // 文件名称
string webFilePath = Server.MapPath(upPath) fileName; // 服务器端文件路径
string FilePath = upPath fileName; //页面中使用的路径
if (!File.Exists(webFilePath))
{
if ((FileUpload1.FileBytes.Length / (1024 * 1024)) > upLength)
{
ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('大小超出 " upLength " M的限制,请处理后再上传!');", true);
return;
}
try
{
FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
if (!Directory.Exists(webFilePath))
{
//加密处理
EncrySHA256(webFilePath);
}
else
{
errStr = "文件上传失败";
}
}
catch (Exception ex)
{
errStr = "文件加密上传异常";
}
}
else
{
errStr = "文件上传失败";
}
}
else
{
errStr = "只能上传txt格式文件";
}
if (!string.IsNullOrEmpty(errStr))
{
ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", string.Format("alert('提示:{0},请联系站长:www.yunjson.com');", errStr), true);
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:请先选择文件');", true);
}
}
/// <summary>
/// 加密处理
/// </summary>
/// <param name="path"></param>
public void EncrySHA256(string path)
{
int LogCount = int.Parse(ConfigurationManager.AppSettings["LogCount"].ToString());//此处为限制用户使用,此处意义请自行领会
FileHelper fileHelper = new FileHelper();
List<string> strlist = fileHelper.GetLineList(path);
if (strlist != null && strlist.Count > 0)
{
if (strlist.Count <= LogCount)
{
List<string> encyList = new List<string>();
foreach (var item in strlist)
{
if (!string.IsNullOrEmpty(item))
{
string oneEncyStr = new SHA256Helper().GetSHA256HashFromString(item);
encyList.Add(oneEncyStr);
}
}
CreateEncyHelper createhelper = new CreateEncyHelper();
createhelper.WriterToText(encyList);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:单个文件最多支持" LogCount "条手机号码,如需更多,请联系站长:www.yunjson.com');", true);
}
}
}
}
}
运行效果展示:
一个多年前的小DEMO拿来记录一下本站的发展历程。