Solved

Credit card algorithm checking in C / MFC C?

Posted on 2000-03-28
11
261 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
 
LVL 16

Accepted Solution

by:
imladris earned 100 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

The aim of this article is to help you solve the error "Cannot insert the value NULL into column 'ShortDescription', table 'albert_store.dbo.Nop_Product'; column does not allow nulls. UPDATE fails." problem and allow you to continue updating your No…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now