C#NET字符编码加解密的参考代码
2021-03-15 18:35
标签:using name 字符编码 encode net color public col oar C#NET字符编码加解密的参考代码 标签:using name 字符编码 encode net color public col oar 原文地址:https://www.cnblogs.com/wz2988/p/12416558.html //C#NET字符编码加解密的参考代码我是从老外程序里面拷贝出来的希望对大家有所用
1 public string Encrypt(string name) {
2 byte ivId = GetIVId(name);
3 cipher.IV = GetIV(ivId);
4 var buf = Encoding.UTF8.GetBytes(name);
5
6 using (var ms = new MemoryStream()) {
7 ms.WriteByte(ivId);
8 using (var stream = new CryptoStream(ms, cipher.CreateEncryptor(), CryptoStreamMode.Write))
9 stream.Write(buf, 0, buf.Length);
10
11 buf = ms.ToArray();
12 return Base64Encode(buf);
13 }
14 }
15
16 public string Decrypt(string name) {
17 using (var ms = new MemoryStream(Base64Decode(name))) {
18 byte ivId = (byte)ms.ReadByte();
19 cipher.IV = GetIV(ivId);
20
21 var result = new MemoryStream();
22 using (var stream = new CryptoStream(ms, cipher.CreateDecryptor(), CryptoStreamMode.Read))
23 stream.CopyTo(result);
24
25 return Encoding.UTF8.GetString(result.ToArray());
26 }
27 }
上一篇:APIView和View的区别