Announcement

The Logic Behind a Credit Card

logic behind credit or debit card - techiners

 INTRODUCTION


A credit card or a debit card has a unique 16 digit number that every one is aware of. But, do you know these numbers are not just random string of numbers rather it reveals a lot more information than you think.

Ever wondered what that 16 digits code actually represents? How that 16 digits identify the bank and the account from which your money needs to be paid to the billing party?

Let's dive into these 16 digits that we just see a string of random numbers and is often wondering that exactly these numbers are.

DESCRIPTION


The first digit of the card represents the category of industry which issued your credit card. This is known as "Major Industry Identifier" short for MII.

Following is the list of MII:

  1. ISOAndOtherIndustries,
  2. Airlines,
  3. AirlinesAndOtherIndustries,
  4. TravelsAndEntertainmentAmericanExpressOrFoodClub,
  5. BankingAndFinanceVISA,
  6. BankingAndFinanceMasterCard,
  7. BankingAndMerchandising,
  8. Petroleum,
  9. TelecommunicationsAndOtherIndustries,
  10. NationalAssignment

The first six digits of the credit card number is known as the Issuer Identification Number short for IIN and this is to identify the institution that issued the card.

VISA cards follow the series 4xx while MasterCard uses 51-55 as the prefix.

Taking away the first 6 identifier digits, the following 6 to 9 digits constitutes unique bank account number. Now this is not exactly the bank account number of a customer but the account number assigned to your credit account.

And lastly, 1 last digit known as the “check digit” which is generated in such a way as to satisfy a certain condition.

LOGIC


Here is the logic behind the condition that must be satisfied:

Let's consider the card number as : 1802909582961827

As of now, just consider first 15 digits i.e., 180290958296182

Now beginning from the left, start with the first number, take every second number and multiply it by 2 as shown here:

1 0 9 9 8 9 1 2 

1 * 2 = 2
0 * 2 = 0
9 * 2 = 18
9 * 2 = 18
8 * 2 = 16
9 * 2 = 18
1 * 2 = 2
2 * 2 = 4

Sum up the digits if the multiplication results into a 2 digit number. i.e., 18 => 1 + 8 = 9
This will give us all the result in a single digit number.

So, we get : 2,0,9,9,7,9,2,4

Now, Sum up all the single digits and all the digits left in the card number.

i.e., 2+0+9+9+7+9+2+4 + 8+2+0+5+2+6+8 = 73

Add the sum together. Now, what must be added to the sum obtained so that it is divisible by 10? In this case, it is 7

So, 73+ 7 = 80 which is divisible by 10.

This last digit so obtained is called as Checksum digit.

That's the logic behind credit/debit card numbers.

How many digits in a Credit Card Number?

Now, number of digits in a credit card number varies with the issuing authority.

Following is the list of some issuing authorities along with their card length:

