using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web; using System.Web.Security; /// /// 加密相關 /// /// namespace MyWeb { public class encrypt { public string MD5(string str, int bits = 32) //MD5加密 { if (bits == 16) { return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16); } else { return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower(); } } public string SHA1(string SourceStr) //SHA1加密 { return FormsAuthentication.HashPasswordForStoringInConfigFile(SourceStr, "SHA1"); } private byte[] Keys = { 0xEF, 0xAB, 0x56, 0x78, 0x90, 0x34, 0xCD, 0x12 }; public string EncryptDes(string SourceStr, string skey) //使用標準DES對稱加密, skey請帶入8位數自訂KEY { try { byte[] rgbKey = Encoding.UTF8.GetBytes(skey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Encoding.UTF8.GetBytes(SourceStr); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); string str = Convert.ToBase64String(mStream.ToArray()); return str; } catch { return SourceStr; } } public string DecryptDes(string SourceStr, string skey) //使用標準DES對稱解密, skey請帶入8位數自訂KEY { try { byte[] rgbKey = Encoding.UTF8.GetBytes(skey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(SourceStr); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return SourceStr; } } public string EncryptAutoKey(string str) { function f = new function(); if (!f.isStrNull(str)) { string key = f.randKey(8); string encode = EncryptDes(str, key); str = f.randKey(3) + f.Left(key, 5) + f.Left(encode, encode.Length - 2) + f.Right(key, 3) + f.Right(encode, 2); return str; } return ""; } public string DecryptAutoKey(string str) { try { function f = new function(); if (!string.IsNullOrEmpty(str)) { string str2 = str?.Substring(3, (str ?? "").Length - 3); string key = f.Left(str2, 5) + f.Left(f.Right(str2, 5), 3); str2 = str2.Substring(5, str2.Length - 10) + f.Right(str2, 2); return DecryptDes(str2, key); } } catch (Exception ex) { } return str; } public string SHA256(string str) { using (SHA256 hash = SHA256Managed.Create()) { return string.Concat(hash .ComputeHash(Encoding.UTF8.GetBytes(str)) .Select(item => item.ToString("x2"))); } } public string followerHash(string s1, string s2) { string input = s1 + s2; input = new string(input.Where(c => char.IsLetterOrDigit(c)).ToArray()).ToUpper(); return SHA256(input); } /// /// 生成 search_keywords 的 HEX 編碼字串 /// /// follower 物件 /// HEX 編碼字串 public string GenerateSearchKeywords(Model.follower follower) { try { // 解密各欄位 string phone_decrypted = string.IsNullOrEmpty(follower.phone) ? "" : DecryptAutoKey(follower.phone); string cellphone_decrypted = string.IsNullOrEmpty(follower.cellphone) ? "" : DecryptAutoKey(follower.cellphone); string id_code_decrypted = string.IsNullOrEmpty(follower.id_code) ? "" : DecryptAutoKey(follower.id_code); string passport_decrypted = string.IsNullOrEmpty(follower.passport) ? "" : DecryptAutoKey(follower.passport); // 組合字串 (使用 | 分隔) string joinedString = string.Join("|", new string[] { phone_decrypted, cellphone_decrypted, id_code_decrypted, passport_decrypted }); // 轉換為 HEX 編碼 if (string.IsNullOrEmpty(joinedString.Replace("|", ""))) { return ""; // 如果所有欄位都是空的,返回空字串 } byte[] bytes = Encoding.UTF8.GetBytes(joinedString); string hexString = BitConverter.ToString(bytes).Replace("-", "").ToLower(); return hexString; } catch { return ""; // 發生錯誤時返回空字串 } } /// /// 將搜尋字串轉換為 HEX 編碼 (用於搜尋功能) /// /// 要搜尋的文字 /// HEX 編碼字串 public string ConvertToHex(string searchText) { try { if (string.IsNullOrEmpty(searchText)) return ""; byte[] bytes = Encoding.UTF8.GetBytes(searchText); string hexString = BitConverter.ToString(bytes).Replace("-", "").ToLower(); return hexString; } catch { return ""; } } } }