- Aes
public static class AesUtil
{
#region AES加密
/// <summary>
/// aes加码
/// </summary>
/// <param name="toEncrypt"></param>
/// <returns></returns>
///
public static string Encrypt(string toEncrypt)
{
try
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("testtesttesttest");
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
RijndaelManaged rDel = new RijndaelManaged();//using System.Security.Cryptography;
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;//using System.Security.Cryptography;
rDel.Padding = PaddingMode.PKCS7;//using System.Security.Cryptography;
ICryptoTransform cTransform = rDel.CreateEncryptor();//using System.Security.Cryptography;
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
StringBuilder builder = new StringBuilder(resultArray.Length * 3);
foreach (byte data in resultArray)
builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
return builder.ToString().ToUpper().Replace(" ", "");
}
catch (Exception ex)
{
Log.Error($"AesEncrypt ex:{ex.Message}");
return "";
}
//return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
#endregion AES加密
#region AES解密
/// <summary>
/// aes解码
/// </summary>
/// <param name="toDecrypt"></param>
/// <returns></returns>
public static string Decrypt(string toDecrypt)
{
try
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("testtesttesttest");
toDecrypt = toDecrypt.Replace(" ", "");
byte[] comBuffer = new byte[toDecrypt.Length / 2];
for (int i = 0; i < toDecrypt.Length; i += 2)
comBuffer[i / 2] = (byte)Convert.ToByte(toDecrypt.Substring(i, 2), 16);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(comBuffer, 0, comBuffer.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
catch (Exception ex)
{
Log.Error($"AesDecrypt ex:{ex.Message}");
return "";
}
}
#endregion AES解密
}
- SHA256
public static class SHA256EncryptUtil
{
/// SHA256加密,并十六进制编码
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string SHA256EncryptString(string data)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
byte[] hash = SHA256Managed.Create().ComputeHash(bytes);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
builder.Append(hash[i].ToString("x2"));
}
return builder.ToString();
}
/// <summary>
/// HMACSHA256加密
/// </summary>
/// <param name="message"></param>
/// <param name="secret"></param>
/// <returns></returns>
public static string HmacSHA256(string message, string secret)
{
secret = secret ?? "";
var encoding = new System.Text.UTF8Encoding();
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < hashmessage.Length; i++)
{
builder.Append(hashmessage[i].ToString("x2"));
}
return builder.ToString();
}
}
/// <summary>
/// 二进制
/// </summary>
/// <param name="message"></param>
/// <param name="secret"></param>
/// <returns></returns>
public static byte[] HmacSHA256Bytes(string message, string secret)
{
secret = secret ?? "";
var encoding = new System.Text.UTF8Encoding();
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
//StringBuilder builder = new StringBuilder();
//for (int i = 0; i < hashmessage.Length; i++)
//{
// builder.Append(hashmessage[i].ToString("x2"));
//}
return hashmessage;
}
}
}
测试密钥文章来源地址https://www.toymoban.com/news/detail-767760.html
公钥
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp3nEMet00r0M72wQW5wpg1Gl06R2iH/p6L9HO5qPw8y2uu5ZXYyP9rcuzMzQ0Gr5NfdJMEf6F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ccr1YbnsFWEPkYARs6L7RzD4dXmhpOlqtVrc1aIRyjYn9Xao6gMOCDluv2D0yghm3CxJ6jzNor9Kuv1WQPd3Uef4rAreaDHzBuJUGNelSZ576ugs/jf+N35YF2jDOgcXbhta1u3wKBgQDAyT1NkFu/rDdOegG5SYpCHBjvjeHKjDqPrmKSz+9pRDHp0AJgbJ4Yl6PNK7RpsDt0MFTSnTGmDTMNqlpcMInaE7qf4hMzrzRwoGvgzj/10l08mbjGnFLtEKVO53bTlTgjL3FPeDO83k3ceyTJLlfUGZtW8596GmPwn6RFRfBfswKBgQDTkMeU9EwpQWo/yq1/8AKyXJqwKoRjDuGjUZmUzW0ii44sHauLXYoUecu2ZoCAI1eBii7nLJgedT+0Y4D3naSf2r/MD+jcxkNEt351+u1csQahKHuopF4zpJZj9DwA/6868JH3RzQsrsMLQ3+Qr1usdz7kEg9uwwReT3MkWNzsiwKBgQC6DXysLnVdNcbqZbTkLKTRyGwIRNVztIGPyKw5I5PZ84Z4L9cbV7f749yhVVx34yx9pWc3G52es35h4dXywqVHYWLqijQd3SiuySNTgbkYdDynD3PTuWoOf9IJpDh5Wbkju9WO7GKCE8jwz6dgwOjiORRBhjySioWI44iVkjm5IQKBgFvbsJmh982qp9dsJuZTzTTcLjw/7wiB1a93Tw/HzPu9s4TjcVVdnJ0SFKHt/ZxToYbuPPHjqiWXi14+lYvp6OLVIsRXufUz965eiCEC3sF6risVhhMacy8KacxVwdIKcDaeSUn3/xEXQhEbghmrhj87RF4K1aA3D2GfK6aBG6V9
-----END PRIVATE KEY-----
文章来源:https://www.toymoban.com/news/detail-767760.html
到了这里,关于c# Aes加解密和SHA256加解密方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!