欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-16 22:52:40
控件滚动方法: 复制代码 代码如下://具体方法//movegroup(string u, panel p1, panel p2)//movegroup(方向<...

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

控件滚动方法:

复制代码 代码如下:

//具体方法
//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;
        }
    }
}