界面显示:

加密:

解密:


代码实现:

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. BitMap(比特位)

    所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省. 腾讯面试的时候,让写了一个BitMap ...

  2. U盘安装ubuntu时出现的gfxboot.c32:not a COM32R image问题

    方法特别简单:只需在提示后面输入   live  然后回车 就OK了

  3. C#程序中:如何向xml文件中写入数据和读取数据

    xml文件作为外部信息存储文件使用简单,方便,其结构和表格略有相似,下面简单的说一下xml文件内容的读取 …… using System.Xml;using System.IO;namespace W ...

  4. ecshop安装程序源码阅读-安装脚本(1)

    定义系统判断常量 引入安装初始化文件 设置时区 报告所有错误 定义站点根常量 定义php自身相对路径 引入系统,公共函数库 引入并初始化错误处理对象 引入并初始化模板引擎 引入安装相关业务 发送HTT ...

  5. python中的字典应用实例

    字典中的键使用时必须满足一下两个条件: 1.每个键只能对应一个项,也就是说,一键对应多个值时不允许的(列表.元组和其他字典的容器对象除外).当有键发生冲突时(即字典键重复赋值),取最后的赋值. > ...

  6. Meta 的两个 相关属性

    Meta标签中的apple-mobile-web-app-status-bar-style属性及含义: “apple-mobile-web-app-status-bar-style”作用是控制状态栏显 ...

  7. IbatisNet开发使用小结

    一.   介绍 平常做企业级应用,需求变化是经常的事,而很多基础代码重复也是很让人头疼的问题.所以很多人会使用一些ORM框架来增强项目的可维护性.可扩展性.IBatis.Net就是一个比较易用的ORM ...

  8. SQL2000和SQL2005和SQL2008同时安装问题

    原文:SQL2000和SQL2005和SQL2008同时安装问题 SQL2000和SQL2005和SQL2008同时安装问题 1,因为SQL2000安装过程中无法修改实例名称,故安装过程中必须先安装S ...

  9. 关于spring3中No Session found for current thread!and Transaction的配置和管理(转)

    今天我是特别的郁闷,本来项目做到一半,以前都好好的,结果下午就出现问题,苦逼的到现在才解决.它出现问题的时候都一声不坑, ,(天啦,现在才发现CSDN啥时候把QQ表情给整过来了)就在注册用户的时候,咦 ...

  10. 创办支持多种屏幕尺寸的Android应用

    创建支持多种屏幕尺寸的Android应用 Android涉及各种各样的支持不同屏幕尺寸和密度的设备.对于应用程序,Android系统通过设备和句柄提供了统一的开发环境,大部分工作是校正每一个应用程序的 ...