c#文本加密程序代码示例

 更新时间:2013年11月29日 10:15:14   作者:  
这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码


控件滚动方法:

复制代码 代码如下:

//具体方法
//Movegroup(string u, Panel p1, Panel p2)
//Movegroup(方向<或>,被移走的控件,被移入的控件)
//注意还要添加两个timer :Return,Next , Interval = 10
        public void Movegroup(string u, Panel p1, Panel p2)
        {
            if (u == ">")     //这是向右,
            {
                up1 = p1;
                up2 = p2;
                p2.Visible = true;
                p1.Enabled = false;
                p2.Enabled = false;
                Next.Enabled = true;
            }
            if (u == "<")
            {
                up1 = p1;
                up2 = p2;
                p2.Visible = true;
                p1.Enabled = false;
                p2.Enabled = false;
                Return.Enabled = true;
            }
        }
        Panel up1, up2;
        int a = 0;
        int b = -580;
        int i = 0;
        int j = 580;
        private void Next_Tick(object sender, EventArgs e)
        {
            i -= 30;
            j -= 30;
            up1.Location = new Point(i, up1.Location.Y);
            up2.Location = new Point(j, up2.Location.Y);
            if (i <= -580 || j <= 0)
            {
                Next.Enabled = false;
                up2.Enabled = true;
                up1.Enabled = false;
                up1.Visible = false;
                i = 0;
                j = 580;
            }
        }
        private void Return_Tick(object sender, EventArgs e)
        {
            a += 30;
            b += 30;
            up1.Location = new Point(a, up1.Location.Y);
            up2.Location = new Point(b, up2.Location.Y);
            if (a >= 580 || b >= 0)
            {
                Return.Enabled = false;
                up2.Enabled = true;
                up1.Visible = false;
                up1.Enabled = false;
                a = 0;
                b = -580;
            }
        }

加密原理:

密码+问题+答案的md5 +“/”+加密后的串 组成一个文本文件,(二进制更好了)

解密原理:

先分离出文件里的 密码+问题+答案的md5在与用户输入的密码+问题+答案的md5对比

如果相符则 以此密码解密文件

如果不相符则 提示密码问题及答案错误

我使用DES加密,这是一个类

复制代码 代码如下:

namespace Pd_kernel
{
    public class Encrypt
    {
        /// <summary>
        /// 进行DES加密。
        /// </summary>
        /// <param name="pToEncrypt">要加密的字符串。</param>
        /// <param name="sKey">密钥,且必须为8位。</param>
        /// <returns>以Base64格式返回的加密字符串。</returns>
        public static string DESEncrypt(string pToEncrypt, string sKey)
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Convert.ToBase64String(ms.ToArray());
                ms.Close();
                return str;
            }
        }

        /// <summary>
        /// 进行DES解密。
        /// </summary>
        /// <param name="pToDecrypt">要解密的以Base64</param>
        /// <param name="sKey">密钥,且必须为8位。</param>
        /// <returns>已解密的字符串。</returns>
        public static string DESDecrypt(string pToDecrypt, string sKey)
        {
            byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }
        }

        public static string MD5encrypt(string text, string method)
        {
            string strMD5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(text, method);
            return strMD5;
        }
    }
}

相关文章

  • c#检测文本文件编码的方法

    c#检测文本文件编码的方法

    这篇文章主要介绍了c#检测文本文件编码的方法
    2016-03-03
  • Unity实现毫秒延时回调功能

    Unity实现毫秒延时回调功能

    这篇文章主要为大家详细介绍了Unity实现毫秒延时回调功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Unity3D绘制地形的实现方法

    Unity3D绘制地形的实现方法

    这篇文章主要为大家详细介绍了Unity3D绘制地形的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • c#显示当前在线人数示例

    c#显示当前在线人数示例

    这篇文章主要介绍了c#显示当前在线人数的示例,需要的朋友可以参考下
    2014-02-02
  • 支持多类型数据库的c#数据库模型示例

    支持多类型数据库的c#数据库模型示例

    本文为大家提供一个c#数据库访问模型,支持多类型数据库,简单抽取数据库访问函数,大家参考使用吧
    2014-01-01
  • C#中正则表达式(Regex)过滤内容的基本使用方法

    C#中正则表达式(Regex)过滤内容的基本使用方法

    在 Regex 类中提供了很多方法来操作正则表达式,这篇文章主要给大家介绍了关于C#中正则表达式(Regex)过滤内容的基本使用方法,需要的朋友可以参考下
    2022-08-08
  • C#中DataTable实现筛选查询的示例

    C#中DataTable实现筛选查询的示例

    本文主要介绍了C#中DataTable实现筛选查询的示例,主要是DataTable进行过滤筛选,常用的一些方法为:Select,dataview,具有一定的参考价值,感兴趣的可以了解一下
    2023-04-04
  • C#读写Excel的流程步骤

    C#读写Excel的流程步骤

    这篇文章主要介绍了详解C#读写Excel的流程步骤,文中通过示例代码介绍的非常详细,对大家的学习或工作有一定的参考学习价值,需要的朋友们下面随着小编来一起来学习吧
    2023-12-12
  • C#实现装饰器模式

    C#实现装饰器模式

    这篇文章介绍了C#实现装饰器模式的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • C#算法之整数反转

    C#算法之整数反转

    这篇文章介绍了C#算法之整数反转,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-01-01

最新评论