Gerador De Cartão De Crédito Gerador de números de cartão de crédito (Master, Visa, Amex)
Autor: Sama Assuero | Publicado: 2012-05-04T08:55:00-07:00

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.Vector;
/**
&nbsp;* See the license below. Obviously, this is not a Javascript credit card number
&nbsp;* generator. However, The following class is a port of a Javascript credit card
&nbsp;* number generator.
&nbsp;* 
&nbsp;* @author robweber
&nbsp;* 
&nbsp;*/
public class RandomCreditCardNumberGenerator {
&nbsp; &nbsp; /*
&nbsp; &nbsp; &nbsp;* Javascript credit card number generator Copyright (C) 2006 Graham King
&nbsp; &nbsp; &nbsp;* graham@darkcoding.net
&nbsp; &nbsp; &nbsp;* 
&nbsp; &nbsp; &nbsp;* This program is free software; you can redistribute it and/or modify it
&nbsp; &nbsp; &nbsp;* under the terms of the GNU General Public License as published by the
&nbsp; &nbsp; &nbsp;* Free Software Foundation; either version 2 of the License, or (at your
&nbsp; &nbsp; &nbsp;* option) any later version.
&nbsp; &nbsp; &nbsp;* 
&nbsp; &nbsp; &nbsp;* This program is distributed in the hope that it will be useful, but
&nbsp; &nbsp; &nbsp;* WITHOUT ANY WARRANTY; without even the implied warranty of
&nbsp; &nbsp; &nbsp;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
&nbsp; &nbsp; &nbsp;* Public License for more details.
&nbsp; &nbsp; &nbsp;* 
&nbsp; &nbsp; &nbsp;* You should have received a copy of the GNU General Public License along
&nbsp; &nbsp; &nbsp;* with this program; if not, write to the Free Software Foundation, Inc.,
&nbsp; &nbsp; &nbsp;* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
&nbsp; &nbsp; &nbsp;* 
&nbsp; &nbsp; &nbsp;* www.darkcoding.net
&nbsp; &nbsp; &nbsp;*/

&nbsp; &nbsp; public static final String[] VISA_PREFIX_LIST = new String[] { "4539",
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "4556", "4916", "4532", "4929", "40240071", "4485", "4716", "4" };

&nbsp; &nbsp; public static final String[] MASTERCARD_PREFIX_LIST = new String[] { "51",
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "52", "53", "54", "55" };

&nbsp; &nbsp; public static final String[] AMEX_PREFIX_LIST = new String[] { "34", "37" };

&nbsp; &nbsp; public static final String[] DISCOVER_PREFIX_LIST = new String[] { "6011" };

&nbsp; &nbsp; public static final String[] DINERS_PREFIX_LIST = new String[] { "300",
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "301", "302", "303", "36", "38" };

&nbsp; &nbsp; public static final String[] ENROUTE_PREFIX_LIST = new String[] { "2014",
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2149" };

&nbsp; &nbsp; public static final String[] JCB_16_PREFIX_LIST = new String[] { "3088",
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "3096", "3112", "3158", "3337", "3528" };

&nbsp; &nbsp; public static final String[] JCB_15_PREFIX_LIST = new String[] { "2100",
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "1800" };

&nbsp; &nbsp; public static final String[] VOYAGER_PREFIX_LIST = new String[] { "8699" };
&nbsp; &nbsp; 
&nbsp; &nbsp; public static Label LABEL;
&nbsp; &nbsp; 
&nbsp; &nbsp; public static enum Label{VISA, MASTER, AMEX, DINERS;
&nbsp; &nbsp; &nbsp; &nbsp; public static Label getLabel(final String label) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(label.toUpperCase().equals("VISA")) return VISA;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(label.toUpperCase().equals("MASTER")) return MASTER;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(label.toUpperCase().equals("AMEX")) return AMEX;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(label.toUpperCase().equals("DINERS")) return DINERS;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Unknown Label: ["+ label +"]");
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; };

&nbsp; &nbsp; static String strrev(String str) {
&nbsp; &nbsp; &nbsp; &nbsp; if (str == null)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "";
&nbsp; &nbsp; &nbsp; &nbsp; String revstr = "";
&nbsp; &nbsp; &nbsp; &nbsp; for (int i = str.length() - 1; i &gt;= 0; i--) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; revstr += str.charAt(i);
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; return revstr;
&nbsp; &nbsp; }

&nbsp; &nbsp; /*
&nbsp; &nbsp; &nbsp;* 'prefix' is the start of the CC number as a string, any number of digits.
&nbsp; &nbsp; &nbsp;* 'length' is the length of the CC number to generate. Typically 13 or 16
&nbsp; &nbsp; &nbsp;*/
&nbsp; &nbsp; static String completed_number(String prefix, int length) {

&nbsp; &nbsp; &nbsp; &nbsp; String ccnumber = prefix;

&nbsp; &nbsp; &nbsp; &nbsp; // generate digits

&nbsp; &nbsp; &nbsp; &nbsp; while (ccnumber.length() &lt; (length - 1)) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ccnumber += new Double(Math.floor(Math.random() * 10)).intValue();
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; // reverse number and convert to int

&nbsp; &nbsp; &nbsp; &nbsp; String reversedCCnumberString = strrev(ccnumber);

&nbsp; &nbsp; &nbsp; &nbsp; List&lt;Integer&gt; reversedCCnumberList = new Vector&lt;Integer&gt;();
&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; reversedCCnumberString.length(); i++) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reversedCCnumberList.add(new Integer(String
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .valueOf(reversedCCnumberString.charAt(i))));
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; // calculate sum

&nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;
&nbsp; &nbsp; &nbsp; &nbsp; int pos = 0;

&nbsp; &nbsp; &nbsp; &nbsp; Integer[] reversedCCnumber = reversedCCnumberList
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toArray(new Integer[reversedCCnumberList.size()]);
&nbsp; &nbsp; &nbsp; &nbsp; while (pos &lt; length - 1) {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int odd = reversedCCnumber[pos] * 2;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (odd &gt; 9) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; odd -= 9;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += odd;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (pos != (length - 2)) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += reversedCCnumber[pos + 1];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pos += 2;
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; // calculate check digit

&nbsp; &nbsp; &nbsp; &nbsp; int checkdigit = new Double(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((Math.floor(sum / 10) + 1) * 10 - sum) % 10).intValue();
&nbsp; &nbsp; &nbsp; &nbsp; ccnumber += checkdigit;

&nbsp; &nbsp; &nbsp; &nbsp; return ccnumber;

&nbsp; &nbsp; }

&nbsp; &nbsp; public static String[] credit_card_number(String[] prefixList, int length,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int howMany) {

&nbsp; &nbsp; &nbsp; &nbsp; Stack&lt;String&gt; result = new Stack&lt;String&gt;();
&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; howMany; i++) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int randomArrayIndex = (int) Math.floor(Math.random()
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * prefixList.length);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String ccnumber = prefixList[randomArrayIndex];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.push(completed_number(ccnumber, length));
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; return result.toArray(new String[result.size()]);
&nbsp; &nbsp; }

&nbsp; &nbsp; public static String[] generateCardNumbers(int howMany) {
&nbsp; &nbsp; &nbsp; &nbsp; return credit_card_number(getPrefixList(), 16, howMany);
&nbsp; &nbsp; }

&nbsp; &nbsp; public static String generateCardNumber() {
&nbsp; &nbsp; &nbsp; &nbsp; return credit_card_number(getPrefixList(), 16, 1)[0];
&nbsp; &nbsp; }

&nbsp; &nbsp; public static String[] getPrefixList(){
&nbsp; &nbsp; &nbsp; &nbsp; if(Label.VISA.equals(LABEL)) return VISA_PREFIX_LIST;
&nbsp; &nbsp; &nbsp; &nbsp; if(Label.MASTER.equals(LABEL)) return MASTERCARD_PREFIX_LIST;
&nbsp; &nbsp; &nbsp; &nbsp; if(Label.AMEX.equals(LABEL)) return AMEX_PREFIX_LIST;
&nbsp; &nbsp; &nbsp; &nbsp; if(Label.DINERS.equals(LABEL)) return DINERS_PREFIX_LIST;
&nbsp; &nbsp; &nbsp; &nbsp; return null; 
&nbsp; &nbsp; }
&nbsp; &nbsp; public static boolean isValidCreditCardNumber(String creditCardNumber) {
&nbsp; &nbsp; &nbsp; &nbsp; boolean isValid = false;

&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String reversedNumber = new StringBuffer(creditCardNumber)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .reverse().toString();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int mod10Count = 0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; reversedNumber.length(); i++) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int augend = Integer.parseInt(String.valueOf(reversedNumber
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .charAt(i)));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (((i + 1) % 2) == 0) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String productString = String.valueOf(augend * 2);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; augend = 0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j &lt; productString.length(); j++) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; augend += Integer.parseInt(String.valueOf(productString
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .charAt(j)));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mod10Count += augend;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((mod10Count % 10) == 0) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isValid = true;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; } catch (NumberFormatException e) {
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; return isValid;
&nbsp; &nbsp; }

&nbsp; &nbsp; public static void main(String[] args) {
&nbsp; &nbsp; &nbsp; &nbsp; int numberOfCards = 0;
&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numberOfCards = Integer.parseInt(args[0]);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LABEL = Label.getLabel(args[1]);
&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Usage error: [NUMBER_OF_CARDS] [VISA|MASTER|AMEX|DINERS]");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println("Examples:");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println("10 VISA");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println("200 MASTER");
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; String[] creditcardnumbers = generateCardNumbers(numberOfCards);
&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; creditcardnumbers.length; i++) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(creditcardnumbers[i]
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + ":"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + (isValidCreditCardNumber(creditcardnumbers[i]) ? "valid"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : "invalid"));
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
}