C#實(shí)現(xiàn)簡(jiǎn)單的文件加密與解密方式
更新時(shí)間:2023年01月25日 14:49:51 作者:Danny_hi
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單的文件加密與解密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
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ù)器上會(huì)遇到前端報(bào)錯(cuò)。因?yàn)橛刑厥庠驅(qū)е隆?/p>
此時(shí)需要對(duì)傳輸?shù)臄?shù)據(jù),進(jìn)行加密,后臺(tái)進(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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
WPF實(shí)現(xiàn)自定義Panel面板的示例詳解
WPF中的Panel(面板),是繼承自FrameworkElement的抽象類,表示一個(gè)可以用來排列子元素的面板,本文主要來和大家聊聊WPF如何實(shí)現(xiàn)自定義Panel,感興趣的可以了解下2023-09-09深入多線程之:雙向信號(hào)與競(jìng)賽的用法分析
本篇文章是對(duì)雙向信號(hào)與競(jìng)賽的用法進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器
這篇文章主要介紹了C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器的相關(guān)代碼和使用方法,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-07-07Unity實(shí)現(xiàn)局域網(wǎng)聊天室功能
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)局域網(wǎng)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10