界面显示:

加密:

解密:


代码实现:

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. npm 好用工具 for 前端

    1. caniuse npm install -g caniuse-cmd

  2. 让IE8兼容问题,参考文档bootstrap

    问题:制作的页面基本没啥问题,只有IE8不好使 参考文档:bootstrap官网 方案一 方案二

  3. php时间选择器亲测可以自己修改

    由于前端的离职  造成我需要自己去做后台页面   所以有一个要填写生日的我就用了这个时间选择器 <?php /** * Created by PhpStorm. * User: Administ ...

  4. MySQL配置文件路径及‘The total number of locks exceeds the lock table size’问题

    在删除mysql中的数据时,遇到报错: ERROR 1206 (HY000): The total number of locks exceeds the lock table size 查了查,发现 ...

  5. iOS中默认样式修改-b

    项目中有大量的UITableView都需要显示sectionHeader.iOS中默认sessionHeader上的textLabel样式跟设计图不符. 按照我们之前的解决方案,是在每个UITable ...

  6. winfrom拷贝文件

    //File.Copy(@"C:\Users\Administrator\Pictures\bg.png", @"g:\images\bg.png", true ...

  7. HJA的异或值

    HJA的异或值 查看 提交 统计 提问 总时间限制:  20000ms 内存限制:  512000kB 描述 形态形成场(Morphogenetic Field)假说是Rupert Sheldrake ...

  8. Servlet处理Cookie

    1.CGI:进程,servlet:线程 2.HttpServletResponse下的方法就没有get开头的,(PrintWriter)getWriter在ServletResponse下. 3.st ...

  9. 减少 WAF 漏报的 8 种方法 !

    近十年来,WAF 已经逐渐发展成熟,被软件行业接受并成为无数企业保护应用的不二选择.很多大型企业甚至相继亲自设计或通过并购涉足其中,在这个硕大的市场里逐鹿竞争,同时也推动了应用层防火墙的技术演进. 与 ...

  10. iOS-NSString-Base64String-Base64原理

    之前看到好多人找Str2Base64Str,还有好多自己写了方法的,仔细研究了下base64的编码原理(这个我写在下面),发现官方的API已经可以完成这项功能,这里贴出来供大家参考. 一言不合就上代码 ...