Luhn 演算法(MOD 10 檢查碼)廣泛用於信用卡卡號、IMEI 等識別碼的驗證。本文提供一支 Python 程式,可依各大卡組織前綴規則,產生符合 Luhn 檢查的測試卡號。僅供開發測試使用,請勿用於非法用途。
#!/usr/bin/python
"""gencc: 產生通過 Luhn 檢查的信用卡卡號"""
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):
cc = prefix[:]
while len(cc) < length - 1:
cc.append(str(Random().randint(0,9)))
# Luhn 計算
total = 0
rev = cc[::-1]
for i, ch in enumerate(rev):
d = int(ch)
if i % 2 == 0:
d *= 2
if d > 9: d -= 9
total += d
check = (total * 9) % 10
cc.append(str(check))
return ''.join(cc)
def credit_card_number(prefixList, length, count):
res = []
rnd = Random()
for _ in range(count):
prefix = copy.copy(rnd.choice(prefixList))
res.append(completed_number(prefix, length))
return res
def output(title, numbers):
lines = [title, '-'*len(title)]
lines.extend(numbers)
lines.append('')
return '\n'.join(lines)
if __name__ == '__main__':
rnd = Random()
print("darkcoding credit card generator\n")
print(output("Mastercard", credit_card_number(mastercardPrefixList, 16, 10)))
print(output("VISA 16 digit", credit_card_number(visaPrefixList, 16, 10)))
print(output("VISA 13 digit", credit_card_number(visaPrefixList, 13, 5)))
print(output("American Express", credit_card_number(amexPrefixList, 15, 5)))
print(output("Discover", credit_card_number(discoverPrefixList, 16, 3)))
print(output("Diners Club", credit_card_number(dinersPrefixList, 14, 3)))
print(output("enRoute", credit_card_number(enRoutePrefixList, 15, 3)))
print(output("JCB", credit_card_number(jcbPrefixList, 16, 3)))
print(output("Voyager", credit_card_number(voyagerPrefixList, 15, 3)))
darkcoding credit card generator
Mastercard
----------
5249241445603178
5474662131739768
...
VISA 16 digit
-------------
4539503803848378
4716973052847436
...
American Express
----------------
376197656730660
342421777432696
...
可搭配 Python 3 Luhn 驗證函數 快速確認生成卡號有效性。
本程式碼基於 GNU GPL 授權釋出,僅供電商網站開發測試使用。任何非法盜刷、詐欺行為均與作者無關,後果自負。