许明会的计算机技术主页

Language:C,C++,.NET Framework(C#)
Thinking:Design Pattern,Algorithm,WPF,Windows Internals
Database:SQLServer,Oracle,MySQL,PostSQL
IT:MCITP,Exchange,Lync,Virtualization,CCNP

导航

魔獸世界台服身份證ID生成器

魔獸世界台服要啟用battle.net 了,到時候全世界同步,享受同樣的待遇。但是我等大陸網民不屬於“全世界”,所以駐留在台服上需要重新註冊“戰網”了。
理所當然,台服戰網要求註冊要求提供台灣人民的身份證ID等信息,我們沒辦法提供身份證,只能造假,當然冒著被盜號的風險(你沒身份證怎麼找回?)
完美主義者,辦假證也要辦個好的,那麼怎麼能取得一個比較亮的身份證呢,需要自己開發了。哎,這麼個簡單的代碼,我居然用了一天的時間,沒天賦啊。

我希望身份證全部帶8,或者全部帶6,或者我希望的任意的號碼!
好吧,台灣身份證共10位,第一位為所在地,第二位為男女,你可以指定裡面的8個數字,讓程序填寫第一位和最後一位;甚至,你乾脆指定後9位數字好了!
提供幾個靚號:A123456789  M123456789  Y123456789 ,程序代碼,見下面了,直接編譯運行,要什麼有什麼。源代碼和執行體這裡下載

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleDemo
{
class Program
{
/*
身份证字号检查规则
算法1.
1.台湾身分证字号共有十码,我们就将它表示成... N1 N2 N3 N4 N5 N6 N7 N8 N9 N10
2.N1 : 一定是一个大写英文字母,代表户籍地 ex.A是台北市 B是台中市 C是基隆市......
3.N2 : 性别栏位:1为男性 2为女性
4.N3~N9 : 流水号
5.N10 : 检测位元,首先要将第一个英文字转换成数值
* A=10 B=11 C=12 D=13 E=14 F=15 G=16 H=17
* J=18 K=19 L=20 M=21 N=22
* P=23 Q=24 R=25 S=26 T=27 U=28 V=29 W=30 X=31 Y=32 Z=33
* I=34 O=35 ==>I,O
6.然后用下列算式计算,若余数为 0 则为正确的身分证字号:
(N1的十位数+N1的个位数x9+N2x8+N3x7+N4x6+N5x5+N6x4+N7x3+N8x2+N9+N10)÷10

范例:
1. F212345674 换算为 15212345674
2. (1*1)+(5*9)+(2*8)+(1*7)+(2*6)+(3*5)+(4*4)+(5*3)+(6*2)+(7*1)+(4*1)=150
3. 150 mod 10 = 0 => Correct

-------------------------------------------------------------------------------------------------------------
算法2.
第一码英文字代表该组号码配赋时所属的县市〔非出生地〕,第二码数字1为男生2为女生,第3码至第10码为任意一串数字.
* 含英文字之全部号码可用公式加以核算该组号码是否正确,例如R123456783,R=25,
* 检查公式是:2+5*9+1*8+2*7+3*6+4*5+5*4+6*3+7*2+8*1=167,第10码取公式结果的个位数7以10减去得3。
A台北市 B台中市 C基隆市 D台南市 E高雄市 F台北县 G宜兰县 H桃园县 I嘉义市 J新竹县 K苗栗县 L台中县
M南投县 N彰化县 O新竹市 P云林县 Q嘉义县 R台南县 S高雄县 T屏东县 U花莲县 V台东县 W金门县 X澎湖县
Y阳明山 Z连江县

*/
public static bool IsValidID(string ID)
{
int[] NUM = new int[10];
int lenght = ID.Length;
if (lenght != 10)
return false; //lengh MUST be 10.
if (!char.IsUpper(ID, 0))
return false; //Not An UPPER Character,ERROR
for (int i = 1; i < 10; i++)
{
//Console.Write("{0}\t",char.IsDigit(ID,i));
NUM[i] = int.Parse(ID[i].ToString());
if (!char.IsDigit(ID, i))
return false; //2~10,Not Digital,ERROR
}
//上面保证了ID的第一个符号为A-Z-------------------------------------
char charFirst = ID[0];
switch (charFirst) //A=65;Z=90
{
case 'I'://I=34 O=35 ==>I,O
NUM[0] = 34;
break;
case 'O':
NUM[
0] = 35;
break;
//A=10 B=11 C=12 D=13 E=14 F=15 G=16 H=17
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
NUM[
0] = charFirst - 55;
break;
// J=18 K=19 L=20 M=21 N=22
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
NUM[
0] = charFirst - 56;
break;
//P=23 Q=24 R=25 S=26 T=27 U=28 V=29 W=30 X=31 Y=32 Z=33
default:
NUM[
0] = charFirst - 57;
break;
}
//Console.Write(NUM[0]);
// (N1的十位数+N1的个位数x9+N2x8+N3x7+N4x6+N5x5+N6x4+N7x3+N8x2+N9x1+N10)÷10
int total = 0;
for (int i = 1; i < 9; i++)
{
total
+= NUM[i] * (9 - i);
}
//
string city = NUM[0].ToString();
total
+= int.Parse(city[0].ToString()) + int.Parse(city[1].ToString()) * 9 + NUM[9];
return (0 == total % 10);
}

/// <summary>
/// 根据给定的8数字seed字串,分别补上首字母A-Z和尾部校验数字0-9
/// F212345674 为一合法ID
/// </summary>
/// <param name="seed8">ID号码的2到9尾数字</param>
/// <returns></returns>
public static List<string> GetIdByChar8(string seed8)
{
string ID = "", city = "";
int total = 0;
int[] NUM = new int[10];
List
<string> IDs = new List<string>();

if (seed8.Length != 8)
return null;
for (int i = 0; i < seed8.Length; i++)
{
if (!char.IsDigit(seed8, i))
{
return null;
}
NUM[i
+ 1] = int.Parse(seed8[i].ToString());
}
for (int i = 65; i < 91; i++)
{
//判断最后一个数字位数值
NUM[0] = i;
total
= 0;

if (NUM[0] == 'I')
city
= "34";
if (NUM[0] == 'O')
city
= "35";
if (NUM[0] >= 'A' && NUM[0] < 'I')
city
= (NUM[0] - 65 + 10).ToString();
if (NUM[0] >= 'J' && NUM[0] < 'N')
city
= (NUM[0] - 66 + 10).ToString();
if (NUM[0] >= 'P' && NUM[0] < 'Z')
city
= (NUM[0] - 67 + 10).ToString();
//(N1的十位数+N1的个位数x9+N2x8+N3x7+N4x6+N5x5+N6x4+N7x3+N8x2+N9+N10)÷10
total += int.Parse(city[0].ToString()) + int.Parse(city[1].ToString()) * 9;
for (int j = 1; j < 9; j++)
total
+= int.Parse(seed8[j - 1].ToString()) * (9 - j);
if (total % 10 == 0)
NUM[
9] = 0;
else
NUM[
9] = 10 - total % 10;

ID
= (char)NUM[0] + seed8 + NUM[9].ToString();
if (IsValidID(ID))
IDs.Add(ID);
}

return IDs;
}

public static List<string> GetIdByChar9(string seed9)
{
string city = "", ID = "";
int total = 0;
int[] NUM = new int[10];
Random rand
= new Random();
List
<string> IDs = new List<string>();

if (seed9.Length != 9)
return null;
for (int i = 0; i < seed9.Length; i++)
{
if (!char.IsDigit(seed9, i))
{
return null;
}
NUM[i
+ 1] = int.Parse(seed9[i].ToString());
}
NUM[
0] = rand.Next(65, 91);
//判断第一个字母
//(N1的十位数+N1的个位数x9+N2x8+N3x7+N4x6+N5x5+N6x4+N7x3+N8x2+N9+N10)÷10
//total += int.Parse(city[0].ToString()) + int.Parse(city[1].ToString()) * 9;
for (int i = 1; i < 9; i++)
total
+= int.Parse(seed9[i - 1].ToString()) * (9 - i);
total
+= int.Parse(NUM[9].ToString());
//10~19->1~82 20-29->2~83 30~35->3~48
int firstNum = ((int)(total / 10) + 1) * 10;
int lastNum = (int)((firstNum + 83) / 10) * 10;
int tempNum = firstNum, firstCharOfID;
while (tempNum <= lastNum)
{
firstCharOfID
= tempNum - total;
for (int i = 10; i <= 35; i++)
{
//查找符合的数值
char ch = 'A';
city
= i.ToString();
int bit1 = int.Parse(city[0].ToString());
if (firstCharOfID == bit1 + int.Parse(city[1].ToString()) * 9)
{
//添加当前i值
if (i == 34)
ch
= 'I';
if (i == 35)
ch
= 'O';
if (i >= 10 && i < 18) //A=10 B=11 C=12 D=13 E=14 F=15 G=16 H=17
ch = (char)(i + 55);//A(65)~10
if (i >= 18 && i < 23)//J=18 K=19 L=20 M=21 N=22
ch = (char)(i + 56);
if (i >= 23 && i <= 33)// P=23 Q=24 R=25 S=26 T=27 U=28 V=29 W=30 X=31 Y=32 Z=33
ch = (char)(i + 57);
//
ID = ch + seed9;
if (IsValidID(ID))
IDs.Add(ID);
}
}
tempNum
+= 10;

}
return IDs;
}

static void Main(string[] args)
{
//if (args.Length != 1)
//{
// Console.WriteLine("Please Give the ID Parameter(Must be 8 or 9 Numbers");
// return;
//}
string idSeed = "123456789";// args[0];
List<string> IDs;

if (idSeed.Length == 8)
IDs
= GetIdByChar8(idSeed);
else if (idSeed.Length == 9)
IDs
= GetIdByChar9(idSeed);
else
IDs
= null;
foreach (var item in IDs)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}

posted on 2011-04-26 17:26  许明会  阅读(2167)  评论(0编辑  收藏  举报