【C#】简易编码与解码小程序

2016年第一次接触C#,闲来无事,趁着周末清闲做一个小程序打发打发时间。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;//调用IO,用以实现打开外部文件操作


namespace 文字加密解密
{
    public partial class FrmMain : Form
    {
        private FrmCover frmCover;
        public static string Source;
        public FrmMain()
        {
            frmCover = new FrmCover();
            frmCover.Show();
            InitializeComponent();         
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            Thread.Sleep(800);//延时450毫秒,需调用System.Threading
            frmCover.Close();
        }

        //****************************************************************************************************
        //名      称:FindText
        //功能描述:在InputBox中查找指定字符
        //输入参数:
        //输出参数:
        //返 回 值 :bool类型
        //****************************************************************************************************
        public bool FindText(string text)
        {
            bool returnValue = false;
            if (text.Length > 0)
            {
                int indexToText = InputBox.Find(text, RichTextBoxFinds.MatchCase);
                if (indexToText >= 0)
                {
                    returnValue = true;
                }
            }
            return returnValue;
        }

        //****************************************************************************************************
        //名      称:OpenFiles
        //功能描述:打开文件
        //输入参数:
        //输出参数:
        //返 回 值 :void类型
        //****************************************************************************************************
        private void OpenFiles()
        {
            OpenFileDialog file = new OpenFileDialog();//定义新的文件打开位置
            file.Filter = "文本文件|*.txt";//设置文件后缀的过滤
            if (file.ShowDialog() == DialogResult.OK)//如果有选择打开文件
            {               
                InputBox.LoadFile(@file.FileName, RichTextBoxStreamType.PlainText);//在RichTextBox控件中打开文件,显示文件中的字符串
            }
        }

        //****************************************************************************************************
        //名      称:SaveFiles
        //功能描述:保存文件
        //输入参数:
        //输出参数:
        //返 回 值 :void类型
        //****************************************************************************************************
        private void SaveFiles()
        {
            SaveFileDialog file = new SaveFileDialog();//定义新的文件保存位置控件
            file.Filter = "文本文件|*.txt";//设置文件后缀的过滤
            if (file.ShowDialog() == DialogResult.OK)//如果有文件保存路径
            {
                StreamWriter sw = File.AppendText(file.FileName);//在目标文件原有内容后面追加内容
                try
                {
                    sw.Write(this.ShowBox.Text.Replace("\n", System.Environment.NewLine));
                    //sw.Write(ShowBox.Text);//写入文本框中的内容
                    sw.Flush();//清除缓冲区
                    sw.Close();
                }
                catch (System.Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
        }

        //****************************************************************************************************
        //名      称:URL加密
        //功能描述:URL加密算法
        //输入参数:
        //输出参数:
        //返 回 值 :void类型
        //****************************************************************************************************
        private void URL_Coding(string Source)
        {
            Source = InputBox.Text;
            Source = System.Web.HttpUtility.UrlEncode(Source, System.Text.Encoding.UTF8);
            ShowBox.Text = Source;
            ShowBox.AppendText("\nURLCODING");      
        }

        //****************************************************************************************************
        //名      称:URL解码
        //功能描述:URL解码算法
        //输入参数:
        //输出参数:
        //返 回 值 :void类型
        //****************************************************************************************************
        private void URL_DeCocing(string Source)
        {
            Source = InputBox.Text;
            Source = System.Web.HttpUtility.UrlDecode(Source, System.Text.Encoding.UTF8);
            ShowBox.Text = Source;
            ShowBox.AppendText("\nURLDECODING");
        }

        //****************************************************************************************************
        //名      称:Base64加密
        //功能描述:Base64加密算法
        //输入参数:
        //输出参数:
        //返 回 值 :void类型
        //****************************************************************************************************
        private void Base64_Coding(string Source)
        {
            Source = InputBox.Text;
            Source = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Source)).Replace("+", "-").Replace("/", "_");
            ShowBox.Text = Source;
            ShowBox.AppendText("\nBASE64CODING");
        }

        //****************************************************************************************************
        //名      称:Base64解码
        //功能描述:Base64解码算法
        //输入参数:
        //输出参数:
        //返 回 值 :void类型
        //****************************************************************************************************
        private void Base64_DeCoding(string Source)
        {
            Source = InputBox.Text;
            Source = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(Source.Replace("-", "+").Replace("_", "/")));
            ShowBox.Text = Source;
            ShowBox.AppendText("\nBASE64DECODING");
        }

        //****************************************************************************************************
        //名      称:解码
        //功能描述:解码算法
        //输入参数:
        //输出参数:
        //返 回 值 :void类型
        //****************************************************************************************************
        private void DeCoding()
        {
            if (FindText("URLCODING"))
            {                
                URL_DeCocing(Source);
            }
            else if (FindText("BASE64CODING"))
            {               
                Base64_DeCoding(Source);
            }            
        }

        public void RespondEvent(int n)
        {
            Source = InputBox.Text; 
            switch (n)
            {
                case 0:
                    //打开文件
                    OpenFiles();
                    break;
                case 1:
                    //保存文件
                    SaveFiles();
                    break;
                case 2:
                    //URL加密
                    URL_Coding(Source);
                    break;
                case 3:
                    //Base64加密
                    Base64_Coding(Source);
                    break;
                case 4:
                    //解密
                    DeCoding();
                    break;             
                case 5:
                    //退出
                    this.Close();
                    break;
            }
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //打开文件
            RespondEvent(0);
        }

        private void 导出SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //保存文件
            RespondEvent(1);
        }

        private void MenuURLCoding_Click(object sender, EventArgs e)
        {
            //URL加密
            RespondEvent(2);
        }

        private void MenuBase64Coding_Click(object sender, EventArgs e)
        {
            //Base64加密
            RespondEvent(3);
        }

        private void MenuEnCoding_Click(object sender, EventArgs e)
        {
            //解密
            RespondEvent(4);
        }

        private void BtnQuit_Click(object sender, EventArgs e)
        {
            //退出
            RespondEvent(5);
        }
    }
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值