Files
17168ERP/web/admin/follower/follower_code.aspx

150 lines
6.7 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="Newtonsoft.Json" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Model.ezEntities _db = new Model.ezEntities();
MyWeb.encrypt encrypt = new MyWeb.encrypt();
var list = _db.followers.ToList();
StringBuilder html = new StringBuilder();
html.Append("<html><head><title>信眾資料工具</title></head><body>");
html.Append("<h2>信眾資料 - 加密/解密對照</h2>");
html.Append("<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">");
html.Append("<tr style=\"background-color: #f0f0f0; font-weight: bold;\">");
html.Append("<th>num</th>");
html.Append("<th>f_number</th>");
html.Append("<th>u_name</th>");
html.Append("<th>phone (加密)</th>");
html.Append("<th>cellphone (加密)</th>");
html.Append("<th>id_code (加密)</th>");
html.Append("<th>passport (加密)</th>");
html.Append("<th>phone (解密)</th>");
html.Append("<th>cellphone (解密)</th>");
html.Append("<th>id_code (解密)</th>");
html.Append("<th>passport (解密)</th>");
html.Append("<th>分隔符號連接</th>");
html.Append("<th>HEX</th>");
html.Append("<th>HEX 還原</th>");
html.Append("<th>SQL UPDATE</th>");
html.Append("</tr>");
foreach (var item in list)
{
string phone_encrypted = item.phone ?? "";
string cellphone_encrypted = item.cellphone ?? "";
string id_code_encrypted = item.id_code ?? "";
string passport_encrypted = item.passport ?? "";
string phone_decrypted = "";
string cellphone_decrypted = "";
string id_code_decrypted = "";
string passport_decrypted = "";
try {
if (!string.IsNullOrEmpty(phone_encrypted))
phone_decrypted = encrypt.DecryptAutoKey(phone_encrypted);
} catch { phone_decrypted = phone_encrypted; }
try {
if (!string.IsNullOrEmpty(cellphone_encrypted))
cellphone_decrypted = encrypt.DecryptAutoKey(cellphone_encrypted);
} catch { cellphone_decrypted = cellphone_encrypted; }
try {
if (!string.IsNullOrEmpty(id_code_encrypted))
id_code_decrypted = encrypt.DecryptAutoKey(id_code_encrypted);
} catch { id_code_decrypted = id_code_encrypted; }
try {
if (!string.IsNullOrEmpty(passport_encrypted))
passport_decrypted = encrypt.DecryptAutoKey(passport_encrypted);
} catch { passport_decrypted = passport_encrypted; }
html.Append("<tr>");
html.Append("<td>").Append(item.num.ToString()).Append("</td>");
html.Append("<td>").Append(Server.HtmlEncode(item.f_number ?? "")).Append("</td>");
html.Append("<td>").Append(Server.HtmlEncode(item.u_name ?? "")).Append("</td>");
html.Append("<td>").Append(Server.HtmlEncode(phone_encrypted)).Append("</td>");
html.Append("<td>").Append(Server.HtmlEncode(cellphone_encrypted)).Append("</td>");
html.Append("<td>").Append(Server.HtmlEncode(id_code_encrypted)).Append("</td>");
html.Append("<td>").Append(Server.HtmlEncode(passport_encrypted)).Append("</td>");
html.Append("<td>").Append(Server.HtmlEncode(phone_decrypted)).Append("</td>");
html.Append("<td>").Append(Server.HtmlEncode(cellphone_decrypted)).Append("</td>");
html.Append("<td>").Append(Server.HtmlEncode(id_code_decrypted)).Append("</td>");
html.Append("<td>").Append(Server.HtmlEncode(passport_decrypted)).Append("</td>");
// 組合分隔符號連接phone|cellphone|id_code|passport (解密的)
string joinedString = string.Join("|", new string[] {
phone_decrypted,
cellphone_decrypted,
id_code_decrypted,
passport_decrypted
});
html.Append("<td>").Append(Server.HtmlEncode(joinedString)).Append("</td>");
// 將連接字串轉換為 HEX
string hexString = "";
try {
byte[] bytes = Encoding.UTF8.GetBytes(joinedString);
hexString = BitConverter.ToString(bytes).Replace("-", "").ToLower();
} catch {
hexString = "";
}
html.Append("<td>").Append(Server.HtmlEncode(hexString)).Append("</td>");
// 將 HEX 還原為原來的字串
string hexDecoded = "";
try {
byte[] hexBytes = Enumerable.Range(0, hexString.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hexString.Substring(x, 2), 16))
.ToArray();
hexDecoded = Encoding.UTF8.GetString(hexBytes);
} catch {
hexDecoded = "";
}
html.Append("<td>").Append(Server.HtmlEncode(hexDecoded)).Append("</td>");
// 生成 SQL UPDATE 語句
string sqlUpdate = "";
if (!string.IsNullOrEmpty(hexString))
{
// 對 HEX 字串中的單引號進行轉義SQL 需要兩個單引號)
string escapedHex = hexString.Replace("'", "''");
sqlUpdate = string.Format("UPDATE followers SET search_keywords = '{0}' WHERE num = {1};",
escapedHex, item.num);
}
html.Append("<td><pre style=\"margin:0; font-size:12px;\">").Append(Server.HtmlEncode(sqlUpdate)).Append("</pre></td>");
html.Append("</tr>");
}
html.Append("</table>");
html.Append("<p>總筆數: ").Append(list.Count).Append("</p>");
html.Append("</body></html>");
_db.Dispose();
Response.Write(html.ToString());
Response.End();
}
</script>
<!--
-- 方法 1: 搜尋 HEX 編碼的 searchable_field並顯示解碼後的值
DECLARE @searchKeyword VARCHAR(50) = '1286'
DECLARE @hexKeyword VARCHAR(MAX) = CONVERT(VARCHAR(MAX), CONVERT(VARBINARY(MAX), @searchKeyword), 2)
SELECT
num,
f_number,
u_name,
-- 解碼 search_keywords (HEX → 文字)
CONVERT(VARCHAR(MAX),
CONVERT(VARBINARY(MAX), search_keywords, 2)
) AS decoded_searchable_field
FROM followers
WHERE search_keywords LIKE '%' + @hexKeyword + '%'
-->