伪造? 还是去检测一张信用卡? 看完这个图, 相信很容易了解其中的数学算法.
原始链接:http://www.geekv5.com/archives/2859
英文原始链接:http://www.mint.com/blog/trends/credit-card-code-01202011/
好了其实这个小知识还是很有趣的, 作为向geek前进的我们, 怎么能不知道这样的小trick.
看见这个,会想起什么?我们的思维真的会从伪造信用卡这个keywrod延伸到,卡片的生产,怎么把磁条信息录入,怎么去设这个陷阱让人踩了还不知道?
至少我不是的。。我第一个想到的是如果我有一个iphone,我的iphone上有一个软件通过摄像头,对着一张信用卡来信用卡真伪识别。So cool? 其实这样的思维常常出现在每个人的脑海中,那些so cool的创意程序可能就在这瞬间的头脑风暴产生了。好吧,其实本不应该说这么多的。其实我是想鼓励自己,纸上得来终觉浅,绝知此事要躬行,just do it.
其实这个信用卡真伪的识别是基于,一个叫做Luhn 的算法 详细内容请移步 http://en.wikipedia.org/wiki/Luhn_algorithm
我对这个算法的描述就是:
- 从右倒左, 偶数位 * 2, 如果偶数大于10, 则个位+(十位/10)
- 把奇数位和刚刚得出的偶数位相加得出 sum
- sum如果能够mod 10 = 0 那就是正确的
这里给出C的code, wikipedia里面有java和pyhon的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | The Luhn Mod-10 Method (ISO 2894/ANSI 4.13) involves a check digit in the one's position. The check digit is calculated starting at the right with the digit immediately preceding the check digit (ten's digit) and moving toward the left, doubling every other digit. If a doubled digit is greater than nine, the two digits are added to together to obtain a single-digit result. The sum of all the resulting digits (including those skipped) is then taken modulo with 10, to obtain the check digit. Below, is untested code. /************************************************************************ * LuhnMod10 - self-checking scheme for validating card account numbers * according to ISO 2894/ANSI 4.13. * * Ex: cardNumber = "795102879015546" * strlen(cardNumber) == 15 * LuhnMod10(cardNumber, 14) == 6 == cardNumber[14] * ************************************************************************/ int LuhnMod10(char* cardNumber, int size) { static int table[2][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 */ } |
站在巨人的肩膀是不是有一种很强大的感觉。好吧,我们既然要把要在iphone上实现,那首先需要一个Objective-C的代码. here is it ,从C代码修改过来并不是很难。
CheckCreditCard.h
1 2 3 4 5 6 7 8 9 | #import
@interface CheckCreditCard : NSObject {
IBOutlet NSTextField *ccNum;
IBOutlet NSTextField *ccTrue;
}
- (IBAction)checkIt:(id)sender;
@end |
CheckCreditCard.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | // CheckCreditCard.m
// Verify Credit Card
//
// Created by cyher on 11-3-8.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "CheckCreditCard.h"
@implementation CheckCreditCard
- (id)init
{
[super init];
NSLog(@"Init!");
return self;
}
- (IBAction)checkIt:(id)sender
{
int sumTable[10][10] = {{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;
NSString *string = [ccNum stringValue];
NSLog(@"ccNum = %@", string);
if ([string length] == 0)
return;
for (int i = [string length] - 1; i >= 0; i--)
sum +=
sumTable[flip++ & 0x1][[string characterAtIndex:i] - '0'];
NSLog(@"sum = %d", sum);
if (0 == (sum % 10)) {
[ccTrue setStringValue:[NSString stringWithFormat
:@"This is a real Credit Card!",
[string length]]];
} else {
[ccTrue setStringValue:[NSString stringWithFormat
:@"This is fake Credit Card!",
[string length]]];
}
return;
}
@end |
来我们看看在mac上的效果吧:
怎么样很简单吧, 如果我们能够调用goggle(google的图形识别程序,基于云计算)的API,我们能够从图片转换成数字。其实一个有意思的小软件就成了. 希望我们能运用生活中的小科技, 来改变我们的生活吧.


近期评论