認証と計算
6038 ワード
public static class IdCardHelper
{
static readonly int[] Weight = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
static readonly char[] VCode = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
private const string Address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
//
private const int AuditAge = 18;
/// <summary>
/// , , , , 。
/// </summary>
/// <param name="idCardNumList"></param>
/// <returns></returns>
public static List<IdCardUserInfo> CheckIdentityCard(List<string> idCardNumList)
{
var list = new List<IdCardUserInfo>();
if (idCardNumList == null || idCardNumList.Count <= 0)
return list;
foreach (var idCardNum in idCardNumList)
{
var user = new IdCardUserInfo { IdCardNum = idCardNum };
ProcessIdCard(idCardNum, user);
list.Add(user);
}
return list;
}
static void ProcessIdCard(string idCardNum, IdCardUserInfo user)
{
try
{
bool valid;
if (idCardNum.Length == 18)
{
var age = GetAge(idCardNum.Substring(6, 4));
var birthday = idCardNum.Substring(6, 4) + "-" + idCardNum.Substring(10, 2) + "-" +
idCardNum.Substring(12, 2);
if (!CheckValidCode(idCardNum.Substring(0, 17), idCardNum[17]))
valid = false;
else if (!CheckACode(idCardNum.Substring(0, 2)))
valid = false;
else if (age == 0)
valid = false;
else
valid = true;
user.IsValid = valid;
if (valid)
{
int g = Convert.ToInt32(idCardNum.Substring(14, 3));
if (g % 2 == 0)
{
user.Gender = 1;
}
else
{
user.Gender = 0;
}
user.Age = age;
user.IsAdult = age >= AuditAge;
user.BirthDay = Convert.ToDateTime(birthday);
}
}
else if (idCardNum.Length == 15)
{
var age = GetAge("19" + idCardNum.Substring(6, 2));
var birthday = "19" + idCardNum.Substring(6, 2) + "-" + idCardNum.Substring(8, 2) + "-" +
idCardNum.Substring(10, 2);
if (!CheckACode(idCardNum.Substring(0, 2)))
valid = false;
else if (age == 0)
valid = false;
else
valid = true;
user.IsValid = valid;
if (valid)
{
int g = Convert.ToInt32(idCardNum.Substring(12, 3));
if (g % 2 == 0)
{
user.Gender = 1;
}
else
{
user.Gender = 0;
}
user.Age = age;
user.IsAdult = age >= AuditAge;
user.BirthDay = Convert.ToDateTime(birthday);
}
}
else
{
user.IsValid = false;
}
}
catch (Exception ex)
{
user.IsValid = false;
}
}
static bool CheckValidCode(string input17, char last)//
{
if (last == CheckValidCode(input17))
return true;
return false;
}
static char CheckValidCode(string input17)//
{
int sum = 0, cur;
for (int i = 0; i < 17; i++)
{
cur = Convert.ToInt32(input17[i]) - 48;
sum += cur * Weight[i];
}
return VCode[(sum % 11)];
}
static int GetAge(string input4)//
{
try
{
return Convert.ToInt32(DateTime.Now.Year) - Convert.ToInt32(input4);
}
catch { return 0; }
}
static bool CheckACode(string input2)//
{
if (Address.IndexOf(input2) != -1)
return true;
return false;
}
}
public class IdCardUserInfo
{
/// <summary>
///
/// </summary>
public string IdCardNum { get; set; }
/// <summary>
/// 0 1
/// </summary>
public int Gender { get; set; }
/// <summary>
///
/// </summary>
public DateTime BirthDay { get; set; }
/// <summary>
///
/// </summary>
public int Age { get; set; }
/// <summary>
///
/// </summary>
public bool IsValid { get; set; }
/// <summary>
///
/// </summary>
public bool IsAdult { get; set; }
}