Zxd.Core/code/DG.Tool/ResUtil.cs

259 lines
8.6 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace DG.Tool
{
public static 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 = ChekMobile(number) || 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>
/// 00852+8位是香港的电话
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static bool ChekMobile(string number)
{
string rgs = @"^(13|14|15|16|17|18|19|0085)\d{9}$";
Regex reg = new Regex(rgs);
return reg.IsMatch(number);
}
/// <summary>
/// 检查是否包含手机号码
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static bool ContainMobile(string number)
{
string rgs = @".*(13|14|15|16|17|18|19)\d{9}.*";
Regex reg = new Regex(rgs);
return reg.IsMatch(number);
}
public static bool CheckIsNum(string txt)
{
string rgs = "^\\d{6,16}$";
Regex reg = new Regex(rgs);
return reg.IsMatch(txt);
}
public static string UserMd5(string str)
{
var cl = str;
var result = string.Empty;
var md5 = MD5.Create();//实例化一个md5对像
// 加密后是一个字节类型的数组这里要注意编码UTF8/Unicode等的选择 
var s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母如果使用大写X则格式后的字符是大写字符
result = result + s[i].ToString("x2");
}
return result;
}
public static string NumberFormat(string number)
{
if (string.IsNullOrEmpty(number) || number.Length <= 6)
{
return number;
}
return number.Substring(0, 3) + new string('*', number.Length - 6) + number.Substring(number.Length - 3);
}
/// <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 = EncryptMD5(rnum + "@no_zuo_no_die@").Substring(10, 13);
Int64 si = Convert.ToInt64(strms, 16);
return Convert.ToString(si, 2).PadLeft(52, '0');
}
/// <summary>
/// md5加密
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static string EncryptMD5(string input)
{
if (string.IsNullOrEmpty(input))
{
return string.Empty;
}
MD5 md5 = MD5.Create();
byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
/// <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;
}
}
}