using System; using System.Security.Cryptography; /// /// 生成密钥 /// public class KeyGenerator { /// /// 根据ID与当前日期生成密钥 /// /// /// public static string GenerateKey(string id) { // 将ID和当前日期组合成一个字符串 //string combinedString = id + DateTime.Now.ToString("yyyyMMdd"); string combinedString = id + DateTime.Now.ToString("yyyyMMddHHmmss"); // 将字符串转换成字节数组 byte[] combinedBytes = System.Text.Encoding.UTF8.GetBytes(combinedString); // 使用SHA256哈希算法生成密钥 using (var sha256 = SHA256.Create()) { byte[] hashBytes = sha256.ComputeHash(combinedBytes); // 将字节数组转换成字符串表示形式作为最终的密钥 string key = BitConverter.ToString(hashBytes)/*.Replace("-", "")*/.ToLower(); return key; } } }