Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Credit card algorithm checking in C / MFC C?

Posted on 2000-03-28
11
Medium Priority
?
275 Views
Last Modified: 2011-09-20
hi guys,

can anybody share the credit card algorithm checking function written in C / MFC C?

Thanks !!!
0
Comment
Question by:Haho
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 2664079
See:

http://www.binary.net/dturley/js/ccver.html

It's in Javascript but can be translated to C pretty easily.
0
 

Expert Comment

by:wardkd
ID: 2664109
This is dependant on the type of card, but the Standard ISO is as follows:                        
      19      18      17      16      15      14      13      12      11      10      9      8      7      6      5      4      3      2      1            
                        5      4      0      1      5      2      1      9      0      3      0      7      8      3      1      8      OK      
Weights      1      2      1      2      1      2      1      2      1      2      1      2      1      2      1      2      1      2      1            

Products      0      0      0      10      4      0      1      10      2      2      9      0      3      0      7      16      3      2      8            
                                                                                                                              
Digits      0      0      0      1      0      0      0      1      0      0      0      0      0      0      0      1      0      0      0      50      Sum of digits
      0      0      0      0      4      0      1      0      2      2      9      0      3      0      7      6      3      2      8            
                                                                                                                        10      Modulus
0
 

Expert Comment

by:wardkd
ID: 2664138
Please ignore my previous comment. This is pasted from a spreadsheet, which hasn't worked too well ;)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Accepted Solution

by:
imladris earned 400 total points
ID: 2665437
That JavaScript is an awful piece of code. I hope it is merely the constraints of that pseudo language. Here is what we use. The argument is a 19 character string representing the credit card number, right justified:

ccnvalid(ccn)
char *ccn;
{
      int i,cs,j,d;
      
      d=cs=0;
      for(i=18; i>=0;  --i)
      {      if(ccn[i]!=' ')
            {      j=(ccn[i]-'0')*(d+1);
                  cs+=j%10;
                  cs+=j/10;
                  d=!d;
            }
      }
      return(cs%10==0);
}
0
 
LVL 16

Expert Comment

by:imladris
ID: 2670136
Is there any clarification you need before grading the answer?
0
 
LVL 32

Expert Comment

by:jhance
ID: 2670431
imladris,

There seem to be problems with your code:

1) It fails if the number is not formatted like: XXXX XXXX XXXX XXXX

2) It fails for valid 13 digit Visa card numbers: XXXX XXX XXX XXX

3) It fails with AMEX #s which are all 15-digit.

4) I didn't check it with Discover #s but I think it fails there as well.
0
 
LVL 16

Expert Comment

by:imladris
ID: 2670828
jhance,

This is production code, running in thousands of sites throughout North America.
It may be that I have not explained the parameter in enough detail.

In fact, I would expect it to fail if there are embedded spaces in the number (notice that it stops processing when it encounters a blank). If the number is shorter than 19 characters, it should have leading blanks.

I'm not clear on why you are quoting the length as being a problem in case 2 and 3, there is no specific reference to length in the algorithm or the code. It simply processes digits, right to left, until it encounters a blank.

If you could provide a specific failure case, perhaps we could discuss it in detail.
0
 
LVL 32

Expert Comment

by:jhance
ID: 2670873
I hate to be critical but if this is production code, I think it's a miracle it works at all.

For example consider:

for(i=18; i>=0;  --i)
{ if(ccn[i]!=' ')


What happens if ccn is < 18 chars long?

See my point? Any old garbage in the string after the terminating NULL will be considered.  In my case, I fed it a 16 digit CCN of the form:

1234123412341234

Well ccn[18] comes 3 chars to the _right_ of the last "4".  I don't know what's in the space after the 4.  It might be 0 or it might be junk...

Now perhaps in the context of the application where you're running this code, you are massaging the CCN BEFORE it gets to this function.  In that case, things might be better but it's dangerous code as posted.

0
 
LVL 16

Expert Comment

by:imladris
ID: 2671057
You're correct in that in our code the parameter is guaranteed to be 19 characters before it gets to the function. You'll note that I originally specified that the parameter is a 19 character string. If one would want to be more general than that, you could provide one level of generality up by providing a sprintf in it, along the lines of:

ccnvalid(cca)
char *cca;
{
int i,cs,j,d;
char ccn[20];

sprintf(ccn,"%19s",cca);
d=cs=0;
for(i=18; i>=0;  --i)
{ if(ccn[i]!=' ')
{ j=(ccn[i]-'0')*(d+1);
cs+=j%10;
cs+=j/10;
d=!d;
}
}
return(cs%10==0);
}

Does that cover your concerns?
0
 
LVL 1

Author Comment

by:Haho
ID: 2679072
guys,

does it work for XXXXXXXXXXXXXXXX ?

Thanks!
0
 
LVL 16

Expert Comment

by:imladris
ID: 2680486
Absolutely. In fact, the function I provided assumes there are no blanks embedded in the credit card number string.
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Businesses who process credit card payments have to adhere to PCI Compliance standards. Here’s why that’s important.
The Internet has made sending and receiving information online a breeze. But there is also the threat of unauthorized viewing, data tampering, and phoney messages. Surprisingly, a lot of business owners do not fully understand how to use security t…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question