Tag Archive for ‘mac’

伪造?还是检测信用卡 cocoa, Objc实现

伪造? 还是去检测一张信用卡? 看完这个图, 相信很容易了解其中的数学算法.

原始链接: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,我们能够从图片转换成数字。其实一个有意思的小软件就成了. 希望我们能运用生活中的小科技, 来改变我们的生活吧.

 

 

原来苹果不会那么傻阿

话说apple 09.09.09的发布会 Rock n’ Roll,看来苹果选的日子也不错阿, 殊不知地球这边就一个北京就有近20000对情侣喜结连理. 发布会之前, 我又一个想法, 当3代touch配上3GS的处理器和内存, 还有摄像头和mic, 我就入个touch+any andorid手机来代替我的iphone. 哈哈, 可惜apple不会这么傻, 最起码没我这么梦幻. 这不是技术问题吧? Maybe,之前听说了touch有技术问题没有解决. 但我觉得, 这就是apple的经营理念, apple不傻, 真的出了个那么牛逼的touch, 除了打电话什么都和iphone一样了而价格又第很多, 人们确实都会像我一样 touch+any phone = iphone的. apple就是想告诉我们,我们的产品是没有完美的, 如果想完美,那价格也一定不是完美的, touch+phone ≠ 3GS. 或许, touch+nano+phone = 3GS,但是价格也是划等号的, 为什么我们不选又小又cool 的 3GS.

经过上面的分析, apple其实不会那么傻的, 他每次做产品, 定价和功能总能对应, 并且让你难以选择, 功能没有强大多少,但是你需要, 价格提高的幅度也让你难以抉择. 哈哈,到现在你就中了apple的心里陷阱了, 现在你明白了, 为什么果粉,总是有很多apple的设备, 但是这些东西从大体上看功能都差不多, 不得不说这是apple对发烧友的一种心里攻势. 总是让人又爱又恨.

等? 你错了, apple对等待他产品的人,会有更”严厉的惩罚”, 当你等到了 带设想头的touch的时候, iphone 3GS已经降价了或者出了个iphone 3G AS(Advanced Slim), 你到时候觉得疯了, 然后不顾一切的买了最新的 iphone 3G AS. 你的腰包被掏空, apple一样不少赚.

哈哈, 没事干分析自己的心情, 可能大多数人不会这样想,但是我见过的果粉,基本都是比我疯狂的.最后重申,只有一件apple的产品的绝对不是果粉, 因为如果你买了一件apple的东西,你会发现, 另一件产品和你有的这件原来配合的这么好,然后你就成了果粉,你不得不说apple东西好(确实也不错),要不你不是扇自己脸么.至于那些没有买第2件的, 你只是把iphone当成手机, touch当成mp4, nano当成mp3, mac当成pc了.

结束, 不能再说了,再说又开始给apple做广告了…….

About

You are currently browsing the Cyher.NET weblog archives for 'mac' tag.