当前位置:网站首页>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;
}
}
}
边栏推荐
- MySQL time processing
- [mathematical modeling] differential equation -- sustainable development of fishing industry
- Crazy God said redis notes
- Nestjs配置文件上传, 配置中间件以及管道的使用
- 二叉树基本知识和例题
- Compilation and connection of shader in games202 webgl (learn from)
- Introduction of several RS485 isolated communication schemes
- The video in win10 computer system does not display thumbnails
- Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
- Please wait while Jenkins is getting ready to work
猜你喜欢
Why does MySQL need two-phase commit
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
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
比尔·盖茨晒18岁个人简历,48年前期望年薪1.2万美元
ISP learning (2)
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
idea一键导包
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
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
Nacos - TC Construction of High available seata (02)
随机推荐
Codeforces Round #804 (Div. 2)
程序员在互联网行业的地位 | 每日趣闻
Sorting out the knowledge points of multicast and broadcasting
趋势前沿 | 达摩院语音 AI 最新技术大全
Summary of three log knowledge points of MySQL
2021robocom robot developer competition (Preliminary)
Vite configures the development environment and production environment
The web project imported the MySQL driver jar package but failed to load it into the driver
關於Unity Inspector上的一些常用技巧,一般用於編輯器擴展或者其他
Postman前置脚本-全局变量和环境变量
The IPO of mesk Electronics was terminated: Henan assets, which was once intended to raise 800 million yuan, was a shareholder
ORM aggregate query and native database operation
[mathematical modeling] differential equation -- sustainable development of fishing industry
Some common skills on unity inspector are generally used for editor extension or others
组播和广播的知识点梳理
Postman manage test cases
nacos-高可用seata之TC搭建(02)
Yolov5 tensorrt acceleration
Request (request object) and response (response object)
[leetcode16] the sum of the nearest three numbers (double pointer)