首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 更多程式設計知識

Python 依 Luhn 演算法隨機產生信用卡卡號(附完整程式碼)

     編輯:更多程式設計知識

前言

Luhn 演算法(MOD 10 檢查碼)廣泛用於信用卡卡號、IMEI 等識別碼的驗證。本文提供一支 Python 程式,可依各大卡組織前綴規則,產生符合 Luhn 檢查的測試卡號。僅供開發測試使用,請勿用於非法用途。

核心邏輯

  1. 依卡別前綴(Visa、Mastercard、Amex 等)起始。
  2. 隨機補齊至指定長度減一位。
  3. 由右至左每第二位乘二,大於 9 減 9,累加總和。
  4. 計算檢查碼使總和為 10 的倍數。

完整程式碼

#!/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 授權釋出,僅供電商網站開發測試使用。任何非法盜刷、詐欺行為均與作者無關,後果自負。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved