C#實(shí)現(xiàn)簡(jiǎn)單的文件加密與解密方式
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)文章
關(guān)于C#數(shù)強(qiáng)轉(zhuǎn)會(huì)不會(huì)拋出異常詳解
這篇文章主要給大家介紹了關(guān)于C#數(shù)強(qiáng)轉(zhuǎn)會(huì)不會(huì)拋出異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
深入分析C#鍵盤勾子(Hook),屏蔽鍵盤活動(dòng)的詳解
本篇文章是對(duì)C#鍵盤勾子(Hook),屏蔽鍵盤活動(dòng)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C#中調(diào)用VB中Inputbox類的實(shí)現(xiàn)方法
本文主要介紹在項(xiàng)目中引用Microsoft.VisualBasic,間接使用VB中的各種類庫的方法,或者自己創(chuàng)建函數(shù),調(diào)用自定義方法,以實(shí)現(xiàn)InputBox相關(guān)的功能。2016-05-05
C#實(shí)現(xiàn)泛型動(dòng)態(tài)循環(huán)數(shù)組隊(duì)列的方法
隊(duì)列一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),本文通過實(shí)例代碼給大家介紹下C#實(shí)現(xiàn)泛型動(dòng)態(tài)循環(huán)數(shù)組隊(duì)列的方法,感興趣的朋友一起看看吧2022-01-01
C#基于Mongo的官方驅(qū)動(dòng)手?jǐn)]一個(gè)Super簡(jiǎn)易版MongoDB-ORM框架
本文給大家分享C#基于Mongo的官方驅(qū)動(dòng)手?jǐn)]一個(gè)簡(jiǎn)易版MongoDB-ORM框架,是一款屬于super簡(jiǎn)易版的,通過圖文的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-05-05
C# byte轉(zhuǎn)為有符號(hào)整數(shù)實(shí)例
這篇文章主要介紹了C# byte轉(zhuǎn)為有符號(hào)整數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11

