身份证号码正确性验证(第18位号码的求法)

发布时间:2011年8月28日 作者:未知 查看次数:1708

身份证号码正确性验证(第18位号码的求法)


身份证号码正确性验证(第18位号码的求法)

身份证中第十八位数字的计算方法为:
1. 将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7、9、10、5、8、4、2、1、6、3、7、9、10、5、8、4、2;
2. 将这17位数字和系数相乘的结果相加;
3. 用加出来和除以11,看余数是多少?
4. 余数只可能有0 、1、 2、 3、 4、 5、 6、 7、 8、 9、 10这11个数字。其分别对应的最后一位身份证的号码为1、0、X、9、8、7、6、5、4、3、2;
5. 例如,通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的Ⅹ。如果余数是10,身份证的最后一位号码就是2。

以下是汇编代码:

szH_SFZCheck db 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1 ;身份证验证系数
szH_SFZRCode db "1","0","X","9","8","7","6","5","4","3","2" ;身份证验证校验码

;检测身份证号的合法性
cCheckSFZ proc uses edi esi ebx ecx edx @addr:DWORD
local @SFZHaoBuf[22]:BYTE
local @tBuf[20]:BYTE
;返回eax=1正确
;eax=0位数不正确(非18位)
;eax=2有不合法字母
;eax=3尾号验证码不正确
;eax=4年龄太大了(超110岁)
;eax=5未出生
;eax=6月份不正确
 mov @SFZHaoBuf[0],0
 invoke lstrcpy,addr @SFZHaoBuf,@addr
 invoke lstrlen,addr @SFZHaoBuf
 .if eax!=18
  mov eax,0
  ret
 .endif
 invoke ucase,addr @SFZHaoBuf ;转为大写
 ;以下验证是否为数字(尾号可X)
 lea esi,@SFZHaoBuf
 mov ecx,0
 mov edx,0
 .while ecx<18
  mov bl,byte ptr [esi]
  .if bl>="0" && bl<="9"
  .else
   .if bl=="X"
   .else
    mov edx,1
   .endif
  .endif
  inc esi
  inc ecx
 .endw
 .if edx==1
  mov eax,2
  ret
 .endif
 ;以下验证年月日
 lea esi,@SFZHaoBuf
 add esi,6
 lea edi,@tBuf
 mov ecx,4
 rep movsb
 mov byte ptr [edi],0
 invoke atodw,addr @tBuf
 .if eax<1900
  mov eax,4
  ret
 .elseif eax>2011
  mov eax,5
  ret
 .endif
 ;月
 lea esi,@SFZHaoBuf
 add esi,10
 lea edi,@tBuf
 mov ecx,2
 rep movsb
 mov byte ptr [edi],0
 invoke atodw,addr @tBuf
 .if eax<1 || eax>12
  mov eax,6
  ret
 .endif
 ;日
 lea esi,@SFZHaoBuf
 add esi,12
 lea edi,@tBuf
 mov ecx,2
 rep movsb
 mov byte ptr [edi],0
 invoke atodw,addr @tBuf
 .if eax<1 || eax>31
  mov eax,7
  ret
 .endif

 ;以下验证尾号
 mov ecx,0
 mov ebx,0
 .while ecx<17
  push ebx
  push ecx
  lea esi,@SFZHaoBuf
  add esi,ecx
  lea edi,@tBuf
  mov ecx,1
  rep movsb
  mov byte ptr [edi],0
  invoke atodw,addr @tBuf
  lea esi,szH_SFZCheck
  pop ecx
  push ecx
  add esi,ecx
  mov ebx,0
  mov bl,byte ptr [esi]
  mov edx,0
  mul ebx

  pop ecx
  pop ebx
  add ebx,eax
  inc ecx
 .endw
 mov eax,ebx
 mov edx,0
 mov ecx,11
 div ecx
 lea esi,szH_SFZRCode
 add esi,edx
 mov ebx,0
 mov bl,byte ptr [esi]
 lea edi,@SFZHaoBuf
 add edi,17
 mov ecx,0
 mov cl,byte ptr [edi]
 .if bl!=cl
  mov esi,@addr
  add esi,17
  mov byte ptr [esi],bl
  mov eax,3
  ret
 .endif
 mov eax,1
 ret
cCheckSFZ endp

 



版权所有!www.sieye.cn
E.Mail:sieye@sohu.com QQ:66697110