TG.WXCRM.V4/Common/ResUtil.cs

177 lines
5.6 KiB
C#
Raw 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.

using System;
using System.Text;
using System.Text.RegularExpressions;
namespace WX.CRM.Common
{
public class ResUtil
{
//生成resid
public static string CreateResId(string number)
{
number = number.Replace("+86", "");
if (number.StartsWith("01") && number.Length == 12)
{
number = number.Substring(1);
}
//1、正则表达式提取数字串值
number = Regex.Replace(number, @"[^\d.\d]", "", RegexOptions.IgnoreCase);
//*******开始resid算法******************//
bool ismobile = Utility.ChekMobile(number) || Utility.ChekMobile(number.TrimStart('0'));
if (ismobile)
number = number.TrimStart('0');
string bkn_ms_52 = InReversibleStr(number, ismobile);
string kn_ms_6 = ReversibleStr(number);
string resId = MixResID(bkn_ms_52, kn_ms_6, number, ismobile);
return resId;
}
/// <summary>
/// 不可逆密码串
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
private static string InReversibleStr(string number, bool ismobile)
{
//11位手机前补'x'、不足12位的号码前补'0超过12位的取后12位
string rnum = ismobile ? "x" + number : number;
rnum = rnum.PadLeft(12, '0');
rnum = rnum.Substring(rnum.Length - 12);
string strms = Utility.EncryptMD5(rnum + "@no_zuo_no_die@").Substring(10, 13);
Int64 si = Convert.ToInt64(strms, 16);
return Convert.ToString(si, 2).PadLeft(52, '0');
}
/// <summary>
/// 生成可逆密码串
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
private static string ReversibleStr(string number)
{
//2位可逆
int H2 = Int32.Parse(number.Substring(number.Length - 2)) % 64;
string strms = Convert.ToString(Convert.ToInt64(H2), 2).PadLeft(6, '0');
return MixKN_MS(strms, true);
}
private static string MixResID(string bkn_ms, string kn_ms, string number, bool ismobile)
{
StringBuilder sb = new StringBuilder();
int i = 0;
for (; i < kn_ms.Length; i++)
{
sb.Append(bkn_ms.Substring(i * 2, 2));
sb.Append(kn_ms.Substring(i, 1));
}
sb.Append(bkn_ms.Substring(i * 2));
sb.Append(ismobile ? "1" : "0");
return Convert2TO10(sb.ToString()).ToString().PadLeft(18, '0');
}
private static string MixKN_MS(string kn_ms, bool bMix)
{
int iMove = 2;
StringBuilder sb = new StringBuilder();
if (bMix)
{
kn_ms = kn_ms.Substring(kn_ms.Length - iMove) + kn_ms.Substring(0, kn_ms.Length - iMove);
}
for (int i = 0; i < kn_ms.Length / 2; i++)
{
sb.Append(kn_ms[i * 2 + 1]);
sb.Append(kn_ms[i * 2]);
}
kn_ms = sb.ToString();
if (!bMix)
{
kn_ms = kn_ms.Substring(iMove) + kn_ms.Substring(0, iMove);
}
return kn_ms;
}
/// <summary>
/// 返回后二位的手机号码串
/// </summary>
/// <param name="resId"></param>
/// <returns></returns>
public static string Reve_ms(string resId)
{
string code = Convert10TO2(Convert.ToDecimal(resId)).PadLeft(59, '0');
code = code.Substring(code.Length - 59);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 6; i++)
{
sb.Append(code[i * 3 - 1]);
}
code = MixKN_MS(sb.ToString(), false);
code = Convert.ToInt32(code, 2).ToString().PadLeft(2, '0');
return "*********" + code;
}
/// <summary>
/// 二进制转十进制
/// </summary>
/// <param name="resid"></param>
/// <returns></returns>
public static decimal Convert2TO10(string resid)
{
decimal residNum = 0;
string str = resid;
for (int i = 0, j = resid.Length - 1; i < resid.Length; i++, j--)
{
string bitebag = resid.Substring(j, 1);
decimal t = Convert.ToInt32(bitebag) * Pow(i);
residNum = residNum + t;
}
return residNum;
}
/// <summary>
/// 十进制转二进制
/// </summary>
/// <param name="resudnum"></param>
/// <returns></returns>
public static string Convert10TO2(decimal resudnum)
{
string str = string.Empty;
while (resudnum > 0)
{
int y = Convert.ToInt32(resudnum % Convert.ToDecimal(2));
resudnum = Math.Floor(resudnum / Convert.ToDecimal(2));
str = y.ToString() + str;
}
return str;
}
public static decimal Pow(int y)
{
decimal pow = 1;
if (y == 0)
{
pow = 1;
}
else if (y == 1)
{
pow = 2;
}
else
{
pow = 2;
while (y > 1)
{
pow = pow * 2;
y--;
}
}
return pow;
}
}
}