Credit Card Gen

daviddan2010 0 Tallied Votes 2K Views Share

First i'd like to say that this is for testing only. If you try to use this program for any illegal practices what so ever... you will be responsable for your actions and i will be responsable for nothing. you would need some kind of security code generator to illegally use these generated card numbers any ways. and im pretty sure that they don't exist because the banks issue them. but im not the most knowlagable about these kinds of things either. i just researched the anatomy of a credit card a little.

This program will generate sudo random credit card numbers for Master, Discover, American Express, and Visa cards. There is also a check in the code that will verify that it has generated a valid card number based on card prefix to card issuer, card lenth to card issuer, and Luhn check.

if there is anything that you see wrong with the logic, please let me know. and if you guys have any suggestions of what i can do better, or how to make it less lines, then shoot me a line. also sorry that it's so sloppy. i try to keep it clean but im not a very organized person and i have only been programming in python for a few months. thanks

import re
import random
import operator

def card_gen(card_type, card_dict, pre_list):
    card_type = card_type
    card_dict = card_dict
    card_num = ''

    pre_list = pre_list
    
    index = pre_list[pre_list.index(card_type)+1]
    prefix = str(random.randint(int(index[0]),int(index[len(index)-1])))

    for x in range(card_dict[card_type]-(len(prefix)+1)):
        rand_num = str(random.randint(0,9))
        card_num = card_num + rand_num

    card_num = prefix + card_num + '5'
    
    return card_num, prefix


def card_choice():
    while True:
        card_type = raw_input('Card Type: (1) - Visa (2) - Master (3) - Discover (4) -  American >> ')
        
        if card_type == '1':
            card_type = 'Visa'
            break
        if card_type == '2':
            card_type = 'Master'
            break
        if card_type == '3':
            card_type = 'Discover'
            break
        if card_type == '4':
            card_type = 'American'
            break
        else:
            print '\n%s is not a valid choice\n' % card_type
            
    visa_len = random.randint(1,2)
    if visa_len == 1:
        visa_len = 16
    else:
        visa_len = 13

    master_len = 16
    discover_len = 16
    american_len = 15
    
    card_dict = {'Visa': visa_len, 'Master': master_len, 'Discover': discover_len, 'American': american_len}
        
    
    return card_type, card_dict

    
def card_valid(card_type=None, card_num=None):
    errors = []
    pre_list = [ 'Visa',['4'], 'Master',['51','52','53','54','55'], 'Discover',['6011'], 'American', ['34','37'] ]
    
    if card_type:
        card_type = card_type
    else:
        card_type = card_choice()
        card_dict = card_type[1]
        card_type = card_type[0]
        card_num = card_gen(card_type, card_dict, pre_list)
        prefix = card_num[1]
        card_num = card_num[0]
        

    if card_num:
        card_number = card_num
    else:
        card_number = card_gen(card_type)
    
    card_list = re.findall("[0-9]",card_number)
    card_list.reverse()
    
    if card_type == 'Visa':
        if len(card_number) != 16 and len(card_number) != 13:
            errors.append('Invalid length for Visa')
        if card_number[0] not in pre_list[pre_list.index(card_type)+1]:
            errors.append('Invalid prefix for Visa')
            
    elif card_type == 'Master':
        if len(card_number) != 16:
            errors.append('Invalid length for Master')

        if card_number[:2] not in pre_list[pre_list.index(card_type)+1]:
            errors.append('Invalid prefix for Master')
    
    elif card_type == 'Discover':
        if len(card_number) != 16:
            errors.append('Invalid length for Dicover')
        if card_number[:4] not in pre_list[pre_list.index(card_type)+1]:
            errors.append('Invalid prefix for Discover')
            
    elif card_type == 'American':
        if len(card_number) != 15:
            errors.append('Invalid length for American')
        if card_number[:2] not in pre_list[pre_list.index(card_type)+1]:
            errors.append('Invalid prefix for American')
    else:
        errors.append('Some how you managed to get here.')
            
            
    if not errors:   
        num = 0
        for x in range(len(card_list)):
            if (x+1)%2 == 0:
                card_num = int(card_list[x])*2
                card_num = '%d' % (card_num)
                if len(card_num) == 2:
                    card_num = re.findall('[0-9]',card_num)
                    for x in range(len(card_num)):
                        num = num + int(card_num[x])
                else:
                    num = num + int(card_num)
            else:
                num = num + int(card_list[x])
    
        if num%10 == 0:
            print card_type + ' : ' + card_number
            print 'Valid Card'
            bool = True
        else:
            print card_type + ' : ' + card_number
            print 'invalid Card'
            bool = card_number + '<<%s>>' % num
    else:
        card_type = ''
        bool = ''
        
    if errors:
        for x in errors:
            print x
            
    return card_type, bool


def card_make(card_type, card_change):
    card_change = card_change
    card_change = re.split('(\W\W)',card_change)
    del card_change[len(card_change)-1]
    
    for x in card_change:
        if x == '<<' or x == '>>':
            del card_change[card_change.index(x)]
    
    card_num = card_change[0]
    card_num_int = int(card_num[len(card_num)-1])
    card_dif = card_change[1]
    card_dif = int(card_dif[len(card_dif)-1])
    
    if card_dif > 5:
        card_num_int = card_num_int + (10 - card_dif)
    else:
        card_num_int = card_num_int - card_dif
        
    card_num = card_num[:len(card_num)-1]
    card_num = card_num + '%s' % card_num_int
    
    return card_type, card_num



   
card = card_valid()

if card[1] != True and card[1] != '':
    make = card_make(card[0],card[1])
    card_valid(make[0],make[1])
elif card[1] == '':
    pass
else:
    print 'First try gen'
TrustyTony 888 pyMod Team Colleague Featured Poster

Luhn test is one Rosetta code task:
http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers

Python solution on that page currently:

def luhn(n):
    r = [int(ch) for ch in str(n)][::-1]
    return (sum(r[0::2]) + sum(sum(divmod(d*2,10)) for d in r[1::2])) % 10 == 0
 
for n in (49927398716, 49927398717, 1234567812345678, 1234567812345670):
    print(n, luhn(n))
"""Output:
(49927398716L, True)
(49927398717L, False)
(1234567812345678L, False)
(1234567812345670L, True)
"""

Old interactive solution reformatted by me to untabified program file form.

daviddan2010 0 Newbie Poster

right on man thanks. pretty cool

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.