C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#实现文件加密与解密

C#实现简单的文件加密与解密方式

作者:Danny_hi

这篇文章主要介绍了C#实现简单的文件加密与解密方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

C#实现文件加密与解密

代码:

static class HandleFiles
    {
        public static void EncryptFile(string inputFile, string outputFile)   //加密
        {
            try
            {
                string password = @"12345678";
                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);

                string cryptFile = outputFile;
                FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

                RijndaelManaged RMCrypto = new RijndaelManaged();

                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateEncryptor(key, key),
                    CryptoStreamMode.Write);

                FileStream fsIn = new FileStream(inputFile, FileMode.Open);

                int data;
                while ((data = fsIn.ReadByte()) != -1)
                    cs.WriteByte((byte)data);


                fsIn.Close();
                cs.Close();
                fsCrypt.Close();


                MessageBox.Show("Encrypt Source file succeed!", "Msg :");
            }
            catch(Exception ex)
            {
                MessageBox.Show("Source file error!", "Error :");
            }
        }

        public static void DecryptFile(string inputFile, string outputFile)   //解密
        {
            try
            {
                string password = @"12345678";
                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);

                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

                RijndaelManaged RMCrypto = new RijndaelManaged();

                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateDecryptor(key, key),
                    CryptoStreamMode.Read);

                FileStream fsOut = new FileStream(outputFile, FileMode.Create);

                int data;
                while ((data = cs.ReadByte()) != -1)
                    fsOut.WriteByte((byte)data);

                fsOut.Close();
                cs.Close();
                fsCrypt.Close();

                MessageBox.Show("Decrypt Source file succeed!", "Msg :");

            }
            catch(Exception ex)
            {
                MessageBox.Show("Source file error", "Error :");
            }
        }
    }

C#进行url加密解密与jquery前端加密解密

当我们程序发布于服务器上会遇到前端报错。因为有特殊原因导致。

此时需要对传输的数据,进行加密,后台进行解密处理

C#进行url加密与解密

HttpUtility.UrlEncode(val);  //utf-8 编码
HttpUtility.UrlDecode(val);  //utf-8 解码
HttpUtility.UrlEncode(val, System.Text.Encoding.GetEncoding(936));  //gb2312编码
HttpUtility.UrlDecode(val, System.Text.Encoding.GetEncoding(936));  //gb2312解码
System.Web.HttpUtility.UrlEncode(val, System.Text.Encoding.GetEncoding("GB2312"));//gb2312编码
System.Web.HttpUtility.UrlDecode(val, System.Text.Encoding.GetEncoding("GB2312"));//gb2312解码

jquery

decodeURIComponent(val);//Jquery解码
encodeURIComponent(val);//Jquery编码

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文