Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the verification rule of bank card based on Luhn algorithm

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what are the bank card verification rules based on Luhn algorithm". In daily operation, I believe many people have doubts about what the bank card verification rules based on Luhn algorithm are. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubts about "what are the bank card verification rules based on Luhn algorithm". Next, please follow the editor to study!

Case study:

When you enter your credit card number, are you worried about the loss caused by the wrong input? In fact, you don't have to worry so much, because not all random credit card numbers are legal, and it must be verified by the Luhn algorithm.

The process of the verification:

1. Start with the last digit of the card number and add the odd digits (1, 3, 5, etc.) in reverse.

2. Starting with the last digit of the card number, inversely multiply the even digit by 2 (if the product is two digits, subtract it by 9), and then sum.

3. add the sum of odd digits to the sum of even digits, and the result should be divisible by 10.

For example, the card number is 5432123456788881

Then the odd and even digits (marked in red) are distributed: 5432123456788881

Odd digit sum = 35

The result of multiplying even digits by 2 (some subtract 9): 16 2 6 1 5 7 7, summation = 35.

Finally, 35-35-70 can be divisible by 10, and the check is determined to pass.

Import java.util.Scanner;// credit card number verification algorithm public class Luhn {public static void main (String [] args) {System.out.println ("Please input your credit card number:"); Scanner input = new Scanner (System.in); int sumOdd = 0; int sumEven = 0; String number = input.next (); int length = number.length (); int [] wei = new int [length]; for (int I = 0; I

< number.length(); i++) { wei[i] = Integer.parseInt(number.substring(length - i - 1, length - i));// 从最末一位开始提取,每一位上的数值 System.out.println("第" + i + "位数字是:" + wei[i]); } for (int i = 0; i < length / 2; i++) { sumOdd += wei[2 * i]; if ((wei[2 * i + 1] * 2) >

9) wei [2 * I + 1] = wei [2 * I + 1] * 2-9; else wei [2 * I + 1] * = 2; sumEven + = wei [2 * I + 1];} System.out.println ("odd sum is:" + sumOdd); System.out.println ("even sum is:" + sumEven); if ((sumOdd + sumEven)% 10 = 0) System.out.println ("Recept.") Else System.out.println ("Can not recept.");}} run result:

Please input your credit card number:

5432123456788881

The zero digit is: 1

The first digit is: 8

The second digit is: 8

The third digit is: 8

The fourth digit is: 8

The fifth digit is: 7

The sixth digit is: 6

The seventh digit is: 5

The eighth digit is: 4

The ninth digit is: 3

The 10th digit is: 2

The 11th digit is: 1

The 12th digit is: 2

The 13th digit is: 3

The 14th digit is: 4

The 15th digit is: 5

The sum of odd digits is 35

The sum of even digits is 35

Recept.

Bank card verification rules (Luhn algorithm)

Luhn Test Digital algorithm (Luhn Check Digit Algorithm), also known as modulus 10 formula, is a simple algorithm used to verify the validity of bank cards and credit card numbers. Credit cards issued by all large credit card companies, including Express, Passport, MasterCard, Discover and diners Club, work. This algorithm was originally developed by a group of mathematicians in the 1960s, and now Luhn tests that the digital algorithm belongs to the public and can be used by anyone.

Algorithm: double each odd number and make it a single number, if necessary by subtracting 9 and adding these values to each even number. If this card is to be valid, the result must be a multiple of 10.

For example, the card number in the image above is 3759 8765 4321 (15 digits). Starting from the highest bit, all the odd digits are added, and the even number is multiplied by 2 (if the even number is multiplied by 2, the two digits are added if it is greater than 10). Add these odd and even numbers together to get 57.

(573.3) = = 0 if this card satisfies divisibility by 10, the significant bit must be 3.

According to the current records of successful withdrawals (clear_success), the relationship between the number of digits and the number of card numbers is as follows:

Number of digits of card number

20 504 +

Description:

The card number of less than 14 digits is basically a foreign bank or a small bank.

The card number of less than 14 digits is basically a foreign bank or a small bank.

* * 496748 HSBC HSBCHKHHHKH

* * 430259 HSBC HSBCHKHHHKH

* 51878018 Bank of East Asia Limited BEASHKHHXXX

Card numbers with more than 23 digits are mostly card numbers that contain letters or spaces.

62270014 * * 0045 * CHINA CONSTRUCTION BANK * *

601382700 * 9077 * * BANK OF CHINA FOSHAN BRANCH * *

If it is the domestic mainstream banks (China, agriculture, industry, construction, recruitment, communication, etc.) are basically based on 16-digit or 19-digit card number.

Let's take a look at how many cards in clear_success meet the Luhn rules.

Accuracy for 16 dollar individual users: 99.84%

Correct number of card numbers: 3105 incorrect number of card numbers: 14

Card number 45806509007 is NOT valid

Card number 48620375555016 is NOT valid

Card number 54202100231152 is NOT valid

Card number 58890201075786 is NOT valid

Card number 62106200000456 is NOT valid

Card number 62129986037235please * is NOT valid

Card number 62252017026526 is NOT valid

Accuracy for 19 US dollar individual users: 99.96%

Correct number of card numbers: 10574 incorrect number of card numbers: 13 of which 3 refunds occurred

Card number 60138214000567721 is NOT valid

Card number 60138217000662109card * is NOT valid

Card number 60138220005824282 is NOT valid

Card number 60138220006014219 is NOT valid

Card number 60220001386050410 is NOT valid

Card number 62161132000004484 is NOT valid

Card number 62220212082154900 is NOT valid

Card number 62220836020035821 is NOT valid

Card number 63214140980000000 is NOT valid

The above are all successful withdrawal card numbers, and you can see that all of them strictly meet the Luhn algorithm.

Number of digits of card number

15 1

16 149

17 4

18 64

19 502

20 3

Correct number of card numbers: 622

Incorrect number of card numbers: 29 + 1 + 4" 643"

14% of the card violates the Luhn algorithm, that is, this part of the card number can be detected by the Luhn check in advance.

The conclusion is that when we fill in the collection account, we can add a JS script of the Luth algorithm to check that there is a problem with the card number filled in by the user. Of course, the card number filled in by the instant user violates the rule, we still run the user to fill in, but give the corresponding warning content (may fill in incorrectly).

At present, for credit card transactions on the cashier page (Checkout.vm), the JS: isValidCardfunction function contains the Luth verification rule, which must be strictly followed before the card can submit the form.

At this point, the study of "what are the bank card verification rules based on Luhn algorithm" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report