How Credit Card Numbers Work — The Luhn Algorithm Explained
Every credit card number follows a precise structure that encodes the card network, issuing bank, account identifier, and a self-checking digit. Understanding this structure helps developers build better payment forms, testers validate input handling, and curious minds appreciate the engineering behind everyday finance.
Anatomy of a Credit Card Number
A credit card number is not a random string of digits. It is a carefully structured identifier made up of three distinct parts:
1. Issuer Identification Number (IIN/BIN) — First 6-8 Digits
The first six to eight digits are called the Issuer Identification Number (IIN), formerly known as the Bank Identification Number (BIN). These digits identify two things: the card network (Visa, Mastercard, etc.) and the specific bank or financial institution that issued the card.
The very first digit is called the Major Industry Identifier (MII) and indicates the broad category of the issuer:
- 1, 2 — Airlines
- 3 — Travel and entertainment (American Express, Diners Club)
- 4 — Banking and financial (Visa)
- 5 — Banking and financial (Mastercard)
- 6 — Merchandising and banking (Discover)
- 7 — Petroleum
- 8 — Healthcare, telecommunications
- 9 — National assignment
2. Account Number — Middle Digits
The digits between the IIN and the check digit form the individual account number. This portion is assigned by the issuing bank and uniquely identifies the cardholder's account. For a typical 16-digit Visa card with a 6-digit IIN, this means digits 7 through 15 (nine digits) represent the account number, allowing up to 1 billion unique accounts per IIN range.
3. Check Digit — Last Digit
The final digit is a check digit calculated using the Luhn algorithm. It serves as a quick error-detection mechanism — if someone mistypes a single digit when entering their card number, the Luhn check will catch it immediately without needing to contact the bank.
Major Card Networks
Each card network has its own IIN ranges and card number lengths:
| Network | Starts With | Length | Example Pattern |
|---|---|---|---|
| Visa | 4 |
16 digits | 4XXX XXXX XXXX XXXX |
| Mastercard | 51-55, 2221-2720 |
16 digits | 5XXX XXXX XXXX XXXX |
| American Express | 34, 37 |
15 digits | 3XXX XXXXXX XXXXX |
| Discover | 6011, 644-649, 65 |
16 digits | 6XXX XXXX XXXX XXXX |
| Diners Club | 300-305, 36, 38 |
14-19 digits | 3XXX XXXX XXXX XX |
| JCB | 3528-3589 |
16 digits | 35XX XXXX XXXX XXXX |
| UnionPay | 62 |
16-19 digits | 62XX XXXX XXXX XXXX |
| Maestro | 5018, 5020, 5038, 6304 |
12-19 digits | 50XX XXXX XXXX (varies) |
The Luhn Algorithm — Step by Step
The Luhn algorithm (also called the "modulus 10" or "mod 10" algorithm) was created by IBM scientist Hans Peter Luhn in 1954. It detects any single-digit error and most transpositions of adjacent digits. Here is how it works:
How to Validate a Card Number
- Start from the rightmost digit (the check digit) and move left
- Double every second digit (i.e., the second-to-last, fourth-to-last, etc.)
- If doubling results in a number greater than 9, subtract 9 from it
- Sum all the digits
- If the total modulo 10 equals 0, the number is valid
Worked Example
Let's validate the number 4539 1488 0343 6467:
Why Fake Card Numbers Pass Validation but Cannot Make Purchases
When a generator creates a fake credit card number, it constructs the number to pass the Luhn checksum. This means the number has a valid format — the correct prefix for the card network, the right number of digits, and a mathematically correct check digit.
However, passing Luhn validation is just the first of many checks a card must clear to complete a transaction:
- Luhn validation — format check (fake numbers pass this)
- BIN lookup — the issuer verifies the IIN range is assigned to them
- Account lookup — the issuer checks if the account number exists in their database
- Authorization — the issuer verifies available credit, card status, fraud flags, and more
- CVV/CVC verification — a cryptographic check against the card's security code
- AVS (Address Verification) — the billing address is matched
A generated number will fail at step 2 or 3 because the account number does not exist in any bank's database. This is why fake card numbers are useful for testing form validation but cannot be used for fraud.
CVV/CVC Explained
The Card Verification Value (CVV) or Card Verification Code (CVC) is a 3- or 4-digit security code printed on the physical card but not encoded in the magnetic stripe or card number itself.
- Visa, Mastercard, Discover: 3-digit code on the back (called CVV2 or CVC2)
- American Express: 4-digit code on the front (called CID)
The CVV is generated using a cryptographic algorithm that takes the card number, expiration date, and a secret key held only by the issuer. This means it is impossible to calculate a valid CVV from just the card number — you need the issuer's secret key.
Expiration Dates
Credit card expiration dates are formatted as MM/YY and serve several purposes:
- Security rotation — forces periodic card replacement, which updates the CVV
- Card-not-present verification — merchants require the expiration date for online transactions
- Physical wear — cards degrade over time, especially magnetic stripes
Most cards are issued with a 3-5 year validity period. The card expires at the end of the printed month — a card showing 03/27 is valid through March 31, 2027.
How to Identify Card Type from the First Digits
You can determine the card network with just the first one or two digits. This is how payment forms auto-detect the card type and show the appropriate logo as you type:
| First Digit(s) | Network | Quick Rule |
|---|---|---|
4 |
Visa | Always starts with 4 |
51-55 |
Mastercard | 51 through 55 (legacy range) |
2221-2720 |
Mastercard | New range added in 2017 |
34 or 37 |
American Express | Starts with 34 or 37, 15 digits total |
6011 or 65 |
Discover | 6011, 644-649, or 65 |
35 |
JCB | 3528 through 3589 |
62 |
UnionPay | Always starts with 62 |
36 |
Diners Club International | Starts with 36 |
Frequently Asked Questions
No. Fake credit card numbers pass basic format validation using the Luhn algorithm, but they are not linked to any real bank account. Any payment processor will decline them during the authorization step because the account does not exist in the issuer's database.
The Luhn algorithm is a checksum formula that catches accidental entry errors like single-digit typos and adjacent-digit transpositions. It is used on credit cards, IMEI numbers, and some national IDs. It is not a security measure and does not verify account existence.
Check the first digits: Visa starts with 4, Mastercard with 51-55 or 2221-2720, American Express with 34 or 37, and Discover with 6011 or 65. The number of digits also helps — Amex cards have 15 digits while most others have 16.
BIN (Bank Identification Number) and IIN (Issuer Identification Number) refer to the first 6-8 digits of a card number. They identify the issuing bank and card network. The industry now prefers the term IIN, but both terms are widely used interchangeably.