当前位置:网站首页>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;
}
}
}
边栏推荐
- 最高法院,离婚案件判决标准
- [数学建模] 微分方程--捕鱼业的持续发展
- 组播和广播的知识点梳理
- 力扣(LeetCode)186. 翻转字符串里的单词 II(2022.07.05)
- What are the advantages of the industry private network over the public network? What specific requirements can be met?
- Application of Flody
- EditorUtility. The role and application of setdirty in untiy
- 关于es8316的音频爆破音的解决
- February 12 relativelayout
- nacos-高可用seata之TC搭建(02)
猜你喜欢

Compilation and connection of shader in games202 webgl (learn from)

Pagoda configuration mongodb

Programmers' position in the Internet industry | daily anecdotes

Why does MySQL need two-phase commit

麥斯克電子IPO被終止:曾擬募資8億 河南資產是股東

Review of double pointer problems

acwing周赛58

JS quick start (II)

Flody的应用

Class inheritance in yyds dry inventory C
随机推荐
Compilation and connection of shader in games202 webgl (learn from)
Leetcode 186 Flip the word II in the string (2022.07.05)
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
A blog to achieve embedded entry
Summary of redis AOF and RDB knowledge points
Postman管理测试用例
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
Flink kakfa data read and write to Hudi
Collection + interview questions
Project manager, can you draw prototypes? Does the project manager need to do product design?
Application of Flody
Oracle query table index, unique constraint, field
Nacos - TC Construction of High available seata (02)
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
[effective Objective-C] - memory management
Three.js学习-光照和阴影(了解向)
项目经理,你会画原型嘛?项目经理需要做产品设计了?
The kernel determines whether peripherals are attached to the I2C address
Sorting out the knowledge points of multicast and broadcasting
Finance online homework