C#實現(xiàn)簡單的文件加密與解密方式
更新時間:2023年01月25日 14:49:51 作者:Danny_hi
這篇文章主要介紹了C#實現(xiàn)簡單的文件加密與解密方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
C#實現(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#進行url加密解密與jquery前端加密解密
當我們程序發(fā)布于服務器上會遇到前端報錯。因為有特殊原因導致。
此時需要對傳輸?shù)臄?shù)據,進行加密,后臺進行解密處理
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編碼
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

