c语言编译测有效信用卡号,Luhn algorithm(附信用卡校验算法C语言实现)

From Wikipedia, the free encyclopedia

The

Luhn algorithm or

Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in US and Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.

The algorithm is in the public domain and is in wide use today. It is specified in ISO/IEC 7812-1[1]. It is not intended to be a cryptographically secure hash function; it was designed to protect against accidental errors, not malicious attacks. Most credit cards and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from collections of random digits.

Contents

1 Strengths and weaknesses

2 Informal explanation

3 Mod 10+5 Variant

4 Implementation of standard Mod 10

5 Other implementations

6 References

Strengths and weaknesses

The Luhn algorithm will detect any single-digit error, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the two-digit sequence 09 to 90 (or vice versa). It will detect 7 of the 10 possible twin errors (it will not detect 22 ↔ 55, 33 ↔ 66 or 44 ↔ 77).

Other, more complex check-digit algorithms (such as the Verhoeff algorithm) can detect more transcription errors. The Luhn mod N algorithm is an extension that supports non-numerical strings.

Because the algorithm operates on the digits in a right-to-left manner and zero digits affect the result only if they cause shift in position, zero-padding the beginning of a string of numbers does not affect the calculation. Therefore, systems that normalize to a specific number of digits by converting 1234 to 00001234 (for instance) can perform Luhn validation before or after the normalization and achieve the same result.

The algorithm appeared in a US Patent for a hand-held, mechanical device for computing the checksum. It was therefore required to be rather simple. The device took the mod 10 sum by mechanical means. The substitution digits, that is, the results of the double and reduce procedure, were not produced mechanically. Rather, the digits were marked in their permuted order on the body of the machine.

Informal explanation

The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This account number must pass the following test:

Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.

Sum the digits of the products together with the undoubled digits from the original number.

If the total ends in 0 (put another way, if the total modulo 10 is equal to 0), then the number is valid according to the Luhn formula; else it is not valid.

As an illustration, if the account number is 49927398716, it will be validated as follows:

Double every second digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18

Sum all the individual digits (digits in parentheses are the products from Step 1): 6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70

Take the sum modulo 10: 70 mod 10 = 0; the account number is valid.

Mod 10+5 Variant

Some credit cards use the "Mod 10 plus 5" variant to extend the space of valid card numbers.[citation needed] In this variant, if the sum ends in 0 or 5, the number is considered valid.

Implementation of standard Mod 10

Python variant:

def is_mod10(cc):

dub, tot = 0, 0

for i in range(len(cc) - 1, -1, -1):

for c in str((dub + 1) * int(cc[i])):

tot += int(c)

dub = (dub + 1) % 2

return (tot % 10) == 0

Java variant:

public static boolean isValidCC(String num) {

final int[][] sumTable = {{0,1,2,3,4,5,6,7,8,9},{0,2,4,6,8,1,3,5,7,9}};

int sum = 0, flip = 0;

for (int i = num.length() - 1; i >= 0; i--, flip++)

sum += sumTable[flip & 0x1][num.charAt(i) - '0'];

return sum % 10 == 0;

}

c variant:

int LuhnMod10 ( char * cardNumber , int size )

{

static int table [ 10 ] = { { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } , { 0 , 2 , 4 , 6 , 8 , 1 , 3 , 5 , 7 , 9 } } ;

for ( int i = size - 1 , odd = 0 , sum = 0 ; i >= 0 ; i -- )

if ( isdigit ( cardNumber [ i ] ) )

sum += table [ ( odd = 1 - odd ) ] [ cardNumber [i] - '0' ] ;

sum %= 10 ;

return ( sum ? 10 - sum : 0 ) ; /* return the check digit */

}

这里有一个表格,概述了主要的信用卡,您可能要验证。

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

卡类型CARD TYPE

前缀Prefix

长度Length

校验码算法Check digit algorithm

万事达卡MASTERCARD

51-5551-55

1616

10mod 10

签证VISA

44

13,1613, 16

10mod 10

美国证券交易所AMEX

3434

37 37

1515

10mod 10

大来卡/Diners Club/全权委托Carte Blanche

300-305300-305

36 36

38 38

1414

10mod 10

探索Discover

60116011

1616

10mod 10

途中enRoute

2014年2014

2149 2149

1515

任何any

联合协调JCB

33

1616

10mod 10

联合协调JCB

21312131

1800 1800

1515

10mod 10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
详细介绍了AES-CMAC的算法的原理与实现C语言写的样例程序。 以下是原文的introduction: The National Institute of Standards and Technology (NIST) has recently specified the Cipher-based Message Authentication Code(CMAC). CMAC [NIST-CMAC] is a keyed hash function that is based on a symmetric key block cipher, such as the Advanced Encryption Standard [NIST-AES]. CMAC is equivalent to the One-Key CBC MAC1 (OMAC1) submitted by Iwata and Kurosawa [OMAC1a, OMAC1b]. OMAC1 is an improvement of the eXtended Cipher Block Chaining mode (XCBC) submitted by Black and Rogaway [XCBCa, XCBCb], which itself is an improvement of the basic Cipher Block Chaining-Message Authentication Code (CBC-MAC). XCBC efficiently addresses the security deficiencies of CBC-MAC, and OMAC1 efficiently reduces the key size of XCBC. AES-CMAC provides stronger assurance of data integrity than a checksum or an error-detecting code. The verification of a checksum or an error-detecting code detects only accidental modifications of the data, while CMAC is designed to detect intentional, unauthorized modifications of the data, as well as accidental modifications. AES-CMAC achieves a security goal similar to that of HMAC [RFC-HMAC]. Since AES-CMAC is based on a symmetric key block cipher, AES, and HMAC is based on a hash function, such as SHA-1, AES-CMAC is appropriate for information systems in which AES is more readily available than a hash function. This memo specifies the authentication algorithm based on CMAC with AES-128. This new authentication algorithm is named AES-CMAC.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值