C#实现文件加密与解密
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
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加密与解密
1
2
3
4
5
6
|
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
1
2
|
decodeURIComponent(val); //Jquery解码 encodeURIComponent(val); //Jquery编码 |
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_43024228/article/details/106568590