界面显示:

加密:

解密:


代码实现:

public string EncryptString(string str)

        {

            #region 加密程序

            char[] Base64Code = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '=' };

            byte empty = (byte)0;

            System.Collections.ArrayList byteMessage = new System.Collections.ArrayList(System.Text.Encoding.Default.GetBytes(str));

            System.Text.StringBuilder outmessage;

            int messageLen = byteMessage.Count;

            int page = messageLen / 3;

            int use = 0;

            if ((use = messageLen % 3) > 0)

            {

                for (int i = 0; i < 3 - use; i++)

                    byteMessage.Add(empty);

                page++;

            }

            outmessage = new System.Text.StringBuilder(page * 4);

            for (int i = 0; i < page; i++)

            {

                byte[] instr = new byte[3];

                instr[0] = (byte)byteMessage[i * 3];

                instr[1] = (byte)byteMessage[i * 3 + 1];

                instr[2] = (byte)byteMessage[i * 3 + 2];

                int[] outstr = new int[4];

                outstr[0] = instr[0] >> 2;



                outstr[1] = ((instr[0] & 0x03) << 4) ^ (instr[1] >> 4);

                if (!instr[1].Equals(empty))

                    outstr[2] = ((instr[1] & 0x0f) << 2) ^ (instr[2] >> 6);

                else

                    outstr[2] = 64;

                if (!instr[2].Equals(empty))

                    outstr[3] = (instr[2] & 0x3f);

                else

                    outstr[3] = 64;

                outmessage.Append(Base64Code[outstr[0]]);

                outmessage.Append(Base64Code[outstr[1]]);

                outmessage.Append(Base64Code[outstr[2]]);

                outmessage.Append(Base64Code[outstr[3]]);

            }

            return outmessage.ToString();

            #endregion

        }

        private void btnEncrypt_Click(object sender, EventArgs e)

        {

            #region 加密按钮

            try

            {

                txtMiWen.Text = EncryptString(txtMingWen.Text);

            }

            catch

            {

                MessageBox.Show("明文(密文)有误!", System.Windows.Forms.Application.ProductName);

                txtMiWen.Text = "";

                return;

            }

            #endregion

        }

        public string DecryptString(string str)

        {

            #region 解密程序

            if ((str.Length % 4) != 0)

            {

                throw new ArgumentException("不是正确的BASE64编码,请检查。", "str");

            }

            if (!System.Text.RegularExpressions.Regex.IsMatch(str, "^[A-Z0-9/+=]*$", System.Text.RegularExpressions.RegexOptions.IgnoreCase))

            {

                throw new ArgumentException("包含不正确的BASE64编码,请检查。", "str");

            }

            string Base64Code = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=";

            int page = str.Length / 4;

            System.Collections.ArrayList outMessage = new System.Collections.ArrayList(page * 3);

            char[] message = str.ToCharArray();

            for (int i = 0; i < page; i++)

            {

                byte[] instr = new byte[4];

                instr[0] = (byte)Base64Code.IndexOf(message[i * 4]);

                instr[1] = (byte)Base64Code.IndexOf(message[i * 4 + 1]);

                instr[2] = (byte)Base64Code.IndexOf(message[i * 4 + 2]);

                instr[3] = (byte)Base64Code.IndexOf(message[i * 4 + 3]);

                byte[] outstr = new byte[3];

                outstr[0] = (byte)((instr[0] << 2) ^ ((instr[1] & 0x30) >> 4));

                if (instr[2] != 64)

                {

                    outstr[1] = (byte)((instr[1] << 4) ^ ((instr[2] & 0x3c) >> 2));

                }

                else

                {

                    outstr[2] = 0;

                }

                if (instr[3] != 64)

                {

                    outstr[2] = (byte)((instr[2] << 6) ^ instr[3]);

                }

                else

                {

                    outstr[2] = 0;

                }

                outMessage.Add(outstr[0]);

                if (outstr[1] != 0)

                    outMessage.Add(outstr[1]);

                if (outstr[2] != 0)

                    outMessage.Add(outstr[2]);

            }

            byte[] outbyte = (byte[])outMessage.ToArray(Type.GetType("System.Byte"));

            return System.Text.Encoding.Default.GetString(outbyte);

            #endregion

        }

        private void btnDecrypt_Click(object sender, EventArgs e)

        {

            #region 解密按钮

            try

            {

                txtMiWen.Text = DecryptString(txtMingWen.Text);

            }

            catch

            {

                MessageBox.Show("明文(密文)有误", System.Windows.Forms.Application.ProductName);

                txtMiWen.Text = "";

                return;

            }

            #endregion

        }

