现在的位置: 首页 > 综合 > 正文

python写的随机生成身份证号码的函数

2013年04月02日 ⁄ 综合 ⁄ 共 1902字 ⁄ 字号 评论关闭
现在上网很多地方都要求输入身份证号码,因为我不想在网上随便透露真实身份证号码,所以翻阅了有关资料,写了个随机生成身份证号码的函数。

本函数只能糊弄网站的身份证号码验证程序,到公安局肯定会露馅的,所以请大家谨慎使用。

三个函数:
makeNew()      用于随机生成18位身份证号码
isTrue()             用于验证身份证号码真伪
old2new()         用于将15位身份证号码转换为18位身份证号码

import time, random

ARR 
= (7910584216379105842)
LAST 
= ('1''0''X''9''8''7''6''5''4''3''2')

def makeNew():
    u
''' 随机生成新的18为身份证号码 '''
    t 
= time.localtime()[0]
    x 
= '%02d%02d%02d%04d%02d%02d%03d' %(random.randint(10,99),
                                        random.randint(
01,99),
                                        random.randint(
01,99),
                                        random.randint(t 
- 80, t - 18),
                                        random.randint(
1,12),
                                        random.randint(
1,28),
                                        random.randint(
1,999))
    y 
= 0
    
for i in range(17):
        y 
+= int(x[i]) * ARR[i]

    
return '%s%s' %(x, LAST[y % 11])

def isTrue():
    u
''' 验证身份证号码是否真实号码 '''
    
print u'请输入身份证号码'
    x1 
= raw_input('?')

    xlen 
= len(x1)
    
if xlen != 18 and xlen != 15:
        
return u'身份证号码长度错误'

    
try:
        
if xlen == 18:
            x2 
= x1[6:14]
            x3 
= time.strptime(x2, '%Y%m%d')
            
if x2 < '19000101' or x3 > time.localtime():
                
return u'时间错误,超过允许的时间范围'
        
else:
            x2 
= time.strptime(x1[6:12], '%y%m%d')
    
except:
        
return u'时间错误,非合法时间'

    
if xlen == 18:
        y 
= 0
        
for i in range(17):
            y 
+= int(x1[i]) * ARR[i]

        
if LAST[y % 11!= x1[-1].upper():
            
return u'验证码错误'

    
return u'YES'

def old2new():
    u
''' 15位身份证号码转换为18位身份证号码 '''
    
print u'请输入15位老身份证号码'
    x1 
= raw_input('?')
    
if len(x1) != 15:
        
return u'身份证号码输入错误,身份证号码长度不为15位'

    oldcard 
= '%s19%s' %(x1[:6], x1[6:])
    y 
= 0
    
for i in range(17):
        y 
+= int(oldcard[i]) * ARR[i]

    
return '%s%s' %(oldcard, LAST[y % 11])

抱歉!评论已关闭.