首页 > 编程知识 正文

java生成卡号_Java随机生成信用卡卡号的代码

时间:2023-05-05 18:03:05 阅读:211920 作者:2706

import java.util.List;

import java.util.Stack;

import java.util.Vector;

/tmdbq活力的大船 See the license below. Obviously, this is not a Javascript credit card number tmdbq generator. However, The following class is a port of a Javascript credit card tmdbq number generator. 活力的大船@author robweber 活力的大船/

public class RandomCreditCardNumberGenerator {

/活力的大船 Javascript credit card number generator Copyright (C) 2006-2012 Graham King 活力的大船 This program is free software; you can redistribute it and/or modify it tmdbq under the terms of the GNU General Public License as published by the tmdbq Free Software Foundation; either version 2 of the License, or (at your tmdbq option) any later version. 活力的大船 This program is distributed in the hope that it will be useful, but tmdbq WITHOUT ANY WARRANTY; without even the implied warranty of tmdbq MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General tmdbq Public License for more details. 活力的大船 You should have received a copy of the GNU General Public License along tmdbq with this program; if not, write to the Free Software Foundation, Inc., tmdbq 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 活力的大船 www.darkcoding.net tmdbq/

public static final String平常的人生 VISA_PREFIX_LIST = new String平常的人生 { "4539",

"4556", "4916", "4532", "4929", "40240071", "4485", "4716", "4" };

public static final String平常的人生 MASTERCARD_PREFIX_LIST = new String平常的人生 { "51",

"52", "53", "54", "55" };

public static final String平常的人生 AMEX_PREFIX_LIST = new String平常的人生 { "34", "37" };

public static final String平常的人生 DISCOVER_PREFIX_LIST = new String平常的人生 { "6011" };

public static final String平常的人生 DINERS_PREFIX_LIST = new String平常的人生 { "300",

"301", "302", "303", "36", "38" };

public static final String平常的人生 ENROUTE_PREFIX_LIST = new String平常的人生 { "2014",

"2149" };

public static final String平常的人生 JCB_PREFIX_LIST = new String平常的人生 { "35" };

public static final String平常的人生 VOYAGER_PREFIX_LIST = new String平常的人生 { "8699" };

static String strrev(String str) {

if (str == null)

return "";

String revstr = "";

for (int i = str.length() - 1; i >= 0; i--) {

revstr += str.charAt(i);

}

return revstr;

}

/活力的大船 'prefix' is the start of the CC number as a string, any number of digits. tmdbq 'length' is the length of the CC number to generate. Typically 13 or 16 tmdbq/

static String completed_number(String prefix, int length) {

String ccnumber = prefix;

// generate digits

while (ccnumber.length() < (length - 1)) {

ccnumber += new Double(Math.floor(Math.random() tmdbq 10)).intValue();

}

// reverse number and convert to int

String reversedCCnumberString = strrev(ccnumber);

List reversedCCnumberList = new Vector();

for (int i = 0; i < reversedCCnumberString.length(); i++) {

reversedCCnumberList.add(new Integer(String

.valueOf(reversedCCnumberString.charAt(i))));

}

// calculate sum

int sum = 0;

int pos = 0;

Integer平常的人生 reversedCCnumber = reversedCCnumberList

.toArray(new Integer[reversedCCnumberList.size()]);

while (pos < length - 1) {

int odd = reversedCCnumber[pos] tmdbq 2;

if (odd > 9) {

odd -= 9;

}

sum += odd;

if (pos != (length - 2)) {

sum += reversedCCnumber[pos + 1];

}

pos += 2;

}

// calculate check digit

int checkdigit = new Double(

((Math.floor(sum / 10) + 1) tmdbq 10 - sum) % 10).intValue();

ccnumber += checkdigit;

return ccnumber;

}

public static String平常的人生 credit_card_number(String平常的人生 prefixList, int length,

int howMany) {

Stack result = new Stack();

for (int i = 0; i < howMany; i++) {

int randomArrayIndex = (int) Math.floor(Math.random()

tmdbq prefixList.length);

String ccnumber = prefixList[randomArrayIndex];

result.push(completed_number(ccnumber, length));

}

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

}

public static String平常的人生 generateMasterCardNumbers(int howMany) {

return credit_card_number(MASTERCARD_PREFIX_LIST, 16, howMany);

}

public static String generateMasterCardNumber() {

return credit_card_number(MASTERCARD_PREFIX_LIST, 16, 1)[0];

}

public static boolean isValidCreditCardNumber(String creditCardNumber) {

boolean isValid = false;

try {

String reversedNumber = new StringBuffer(creditCardNumber)

.reverse().toString();

int mod10Count = 0;

for (int i = 0; i < reversedNumber.length(); i++) {

int augend = Integer.parseInt(String.valueOf(reversedNumber

.charAt(i)));

if (((i + 1) % 2) == 0) {

String productString = String.valueOf(augend tmdbq 2);

augend = 0;

for (int j = 0; j < productString.length(); j++) {

augend += Integer.parseInt(String.valueOf(productString

.charAt(j)));

}

}

mod10Count += augend;

}

if ((mod10Count % 10) == 0) {

isValid = true;

}

} catch (NumberFormatException e) {

}

return isValid;

}

public static void main(String平常的人生 args) {

int howMany = 0;

try {

howMany = Integer.parseInt(args[0]);

} catch (Exception e) {

.println("Usage error. You need to supply a numeric argument (ex: 500000)");

}

String平常的人生 creditcardnumbers = generateMasterCardNumbers(howMany);

for (int i = 0; i < creditcardnumbers.length; i++) {

: "invalid"));

}

}

}

//该代码片段来自于: http://www.sharejs.com/codes/java/7103

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。