当前位置:网站首页>C# AES对字符串进行加密
C# AES对字符串进行加密
2022-07-06 05:02:00 【帅_shuai_】
C# AES对字符串进行加密
public class AESHelper
{
/// <summary>
/// AES加密的密钥 必须是32位
/// </summary>
public static string keyValue = "12345678123456781234567812345678";
/// <summary>
/// AES 算法加密
/// </summary>
/// <param name="content">明文</param>
/// <param name="Key">密钥</param>
/// <returns>加密后的密文</returns>
public static string Encrypt(string content, string Key)
{
try
{
byte[] keyBytes = Encoding.UTF8.GetBytes(Key);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyBytes;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] contentBytes = Encoding.UTF8.GetBytes(content);
byte[] resultBytes = cTransform.TransformFinalBlock(contentBytes, 0, contentBytes.Length);
string result = Convert.ToBase64String(resultBytes, 0, resultBytes.Length);
return result;
}
catch (Exception ex)
{
UnityEngine.Debug.LogError("加密出错:" + ex.ToString());
return null;
}
}
/// <summary>
/// AES 算法解密
/// </summary>
/// <param name="content"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string Decipher(string content, string key)
{
try
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
RijndaelManaged rm = new RijndaelManaged();
rm.Key = keyBytes;
rm.Mode = CipherMode.ECB;
rm.Padding = PaddingMode.PKCS7;
ICryptoTransform ict = rm.CreateDecryptor();
byte[] contentBytes = Convert.FromBase64String(content);
byte[] resultBytes = ict.TransformFinalBlock(contentBytes, 0, contentBytes.Length);
return Encoding.UTF8.GetString(resultBytes);
}
catch (Exception ex)
{
UnityEngine.Debug.LogError("解密出错:" + ex.ToString());
return null;
}
}
}
边栏推荐
- ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
- Three methods of Oracle two table Association update
- Postman Association
- EditorUtility.SetDirty在Untiy中的作用以及应用
- 二叉树基本知识和例题
- [NOIP2009 普及组] 分数线划定
- Hometown 20 years later (primary school exercises)
- 趋势前沿 | 达摩院语音 AI 最新技术大全
- The underlying structure of five data types in redis
- Zynq learning notes (3) - partial reconfiguration
猜你喜欢
趋势前沿 | 达摩院语音 AI 最新技术大全
Compilation and connection of shader in games202 webgl (learn from)
图论的扩展
ORM aggregate query and native database operation
ISP学习(2)
CUDA11.1在线安装
Application of Flody
Zynq learning notes (3) - partial reconfiguration
Weng Kai C language third week 3.1 punch in
Yyds dry inventory SSH Remote Connection introduction
随机推荐
团队协作出了问题,项目经理怎么办?
Oracle query table index, unique constraint, field
Bill Gates posted his 18-year-old resume and expected an annual salary of $12000 48 years ago
MPLS experiment
ORM aggregate query and native database operation
Codeforces Round #804 (Div. 2)
Postman关联
ISP learning (2)
项目经理,你会画原型嘛?项目经理需要做产品设计了?
趋势前沿 | 达摩院语音 AI 最新技术大全
Introduction of several RS485 isolated communication schemes
Summary of redis AOF and RDB knowledge points
Set detailed map + interview questions
Selection sort
IPv6 comprehensive experiment
Upload nestjs configuration files, configure the use of middleware and pipelines
Some common skills on unity inspector are generally used for editor extension or others
win10电脑系统里的视频不显示缩略图
Realize a binary read-write address book
What should the project manager do if there is something wrong with team collaboration?