[Java] Java随机生成信用卡卡号的代码 →→→→→进入此内容的聊天室

来自 , 2019-04-30, 写在 Java, 查看 111 次.
URL http://www.code666.cn/view/c9f2f917
  1. import java.util.List;
  2. import java.util.Stack;
  3. import java.util.Vector;
  4.  
  5. /**
  6.  * See the license below. Obviously, this is not a Javascript credit card number
  7.  * generator. However, The following class is a port of a Javascript credit card
  8.  * number generator.
  9.  *
  10.  * @author robweber
  11.  *
  12.  */
  13. public class RandomCreditCardNumberGenerator {
  14.         /*
  15.          * Javascript credit card number generator Copyright (C) 2006-2012 Graham King
  16.          *
  17.          * This program is free software; you can redistribute it and/or modify it
  18.          * under the terms of the GNU General Public License as published by the
  19.          * Free Software Foundation; either version 2 of the License, or (at your
  20.          * option) any later version.
  21.          *
  22.          * This program is distributed in the hope that it will be useful, but
  23.          * WITHOUT ANY WARRANTY; without even the implied warranty of
  24.          * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  25.          * Public License for more details.
  26.          *
  27.          * You should have received a copy of the GNU General Public License along
  28.          * with this program; if not, write to the Free Software Foundation, Inc.,
  29.          * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  30.          *
  31.          * www.darkcoding.net
  32.          */
  33.  
  34.         public static final String[] VISA_PREFIX_LIST = new String[] { "4539",
  35.                         "4556", "4916", "4532", "4929", "40240071", "4485", "4716", "4" };
  36.  
  37.         public static final String[] MASTERCARD_PREFIX_LIST = new String[] { "51",
  38.                         "52", "53", "54", "55" };
  39.  
  40.         public static final String[] AMEX_PREFIX_LIST = new String[] { "34", "37" };
  41.  
  42.         public static final String[] DISCOVER_PREFIX_LIST = new String[] { "6011" };
  43.  
  44.         public static final String[] DINERS_PREFIX_LIST = new String[] { "300",
  45.                         "301", "302", "303", "36", "38" };
  46.  
  47.         public static final String[] ENROUTE_PREFIX_LIST = new String[] { "2014",
  48.                         "2149" };
  49.  
  50.         public static final String[] JCB_PREFIX_LIST = new String[] { "35" };
  51.  
  52.         public static final String[] VOYAGER_PREFIX_LIST = new String[] { "8699" };
  53.  
  54.         static String strrev(String str) {
  55.                 if (str == null)
  56.                         return "";
  57.                 String revstr = "";
  58.                 for (int i = str.length() - 1; i >= 0; i--) {
  59.                         revstr += str.charAt(i);
  60.                 }
  61.  
  62.                 return revstr;
  63.         }
  64.  
  65.         /*
  66.          * 'prefix' is the start of the CC number as a string, any number of digits.
  67.          * 'length' is the length of the CC number to generate. Typically 13 or 16
  68.          */
  69.         static String completed_number(String prefix, int length) {
  70.  
  71.                 String ccnumber = prefix;
  72.  
  73.                 // generate digits
  74.  
  75.                 while (ccnumber.length() < (length - 1)) {
  76.                         ccnumber += new Double(Math.floor(Math.random() * 10)).intValue();
  77.                 }
  78.  
  79.                 // reverse number and convert to int
  80.  
  81.                 String reversedCCnumberString = strrev(ccnumber);
  82.  
  83.                 List<Integer> reversedCCnumberList = new Vector<Integer>();
  84.                 for (int i = 0; i < reversedCCnumberString.length(); i++) {
  85.                         reversedCCnumberList.add(new Integer(String
  86.                                         .valueOf(reversedCCnumberString.charAt(i))));
  87.                 }
  88.  
  89.                 // calculate sum
  90.  
  91.                 int sum = 0;
  92.                 int pos = 0;
  93.  
  94.                 Integer[] reversedCCnumber = reversedCCnumberList
  95.                                 .toArray(new Integer[reversedCCnumberList.size()]);
  96.                 while (pos < length - 1) {
  97.  
  98.                         int odd = reversedCCnumber[pos] * 2;
  99.                         if (odd > 9) {
  100.                                 odd -= 9;
  101.                         }
  102.  
  103.                         sum += odd;
  104.  
  105.                         if (pos != (length - 2)) {
  106.                                 sum += reversedCCnumber[pos + 1];
  107.                         }
  108.                         pos += 2;
  109.                 }
  110.  
  111.                 // calculate check digit
  112.  
  113.                 int checkdigit = new Double(
  114.                                 ((Math.floor(sum / 10) + 1) * 10 - sum) % 10).intValue();
  115.                 ccnumber += checkdigit;
  116.  
  117.                 return ccnumber;
  118.  
  119.         }
  120.  
  121.         public static String[] credit_card_number(String[] prefixList, int length,
  122.                         int howMany) {
  123.  
  124.                 Stack<String> result = new Stack<String>();
  125.                 for (int i = 0; i < howMany; i++) {
  126.                         int randomArrayIndex = (int) Math.floor(Math.random()
  127.                                         * prefixList.length);
  128.                         String ccnumber = prefixList[randomArrayIndex];
  129.                         result.push(completed_number(ccnumber, length));
  130.                 }
  131.  
  132.                 return result.toArray(new String[result.size()]);
  133.         }
  134.  
  135.         public static String[] generateMasterCardNumbers(int howMany) {
  136.                 return credit_card_number(MASTERCARD_PREFIX_LIST, 16, howMany);
  137.         }
  138.  
  139.         public static String generateMasterCardNumber() {
  140.                 return credit_card_number(MASTERCARD_PREFIX_LIST, 16, 1)[0];
  141.         }
  142.  
  143.         public static boolean isValidCreditCardNumber(String creditCardNumber) {
  144.                 boolean isValid = false;
  145.  
  146.                 try {
  147.                         String reversedNumber = new StringBuffer(creditCardNumber)
  148.                                         .reverse().toString();
  149.                         int mod10Count = 0;
  150.                         for (int i = 0; i < reversedNumber.length(); i++) {
  151.                                 int augend = Integer.parseInt(String.valueOf(reversedNumber
  152.                                                 .charAt(i)));
  153.                                 if (((i + 1) % 2) == 0) {
  154.                                         String productString = String.valueOf(augend * 2);
  155.                                         augend = 0;
  156.                                         for (int j = 0; j < productString.length(); j++) {
  157.                                                 augend += Integer.parseInt(String.valueOf(productString
  158.                                                                 .charAt(j)));
  159.                                         }
  160.                                 }
  161.  
  162.                                 mod10Count += augend;
  163.                         }
  164.  
  165.                         if ((mod10Count % 10) == 0) {
  166.                                 isValid = true;
  167.                         }
  168.                 } catch (NumberFormatException e) {
  169.                 }
  170.  
  171.                 return isValid;
  172.         }
  173.  
  174.         public static void main(String[] args) {
  175.                 int howMany = 0;
  176.                 try {
  177.                         howMany = Integer.parseInt(args[0]);
  178.                 } catch (Exception e) {
  179.                         System.err
  180.                                         .println("Usage error. You need to supply a numeric argument (ex: 500000)");
  181.                 }
  182.                 String[] creditcardnumbers = generateMasterCardNumbers(howMany);
  183.                 for (int i = 0; i < creditcardnumbers.length; i++) {
  184.                         System.out.println(creditcardnumbers[i]
  185.                                         + ":"
  186.                                         + (isValidCreditCardNumber(creditcardnumbers[i]) ? "valid"
  187.                                                         : "invalid"));
  188.                 }
  189.         }
  190. }
  191. //java/7103

回复 "Java随机生成信用卡卡号的代码"

这儿你可以回复上面这条便签

captcha