版权声明:本文为博主原创文章,未经博主允许不得转载。

winform 加密 解密 分类: WinForm 2014-05-16 15:05 400人阅读 评论(0) 收藏的更多相关文章

  1. PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏

    Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...

  2. Find The Multiple 分类: 搜索 POJ 2015-08-09 15:19 3人阅读 评论(0) 收藏

    Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21851 Accepted: 8984 Sp ...

  3. 周赛-DZY Loves Chessboard 分类: 比赛 搜索 2015-08-08 15:48 4人阅读 评论(0) 收藏

    DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Ultra-QuickSort 分类: POJ 排序 2015-08-03 15:39 2人阅读 评论(0) 收藏

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 48111   Accepted: 17549 ...

  5. Drainage Ditches 分类: POJ 图论 2015-07-29 15:01 7人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62016 Accepted: 23808 De ...

  6. max_flow(Dinic) 分类: ACM TYPE 2014-09-02 15:42 94人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> #include<queue> #in ...

  7. SQL 分组 加列 加自编号 自编号限定 分类: SQL Server 2014-11-25 15:41 283人阅读 评论(0) 收藏

    说明: (1)日期以年月形式显示:convert(varchar(7),字段名,120) , (2)加一列 (3)自编号: row_number() over(order by 字段名 desc) a ...

  8. SQL 按月统计(两种方式) 分类: SQL Server 2014-08-04 15:36 154人阅读 评论(0) 收藏

    (1)Convert 函数 select Convert ( VARCHAR(7),ComeDate,120) as Date ,Count(In_code) as 单数,Sum(SumTrueNum ...

  9. SQL 存储过程 分页 分类: SQL Server 2014-05-16 15:11 449人阅读 评论(0) 收藏

    set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- ============================================= -- Au ...

  10. Sort List 分类: leetcode 算法 2015-07-10 15:35 1人阅读 评论(0) 收藏

    对链表进行排序,要求时间复杂度为O(n log n) ,不使用额外的空间. 我一开始的想法是借助quicksort的思想,代码如下: # time O(nlog(n)) # Definition fo ...

随机推荐

  1. Codeforces 549C The Game Of Parity(博弈)

    The Game Of Parity Solution: 这个题只需要分类讨论就可以解决. 先分别统计奇数和偶数的个数. 然后判断谁走最后一步,如果走最后一步时候同时有偶数和奇数,那么走最后一步的赢. ...

  2. DisUnity——Unity3D反编译资源提取利刃

    1.资源 软件及项目源码地址:https://github.com/ata4/disunity/releases 2.使用方法: 将待反编译的文件放入文件夹中:如:E:\Demo\ 在disunity ...

  3. 异步调用backgroudworker

    先看一个小例子:C#客户端打开一个控件,控件中加载了好多数据大约要用5秒中,如果我们直接打开控件,那么这个控件就要5秒中才能弹出来,当然这个时候用户已经把他Kill了.这个时候我们就需要先给用户把控件 ...

  4. WPF实现导航的几种方式

    下面是展示的是几种导航方式: 我们来具体看下xaml文件 <Page x:Class="WPF实现Navigation.Page1" xmlns="http://s ...

  5. WPF学习之路初识

    WPF学习之路初识   WPF 介绍 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 Windows Presentation Found ...

  6. python第一次上机遇到的困难

      正确 10 58 27412 2-1019 长度转换程序(10分) 完善下面的程序,能够: (1) 将用户输入的公制长度单位(米.千米)转换成英制长度单位(英寸.英里): (2) 将用户输入的英制 ...

  7. ASP.NET MVC 学习

    项目结构

  8. 一个消除if语句的例子

    // 一个按钮点击事件,判断点击按钮是那一个显示出他的信息 - (IBAction)buttonPressed:(id)sender { if (sender == self.leftButton) ...

  9. [BZOJ 1014] [JSOI2008] 火星人prefix 【Splay + Hash】

    题目链接:BZOJ - 1014 题目分析 求两个串的 LCP ,一种常见的方法就是 二分+Hash,对于一个二分的长度 l,如果两个串的长度为 l 的前缀的Hash相等,就认为他们相等. 这里有修改 ...

  10. Jamie's Contact Groups

    poj2289:http://poj.org/problem?id=2289 题意:给定一个规模为n的名单,要将名单中的人归到m个组中,给出每个人可能的分组号,需要确定一种分配方案,是的最大规模的组最 ...