日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C#實(shí)現(xiàn)簡單的文件加密與解密方式_C#教程

作者:Danny_hi ? 更新時(shí)間: 2023-03-20 編程語言

C#實(shí)現(xiàn)文件加密與解密

代碼:

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#進(jìn)行url加密解密與jquery前端加密解密

當(dāng)我們程序發(fā)布于服務(wù)器上會遇到前端報(bào)錯(cuò)。因?yàn)橛刑厥庠驅(qū)е隆?/p>

此時(shí)需要對傳輸?shù)臄?shù)據(jù),進(jìn)行加密,后臺進(jìn)行解密處理

C#進(jìn)行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編碼

總結(jié)

原文鏈接:https://blog.csdn.net/qq_43024228/article/details/106568590

欄目分類
最近更新