基本信息
源码名称:简单的加密和解密
源码大小:0.51M
文件格式:.rar
开发语言:C#
更新时间:2016-06-22
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 1 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WFDecryptionAndEncryption
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            List<string> Encryptiontype = new List<string>();
            Encryptiontype.Add("加密");
            Encryptiontype.Add("解密");
            this.cmbEncryptiontype.DataSource = Encryptiontype;
            List<string> Encode = new List<string>();
            Encode.Add("UTF-8");
            this.cmbEncode.DataSource = Encode;
            List<string> Encryptionmode = new List<string>();
            Encryptionmode.Add("Base64");
            Encryptionmode.Add("AES");
            this.cmbEncryptionmode.DataSource = Encryptionmode;
            GetAllInitInfo(this.Controls[0]);
        }
        Encryptioninfo encryptioninfo = new Encryptioninfo();

        #region 控件缩放
        double formWidth;//窗体原始宽度
        double formHeight;//窗体原始高度
        double scaleX;//水平缩放比例
        double scaleY;//垂直缩放比例
        Dictionary<string, string> controlInfo = new Dictionary<string, string>();
        //控件中心Left,Top,控件Width,控件Height,控件字体Size

        /// <summary>
        /// 获取所有原始数据
        /// </summary>
        protected void GetAllInitInfo(Control CrlContainer)
        {
            if (CrlContainer.Parent == this)
            {
                formWidth = Convert.ToDouble(CrlContainer.Width);
                formHeight = Convert.ToDouble(CrlContainer.Height);
            }
            foreach (Control item in CrlContainer.Controls)
            {
                if (item.Name.Trim() != "")
                    controlInfo.Add(item.Name, (item.Left   item.Width / 2)   ","   (item.Top   item.Height / 2)   ","   item.Width   ","   item.Height   ","   item.Font.Size);
                if ((item as UserControl) == null && item.Controls.Count > 0)

                    GetAllInitInfo(item);
            }
        }
        private void ControlsChangeInit(Control CrlContainer)
        {
            scaleX = (Convert.ToDouble(CrlContainer.Width) / formWidth);
            scaleY = (Convert.ToDouble(CrlContainer.Height) / formHeight);
        }
        private void ControlsChange(Control CrlContainer)
        {
            double[] pos = new double[5];//pos数组保存当前控件中心Left,Top,控件Width,控件Height,控件字体Size
            foreach (Control item in CrlContainer.Controls)
            {
                if (item.Name.Trim() != "")
                {
                    if ((item as UserControl) == null && item.Controls.Count > 0)
                        ControlsChange(item);
                    string[] strs = controlInfo[item.Name].Split(',');
                    for (int j = 0; j < 5; j  )
                    {
                        pos[j] = Convert.ToDouble(strs[j]);
                    }
                    double itemWidth = pos[2] * scaleX;
                    double itemHeight = pos[3] * scaleY;
                    item.Left = Convert.ToInt32(pos[0] * scaleX - itemWidth / 2);
                    item.Top = Convert.ToInt32(pos[1] * scaleY - itemHeight / 2);
                    item.Width = Convert.ToInt32(itemWidth);
                    item.Height = Convert.ToInt32(itemHeight);
                    if (item.Top == 0)
                    {
                        return;
                    }
                    item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString()));
                }
            }
        }

        #endregion

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (controlInfo.Count > 0)
            {
                ControlsChangeInit(this.Controls[0]);
                ControlsChange(this.Controls[0]);
            }
        }
        
        private void butBegin_Click(object sender, EventArgs e)
        {
            LoadingEncryptioninfo();
            Begin();
        }

        /// <summary>
        /// 开始选择
        /// </summary>
        private void Begin()
        {
            Encryptionmode enmode = new Encryptionmode(encryptioninfo);
            Type type = typeof(Encryptionmode);
            MethodInfo mm = type.GetMethod(encryptioninfo.Encryptionmode);
            string result = mm.Invoke(enmode,null).ToString();
            if (encryptioninfo.Encryptiontype.Equals("加密"))
            {
                this.txtciphertext.Text = result;
                return;
            }
            this.txtEncryptionstr.Text = result;
        }

        /// <summary>
        /// 加载加密或解密信息
        /// </summary>
        private void LoadingEncryptioninfo()
        {
            encryptioninfo.Encryptiontype = this.cmbEncryptiontype.Text.Trim().ToString();
            encryptioninfo.Encryptionmode = this.cmbEncryptionmode.Text.Trim().ToString();
            encryptioninfo.Encryptionstr = this.txtEncryptionstr.Text.Trim().ToString();
            encryptioninfo.Ciphertext = this.txtciphertext.Text.Trim().ToString();
            encryptioninfo.Encode = this.cmbEncode.Text.Trim().ToString();
        }

        /// <summary>
        /// 格式化XML
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butcheck_Click(object sender, EventArgs e)
        {
            encryptioninfo.Encryptionstr = this.txtEncryptionstr.Text.Trim().ToString();
            XmlDocument xml = new XmlDocument();
            StringReader sr = new StringReader(encryptioninfo.Encryptionstr);
            try
            {
                xml.Load(sr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            XmlTextWriter xtw = null;
            try
            {
                xtw = new XmlTextWriter(sw);

                xtw.Formatting = Formatting.Indented;
                xtw.Indentation = 1;
                xtw.IndentChar = '\t';

                xml.WriteTo(xtw);
            }
            finally
            {
                if (xtw != null)
                    xtw.Close();
            }
            this.txtEncryptionstr.Text = sb.ToString();
            MessageBox.Show("加载xml成功.");
            MessageBox.Show(sb.ToString());
        }

        /// <summary>
        /// 窗体边框改变事假
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Resize(object sender, EventArgs e)
        {
        }
    }
}