c# AES加解密并转ASCII码

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
 
namespace AESDecryptDemo
{
   public class AES
    {
       public static string AESEncryptASCII(string plainStr)
       {
           StringBuilder sb = new StringBuilder();
           byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
           byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
           byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);
 
           string encrypt = null;
           Rijndael aes = Rijndael.Create();
           using (MemoryStream mStream = new MemoryStream())
           {
               using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
               {
                   cStream.Write(byteArray, 0, byteArray.Length);
                   cStream.FlushFinalBlock();
                   encrypt = Encode(Convert.ToBase64String(mStream.ToArray()));
               }
           }
           aes.Clear();
           return encrypt;
       }
       public static string AESEncrypt(string plainStr)
       {
           StringBuilder sb = new StringBuilder();
           byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
           byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
           byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);
 
           string encrypt = null;
           Rijndael aes = Rijndael.Create();
           using (MemoryStream mStream = new MemoryStream())
           {
               using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
               {
                   cStream.Write(byteArray, 0, byteArray.Length);
                   cStream.FlushFinalBlock();
                   encrypt = Convert.ToBase64String(mStream.ToArray());
               }
           }
           aes.Clear();
           return encrypt;
       }
       public static string AESDecryptASCII(string encryptStr)
       {
           encryptStr = Decode(encryptStr);
           byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
           byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
           byte[] byteArray = Convert.FromBase64String(encryptStr);
 
           string decrypt = null;
           Rijndael aes = Rijndael.Create();
           using (MemoryStream mStream = new MemoryStream())
           {
               using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
               {
                   cStream.Write(byteArray, 0, byteArray.Length);
                   cStream.FlushFinalBlock();
                   decrypt = Encoding.UTF8.GetString(mStream.ToArray());
               }
           }
           aes.Clear();
           return decrypt;
       }
       public static string AESDecrypt(string encryptStr)
       {
           byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
           byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
           byte[] byteArray = Convert.FromBase64String(encryptStr);
 
           string decrypt = null;
           Rijndael aes = Rijndael.Create();
           using (MemoryStream mStream = new MemoryStream())
           {
               using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
               {
                   cStream.Write(byteArray, 0, byteArray.Length);
                   cStream.FlushFinalBlock();
                   decrypt = Encoding.UTF8.GetString(mStream.ToArray());
               }
           }
           aes.Clear();
           return decrypt;
       }
       public static string Encode(string strEncode)
       {
           string strReturn = "";//  存储转换后的编码
           foreach (short shortx in strEncode.ToCharArray())
           {
               strReturn += shortx.ToString("X");
           }
           return strReturn;
       }
 
       public static string Decode(string strDecode)
       {
           string sResult = "";
           for (int i = 0; i < strDecode.Length / 2; i++)
           {
               sResult += (char)short.Parse(strDecode.Substring(i * 2, 2), global::System.Globalization.NumberStyles.HexNumber);
           }
           return sResult;
       }
       private static string GetKey()
       {
           return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M";
       }
 
       /// <summary>
       /// 获取向量
       /// </summary>
       private static string GetIV()
       {
           return @"L+\~f4,Ir)b$=pkf";
       }
    }
}

  

posted @   青春岁月,无怨无悔  阅读(649)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示