You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Web;
|
|
|
|
namespace TradeManageNew
|
|
{
|
|
public static class EncryptionHelp
|
|
{
|
|
public static string EncryptString(string plainText, string key)
|
|
{
|
|
using (Aes aesAlg = Aes.Create())
|
|
{
|
|
aesAlg.Key = Encoding.UTF8.GetBytes(key);
|
|
aesAlg.IV = new byte[16]; // 使用默认IV
|
|
|
|
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
|
|
|
|
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
|
|
|
|
byte[] encryptedBytes;
|
|
|
|
using (var msEncrypt = new System.IO.MemoryStream())
|
|
{
|
|
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
|
|
{
|
|
csEncrypt.Write(plainBytes, 0, plainBytes.Length);
|
|
csEncrypt.FlushFinalBlock();
|
|
encryptedBytes = msEncrypt.ToArray();
|
|
}
|
|
}
|
|
|
|
return Convert.ToBase64String(encryptedBytes);
|
|
}
|
|
}
|
|
|
|
public static string GetMD5(string input)
|
|
{
|
|
// 将输入字符串转换为字节数组
|
|
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
|
|
|
|
// 创建一个MD5实例
|
|
MD5 md5 = MD5.Create();
|
|
|
|
// 计算输入数据的MD5哈希值
|
|
byte[] hashBytes = md5.ComputeHash(inputBytes);
|
|
|
|
// 将计算得到的哈希值转换为字符串表示
|
|
StringBuilder sb = new StringBuilder();
|
|
for (int i = 0; i < hashBytes.Length; i++)
|
|
{
|
|
sb.Append(hashBytes[i].ToString("x2"));
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
} |