当前位置:网站首页>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;
}
}
}
边栏推荐
- Rce code and Command Execution Vulnerability
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- win10电脑系统里的视频不显示缩略图
- Class inheritance in yyds dry inventory C
- Redis 排查大 key 的4种方法,优化必备
- 内核判断i2c地址上是否挂载外设
- Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
- Orm-f & Q object
- Tetris
- MPLS experiment
猜你喜欢
Microblogging hot search stock selection strategy
[数学建模] 微分方程--捕鱼业的持续发展
Pagoda configuration mongodb
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
趋势前沿 | 达摩院语音 AI 最新技术大全
Postman assertion
图论的扩展
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
The IPO of mesk Electronics was terminated: Henan assets, which was once intended to raise 800 million yuan, was a shareholder
ISP学习(2)
随机推荐
优秀PM必须经历这3层蜕变!
Finance online homework
Nacos TC setup of highly available Seata (02)
2021robocom robot developer competition (Preliminary)
Tetris
[NOIP2008 提高组] 笨小猴
集合详解之 Collection + 面试题
Basic knowledge and examples of binary tree
程序员在互联网行业的地位 | 每日趣闻
Orm-f & Q object
Fiddler installed the certificate, or prompted that the certificate is invalid
[effective Objective-C] - memory management
Quelques conseils communs sur l'inspecteur de l'unit é, généralement pour les extensions d'éditeur ou d'autres
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
图论的扩展
Summary of redis AOF and RDB knowledge points
Oracle query table index, unique constraint, field
Postman关联
The IPO of mesk Electronics was terminated: Henan assets, which was once intended to raise 800 million yuan, was a shareholder
RT thread analysis - object container implementation and function