前幾天在進行資料整理時無意間找到了一個多年前用C#實現(xiàn)的上傳記事本文件,并對文本內(nèi)容進行批量加密的小功能,今天來和大家分享下



簡介:C#上傳記事本文件 在線實現(xiàn)對記事本中的全部手機號碼(當(dāng)然也可以是任意內(nèi)容)進行SHA256加密運算,加密完成之后自動下載加密后文件到用戶本機保存。(本案例使用SHA256加密文本內(nèi)容,如有需要也可以換成任意加密方式,更多功能請自行擴展)



先說說這個SHA256加密小案例的由來吧:

大概是在2017年7月份,一個本站粉絲找到我,讓我?guī)退麑懸粋€上傳記事本文件并對記事本中的上萬個手機號碼用SHA256加密方式批量加密,

當(dāng)時也是比較清閑,隨手幫他寫了這個小功能,并放在線上讓他免費使用了不知多長時間。(自夸:一個勤奮并熱心的站長)


對于當(dāng)時他使用這個加密功能的目的暫不做討論,下面只來看下具體實現(xiàn)的代碼部分。


HTML頁面部分代碼(使用Bootstrap簡單整理了一下頁面):


<!DOCTYPE html>
<html>
<head runat="server">
    <title>SHA256批量加密</title>
    <link  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">
        提示:單個文件數(shù)據(jù)最多不能超過5萬行。數(shù)據(jù)較大時處理需要耗費時長,請耐心等待</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在線加密案例演示
    /// 作者:hnxxbl.cn
    /// </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;        // 服務(wù)器端文件路徑
                    string FilePath = upPath   fileName;   //頁面中使用的路徑
                    if (!File.Exists(webFilePath))
                    {
                        if ((FileUpload1.FileBytes.Length / (1024 * 1024)) > upLength)
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('大小超出 "   upLength   " M的限制,請?zhí)幚砗笤偕蟼鳎?);", 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},請聯(lián)系站長:hnxxbl.cn');", 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());//此處為限制用戶使用,此處意義請自行領(lǐng)會

            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   "條手機號碼,如需更多,請聯(lián)系站長:hnxxbl.cn');", true);
                }
            }
        }
    }
}


運行效果展示:





一個多年前的小DEMO拿來記錄一下本站的發(fā)展歷程。


點擊下載SHA256批量加密源碼DEMO


原文鏈接:點擊下載SHA256批量加密源碼DEMO