Issuing AuthorityCard Length (Description)
Visa and Visa Electron13 or 16
Mastercard16
Discover16
American Express15
Diner's Club14 (including enRoute, International, Blanche)
Maestro12 to 19 (multi-national Debit Card)
Laser16 to 19 (Ireland Debit Card)
Switch16, 18 or 19 (United Kingdom Debit Card)
Solo16, 18 or 19 (United Kingdom Debit Card)
JCB15 or 16 (Japan Credit Bureau)
China UnionPay16 (People's Republic of China)


What checks will we perform on your number?

We perform some checks on your number and explain what each part of it means.

That being said, we we'll show you results for:

Luhn algorithm check
Major Industry Identifier
Issuer identification number
Personal Account Number and Checksum

There is another number which is usually behind the card usually of 3 to 4 digits and is known by various names like CVVs, CVV2s called card verification value, card security code (CSC). These are all calculated using the CVV algorithm.

The codes have different names as per issuing authority:


CIDUnique Card CodeIssuing Authority
CIDCard Identification NumberDiscover
CVC2Card Validation CodeMasterCard
CSCCard Security CodeDebit Card/td>
CVN2Card Validation Number 2China UnionPay/td>
CVEElo Verification CodeElo - Brazil/td>
CVV2Card Verification Value 2Visa

They are required by payment systems such as MasterCard and Visa to authenticate their credit or debit cards.

There are several types of security codes:

The first code is encoded on track 2 of the magnetic stripe of the card and used for card present transactions called CVC1 or CVV1,

The second code is often sought by merchants for card occurring by fax, mail, or Internet telephone, it is called CVV2 or CVC2.

The card security code is typically the last three or four digits printed.

Logic behind CVV generation:

To generate or calculate the 3-digit CVV the algorithm requires :

Primary Account Number (PAN), a 4-digit Expiration Date, a pair of DES keys (CVKs), and a 3-digit Service Code.

That being said here are some tips for Card and PIN Safety:

  1. Sign your card as soon as you receive it.
  2. Safeguard your card as though it was cash.
  3. Memorize your PIN. Never write it down.
  4. Make sure you receive your card back from the salesclerk or waiter when you use it.
  5. Shred receipts that contain the full account number if you do not need to keep them.
  6. Review your account statements as soon as you receive them to make sure all the transactions are yours.
  7. Make a list of your credit card numbers and customer service phone numbers and store it in a locked place.
This will make it easier for you to call to cancel the cards if your purse or wallet is lost or stolen.

PROGRAM


Finally, implementing the Luhn Algorithm using C# to generate Credit Card numbers. The program is as :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CreditCardVerification
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter 16 digit credit/debit card number:");
            string creditCardNumber = string.Empty;
            Random ran = new Random();
            for (int i = 1; i <= 10; i++)
            {
                var cardNumber = ran.Next(10).ToString() + ran.Next(10).ToString() + ran.Next(10).ToString() + ran.Next(10).ToString() + ran.Next(10).ToString()
                    + ran.Next(10).ToString() + ran.Next(10).ToString() + ran.Next(10).ToString() + ran.Next(10).ToString() + ran.Next(10).ToString()
                    + ran.Next(10).ToString() + ran.Next(10).ToString() + ran.Next(10).ToString() + ran.Next(10).ToString() + ran.Next(10).ToString()
                    + ran.Next(10).ToString();
                Console.WriteLine(cardNumber);
                if (VerifyCreditCard(cardNumber))
                {
                    Console.WriteLine("Valid");
                }
                else
                {
                    Console.WriteLine("Invalid");
                }
            }
            Console.ReadLine();
        }

        public static class CreditCardVerification
        {

            public enum MajorIndustryIdentifier
            {
                ISOAndOtherIndustries,
                Airlines,
                AirlinesAndOtherIndustries,
                TravelsAndEntertainmentAmericanExpressOrFoodClubOrCarteBlanche,
                BankingAndFinanceVISA,
                BankingAndFinanceMasterCard,
                BankingAndMerchandising,
                Petroleum,
                TelecommunicationsAndOtherIndustries,
                NationalAssignment,
            };
        }
        private static bool VerifyCreditCard(string creditCardNumber)
        {
            int[] digits = creditCardNumber.Select(a => int.Parse(a.ToString())).ToArray();
            int sum = 0;
            Console.Write("Credit/Debit Card Type:");
            //Get the MII: Major Industry Identifier of the card
            Console.WriteLine(creditCardNumber.StartsWith(((int)CreditCardVerification.MajorIndustryIdentifier.ISOAndOtherIndustries).ToString()) ? "ISO"
                : creditCardNumber.StartsWith(((int)CreditCardVerification.MajorIndustryIdentifier.Airlines).ToString()) ? "Airlines"
                : creditCardNumber.StartsWith(((int)CreditCardVerification.MajorIndustryIdentifier.AirlinesAndOtherIndustries).ToString()) ? "Airlines and other industries"
                : creditCardNumber.StartsWith(((int)CreditCardVerification.MajorIndustryIdentifier.TravelsAndEntertainmentAmericanExpressOrFoodClubOrCarteBlanche).ToString()) ? "Travels and entertainment American Express or Food Club or Carte Blanche"
                : creditCardNumber.StartsWith(((int)CreditCardVerification.MajorIndustryIdentifier.BankingAndFinanceVISA).ToString()) ? "VISA"
                : creditCardNumber.StartsWith(((int)CreditCardVerification.MajorIndustryIdentifier.BankingAndFinanceMasterCard).ToString()) ? "Master Card"
                : creditCardNumber.StartsWith(((int)CreditCardVerification.MajorIndustryIdentifier.BankingAndMerchandising).ToString()) ? "Banking and Merchandising"
                : creditCardNumber.StartsWith(((int)CreditCardVerification.MajorIndustryIdentifier.Petroleum).ToString()) ? "Petroleum"
                : creditCardNumber.StartsWith(((int)CreditCardVerification.MajorIndustryIdentifier.TelecommunicationsAndOtherIndustries).ToString()) ? "Telecommunications and Other Industries"
                : creditCardNumber.StartsWith(((int)CreditCardVerification.MajorIndustryIdentifier.NationalAssignment).ToString()) ? "National Assignment" : "Invalid Card"
                );
            //Get the IIN: Issuer Identifier Number

            //Implementation of Luhn Algorithm
            for (int i = 0; i < creditCardNumber.Length; i++)
            {
                if (i % 2 == 0)
                {
                    int num = digits[i] * 2;
                    if (num >= 10)
                    {
                        sum += num % 10;
                        num /= 10;
                        sum += num;
                    }
                    else
                    {
                        sum += num;
                    }
                }
                else
                {
                    sum += digits[i];
                }
            }
            if (sum % 10 == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

No comments: