本文实例讲述了python随机生成信用卡卡号的实现方法。分享给大家供大家参考。具体分析如下:
这段python代码根据信用卡卡号产生规则随机生成信用卡卡号,是可以通过验证的,仅供学习参考,请不要用于非法用途,否则后果自负。
#!/usr/bin/python
“””
gencc: a simple program to generate credit card numbers that pass the
mod 10 check (luhn formula).
usefull for testing e-commerce sites during development.
copyright 2003-2012 graham king
this program is free software; you can redistribute it and/or modify
it under the terms of the gnu general public license as published by
the free software foundation; either version 2 of the license, or
(at your option) any later version.
this program is distributed in the hope that it will be useful,
but without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose. see the
gnu general public license for more details.
you should have received a copy of the gnu general public license
along with this program; if not, write to the free software
foundation, inc., 59 temple place, suite 330, boston, ma 02111-1307 usa
“””
# different naming convention, because translated from php
# pylint: disable=c0103
from random import random
import copy
visaprefixlist = [
[‘4’, ‘5’, ‘3’, ‘9’],
[‘4’, ‘5’, ‘5’, ‘6’],
[‘4’, ‘9’, ‘1’, ‘6’],
[‘4’, ‘5’, ‘3’, ‘2’],
[‘4’, ‘9’, ‘2’, ‘9’],
[‘4’, ‘0’, ‘2’, ‘4’, ‘0’, ‘0’, ‘7’, ‘1’],
[‘4’, ‘4’, ‘8’, ‘6’],
[‘4’, ‘7’, ‘1’, ‘6’],
[‘4’]]
mastercardprefixlist = [
[‘5’, ‘1’], [‘5’, ‘2’], [‘5’, ‘3’], [‘5’, ‘4’], [‘5’, ‘5’]]
amexprefixlist = [[‘3’, ‘4’], [‘3’, ‘7’]]
discoverprefixlist = [[‘6’, ‘0’, ‘1’, ‘1’]]
dinersprefixlist = [
[‘3’, ‘0’, ‘0’],
[‘3’, ‘0’, ‘1’],
[‘3’, ‘0’, ‘2’],
[‘3’, ‘0’, ‘3’],
[‘3’, ‘6’],
[‘3’, ‘8’]]
enrouteprefixlist = [[‘2’, ‘0’, ‘1’, ‘4’], [‘2’, ‘1’, ‘4’, ‘9’]]
jcbprefixlist = [[‘3’, ‘5’]]
voyagerprefixlist = [[‘8’, ‘6’, ‘9’, ‘9’]]
def completed_number(prefix, length):
“””
‘prefix’ is the start of the cc number as a string, any number of digits.
‘length’ is the length of the cc number to generate. typically 13 or 16
“””
ccnumber = prefix
# generate digits
while len(ccnumber) < (length - 1):
digit = str(generator.choice(range(0, 10)))
ccnumber.append(digit)
# calculate sum
sum = 0
pos = 0
reversedccnumber = []
reversedccnumber.extend(ccnumber)
reversedccnumber.reverse()
while pos < length - 1:
odd = int(reversedccnumber[pos]) * 2
if odd > 9:
odd -= 9
sum += odd
if pos != (length – 2):
sum += int(reversedccnumber[pos + 1])
pos += 2
# calculate check digit
checkdigit = ((sum / 10 + 1) * 10 – sum) % 10
ccnumber.append(str(checkdigit))
return ”.join(ccnumber)
def credit_card_number(rnd, prefixlist, length, howmany):
result = []
while len(result) < howmany:
ccnumber = copy.copy(rnd.choice(prefixlist))
result.append(completed_number(ccnumber, length))
return result
def output(title, numbers):
result = []
result.append(title)
result.append('-' * len(title))
result.append('\n'.join(numbers))
result.append('')
return '\n'.join(result)
#
# main
#
generator = random()
generator.seed() # seed from current time
print("darkcoding credit card generator\n")
mastercard = credit_card_number(generator, mastercardprefixlist, 16, 10)
print(output("mastercard", mastercard))
visa16 = credit_card_number(generator, visaprefixlist, 16, 10)
print(output("visa 16 digit", visa16))
visa13 = credit_card_number(generator, visaprefixlist, 13, 5)
print(output("visa 13 digit", visa13))
amex = credit_card_number(generator, amexprefixlist, 15, 5)
print(output("american express", amex))
# minor cards
discover = credit_card_number(generator, discoverprefixlist, 16, 3)
print(output("discover", discover))
diners = credit_card_number(generator, dinersprefixlist, 14, 3)
print(output("diners club / carte blanche", diners))
enroute = credit_card_number(generator, enrouteprefixlist, 15, 3)
print(output("enroute", enroute))
jcb = credit_card_number(generator, jcbprefixlist, 16, 3)
print(output("jcb", jcb))
voyager = credit_card_number(generator, voyagerprefixlist, 15, 3)
print(output("voyager", voyager))
运行结果如下:
darkcoding credit card generator
mastercard
———-
5249241445603178
5474662131739768
5581517450441661
5588253309068355
5514774944030079
5122826749528287
5152966434002124
5268127987312292
5385676074198087
5330181839518097
visa 16 digit
————-
4539503803848378
4716973052847436
4556149936260376
4486565514724761
4556309354978160
4486560485591158
4916213564870669
4486704671699934
4532839100193192
4486613519985266
visa 13 digit
————-
4929905559973
4556870828321
4916868200216
4556189916056
4024007171180
american express
—————-
376197656730660
342421777432696
342896016604918
343458311286898
374543958196088
discover
——–
6011676180456692
6011298953567876
6011699666196053
diners club / carte blanche
—————————
30066643308583
38302315481859
38251784096120
enroute
——-
214935400146170
201435309964331
201450317314858
jcb
—
3506641780149880
3561856856065701
3508395818816022
voyager
——-
869984254736122
869938197246102
869921075232782
希望本文所述对大家的python程序设计有所帮助。