信用卡卡号验证算法

How to check digits in credit card validation

文章来源:http://platon.sk/article.php?38

 

Author: Hal Stiles | Section: Dev Resources | Date: 2005-06-25

This document outlines procedures and algorithms for Verifying the accuracy and validity of credit card numbers. Most credit card numbers are encoded with a "Check Digit". A check digit is a digit added to a number (either at the end or the beginning) that validates the authenticity of the number.

A simple algorithm is applied to the other digits of the number which yields the check digit. By running the algorithm, and comparing the check digit you get from the algorithm with the check digit encoded with the credit card number, you can verify that you have correctly read all of the digits and that they make a valid combination.

Possible uses for this information:

  • When a user has keyed in a credit card number (or scanned it) and you want to validate it before sending it our for debit authorization.
  • When issuing cards, say an affinity card, you might want to add a check digit using the MOD 10 method.

Prefix, Length, and Check Digit Criteria

Here is a table outlining the major credit cards that you might want to validate.

CARD TYPEPrefixLengthCheck digit algorithm
MASTERCARD51-5516mod 10
VISA413, 16mod 10
AMEX34 3715mod 10
Diners Club / Carte Blanche300-305 36 3814mod 10
Discover601116mod 10
enRoute2014 214915any
JCB316mod 10
JCB2131 180015mod 10

LUHN Formula (Mod 10) for Validation of Primary Account Number

The following steps are required to validate the primary account number:

Step 1: Double the value of alternate digits of the primary account number beginning with the second digit from the right (the first right--hand digit is the check digit.)

Step 2: Add the individual digits comprising the products obtained in Step 1 to each of the unaffected digits in the original number.

Step 3: The total obtained in Step 2 must be a number ending in zero (30, 40, 50, etc.) for the account number to be validated.

For example, to validate the primary account number 49927398716:

Step 1:

        4 9 9 2 7 3 9 8 7 1 6
x2 x2 x2 x2 x2
------------------------------
18 4 6 16 2

Step 2:

4 +(1+8)+ 9 + (4) + 7 + (6) + 9 +(1+6) + 7 + (2) + 6

Step 3: Sum = 70 => Card number is validated

Note: Card is valid because the 70/10 yields no remainder.

Resources

The great folks at ICVERIFY are the original source of this data, I only formatted it in HTML.

If you are in the market, I wrote a set of FoxPro modules for Windows/Dos that interface nicely with ICVERIFY in a multi-user LAN setup. You just set up ICVERIFY on a single station, and all stations on the LAN can authorize credit cards with a single FOXBASE function call. Of course, you have to license ICVERIFY by the node, but it is very reasonable. I also wrote a couple of simple functions to perform pre-authorization, card screening, etc.

Here is a Microsoft Excel worksheet that will validate a number for you.

Horace Vallas made a NeoWebScript (Tcl really) procedure that implements it. Check it out at http://nws.sourceforge.net/

Because I get at least a letter a week regarding this routine, here are some additional helpful notes:

Make sure that you:

  1. Have started with the rightmost digit (including the check digit) (figure odd and even based upon the rightmost digit being odd, regardless of the length of the Credit Card.) ALWAYS work right to left.
  2. The check digit counts as digit #1 (assuming that the rightmost digit is the check digit) and is not doubled.
  3. Double every second digit (starting with digit # 2 from the right).
  4. Remember that when you double a number over 4, (6 for example) you don't add the result to your total, but rather the sum of the digits of the result (in the above example 6*2=12 so you would add 1+2 to your total (not 12).
  5. Always include the Visa or M/C/ prefix.
Article was originally released on the Hal Stiles webpage server and in the Platon.SK content management system it is published with author and host administrator agreeement.

 

常用信用卡卡号规则

Issuer Identifier                           Card Number                                              Length
Diner's Club                                300xxx-305xxx, 3095xx,36xxxx, 38xxxx        14
American Express                       34xxxx, 37xxxx                                              15
VISA                                           4xxxxx                                                            13, 16
MasterCard                                51xxxx-55xxxx                                                16
JCB                                            3528xx-358xxx                                                16
Discover                                     6011xx                                                            16
银联                                           622126-622925                                               16
信用卡号验证基本算法:
1、偶数位卡号奇数位上数字*2,奇数位卡号偶数位上数字*2。
2、大于10的位数减9。
3、全部数字加起来。
4、结果不是10的倍数的卡号非法。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值