/// <summary> /// 批量生成身份证 /// </summary> /// <param name="count"></param> /// <example> /// List<IDCardNumber> list = IDCardNumber.Radom(number); /// </example> /// <returns></returns> public static List <IdCardNumber> Radom(int count) { List <IdCardNumber> list = new List <IdCardNumber>(); string cardNumber; bool isExits; for (int i = 0; i < count; i++) { do { isExits = false; int tick = (int)DateTime.Now.Ticks; cardNumber = IdCardNumber._radomCardNumber(tick * (i + 1)); foreach (IdCardNumber c in list) { if (c.CardNumber == cardNumber) { isExits = true; break; } } } while (isExits); list.Add(new IdCardNumber(cardNumber)); } return(list); }
/// <summary> /// 解析身份证信息 /// </summary> /// <param name="idCardNumber"></param> /// <example> /// IDCardNumber card = IDCardNumber.Get(code); /// </example> /// <returns>IDCardNumber</returns> public static IdCardNumber Get(string idCardNumber) { if (IdCardNumber.Areas.Count < 1) { IdCardNumber.FillAreas(); } if (!IdCardNumber.CheckIDCardNumber(idCardNumber)) { throw new Exception("非法的身份证号码"); } // IdCardNumber cardInfo = new IdCardNumber(idCardNumber); return(cardInfo); }
/// <summary> /// 生成随身份证号 /// </summary> /// <param name="seed">随机数种子</param> /// <returns></returns> private static string _radomCardNumber(int seed) { if (IdCardNumber.Areas.Count < 1) { IdCardNumber.FillAreas(); } System.Random rd = new System.Random(seed); //随机生成发证地 string area = ""; do { area = IdCardNumber.Areas[rd.Next(0, IdCardNumber.Areas.Count - 1)][0]; } while (area.Substring(4, 2) == "00"); //随机出生日期 DateTime birthday = DateTime.Now; birthday = birthday.AddYears(-rd.Next(16, 60)); birthday = birthday.AddMonths(-rd.Next(0, 12)); birthday = birthday.AddDays(-rd.Next(0, 31)); //随机码 string code = rd.Next(1000, 9999).ToString("####"); //生成完整身份证号 string codeNumber = area + birthday.ToString("yyyyMMdd") + code; double sum = 0; string checkCode = null; for (int i = 2; i <= 18; i++) { sum += int.Parse(codeNumber[18 - i].ToString(), NumberStyles.HexNumber) * (Math.Pow(2, i - 1) % 11); } string[] checkCodes = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" }; checkCode = checkCodes[(int)sum % 11]; codeNumber = codeNumber.Substring(0, 17) + checkCode; // return(codeNumber); }