原版js生成银行卡号

时间:2022-08-20 02:52:14
function init() {
undefined = "undefined";
mkCClist();
} function ccchk(cdi) {
document.console.output1.value = "";
if (cdi != "" && cdi != null) {
document.console.output1.value = "Processing...";
var cf = sbtString(cdi, " -/abcdefghijklmnopqrstuvwyzABCDEFGHIJLMNOPQRSTUVWYZ|\#()[]{}?%&=!?+*.,;:'");
var cn = chkCard(cf);
var clcd = chkLCD(cf);
var clcdt = "NOT PASSED";
if (clcd) {
clcdt = "PASSED";
}
var ccck = chkCCCksum(cf, cn);
var ccckt = "NOT PASSED";
if (ccck) {
ccckt = "PASSED";
}
var cjd = "INVALID CARD NUMBER";
if (clcd && ccck) {
cjd = "This card number appears to be valid.";
}
var out = "";
out += "Card type : " + cn + "\n";
out += "CCChecksum : " + ccckt + "\n";
out += "Luhn Check Digit: " + clcdt + "\n";
out += cjd;
document.console.output1.value = out;
}
} function ccngen(p, tr) {
tr *= 1.0;
if (tr < 1 || tr == null) {
tr = 1;
}
document.console.output2.value = "";
if (p != "" && p != null) {
var cn = chkCard(p);
for (var i = tr; i >= 1; i--) {
document.console.output2.value = "Processing... " + i;
var cdi = sbtStringSpRnd(p, "x", "0123456789");
var cf = sbtString(cdi, " -/abcdefghijklmnopqrstuvwyzABCDEFGHIJLMNOPQRSTUVWYZ");
var clcd = chkLCD(cf);
var ccck = chkCCCksum(cf, cn);
if (clcd && ccck) {
break;
}
}
var out = "Couldn't find any valid number for given pattern";
if (clcd && ccck) {
var cn = chkCard(cdi);
var ccnspc = "";
for (var j = 0; j <= 3; j++) {
if (document.console.ccnsp.options[j].selected) {
break;
}
}
if (j == 1) {
ccnspc = " ";
}
if (j == 2) {
ccnspc = "-";
}
var cdif = "";
for (var i = 1; i <= cdi.length; i++) {
var aS = midS(cdi, i, 1);
if (aS == " ") {
aS = ccnspc;
}
cdif += aS;
}
var out = "Valid " + cn + " # found:\n" + cdif;
}
document.console.output2.value = out;
}
} function chgccp() {
mkCClist();
for (var j = 0; j <= tw - 1; j++) {
if (document.console.ccpp.options[j].selected) {
break;
}
}
document.console.ccp.value = c[j + 1];
} function chkCard(cdi) {
cdi += "";
if (c[1] == undefined || c[1] == null || c[1] == "") {
mkCClist();
}
var ccn = 0;
var cn = "unknown"
var cf = sbtString(cdi, " -/abcdefghijklmnopqrstuvwyzABCDEFGHIJLMNOPQRSTUVWYZ");
if (leftS(cf, 1) == "4") {
cf = leftS(cf, 8);
}
for (var i = 1; i <= tw; i++) {
var cct = sbtString(c[i], " -/abcdefghijklmnopqrstuvwyzABCDEFGHIJLMNOPQRSTUVWYZ");
if (leftS(cf, 1) == "4") {
cct = leftS(cct, 8);
}
var ccc = cmpPattern(cf, cct);
if (ccc) {
ccn = i;
break;
}
}
if (ccn > 0) {
cn = cd[i];
}
return cn;
} function chkCCCksum(cf, cn) {
var r = false;
var w = "21";
// if (linstr(cn,"AmEx") || linstr(cn,"Diner")) {var w="12";}
var ml = "";
var j = 1;
for (var i = 1; i <= cf.length - 1; i++) {
var m = midS(cf, i, 1) * midS(w, j, 1);
m = sumDigits(m);
ml += "" + m;
j++;
if (j > w.length) {
j = 1;
}
}
var ml2 = sumDigits(ml, -1);
var ml1 = (sumDigits(ml2, -1) * 10 - ml2) % 10;
if (ml1 == rightS(cf, 1)) {
r = true;
}
return r;
} function chkLCD(cf) {
var r = false;
cf += "";
var bl = isdiv(cf.length, 2);
var ctd = 0;
for (var i = 1; i <= cf.length; i++) {
var cdg = midS(cf, i, 1);
if (isdiv(i, 2) != bl) {
cdg *= 2;
if (cdg > 9) {
cdg -= 9;
}
}
ctd += cdg * 1.0;
}
if (isdiv(ctd, 10)) {
r = true;
}
return r;
} function mkCClist() {
tw = 2;
c = new makeArray(tw);
cd = new makeArray(tw);
var i = 1;
c[i] = "622202360547xxxxxxx";
cd[i] = "工商银行";
i++;
c[i] = "622617310xxxxxxx";
cd[i] = "民生银行";
i++;
} // Powered by MilkieX - http://www.ElfQrin.com
function leftS(aS, n) {
aS += "";
var rS = "";
if (n >= 1) {
rS = aS.substring(0, n);
}
return rS;
} function rightS(aS, n) {
aS += "";
var rS = "";
if (n >= 1) {
rS = aS.substring(aS.length - n, aS.length);
}
return rS;
} function midS(aS, n, n2) {
aS += "";
var rS = "";
if (n2 == null || n2 == "") {
n2 = aS.length;
}
n *= 1;
n2 *= 1;
if (n < 0) {
n++;
}
rS = aS.substring(n - 1, n - 1 + n2);
return rS;
} function linstr(aS, bS) {
aS += "";
bS += "";
var r = false;
if (leftS(aS, bS.length) == bS) {
r = true;
}
return r;
} function sbtString(s1, s2) {
var ous = "";
s1 += "";
s2 += "";
for (var i = 1; i <= s1.length; i++) {
var c1 = s1.substring(i - 1, i);
var c2 = s2.indexOf(c1);
if (c2 == -1) {
ous += c1;
}
}
return ous;
} function sbtStringSpRnd(s1, s2, bS) {
if (bS == null || bS == "") {
bS = "0123456789";
}
var ous = "";
bS += "";
for (var i = 1; i <= s1.length; i++) {
var c1 = s1.substring(i - 1, i);
var c2 = s2.indexOf(c1);
if (c2 == -1) {
ous += c1;
} else {
ous += midS(bS, Math.floor(Math.random() * (bS.length - 1)) + 1, 1);
}
}
return ous;
} function cmpPattern(a, p, x) {
if (x == "" || x == null) {
x = "x";
}
x = "" + x.substring(0, 1);
a += "";
p += "";
r = false;
mc = 0;
if (a.length == p.length) {
for (var i = 1; i <= a.length; i++) {
a1 = midS(a, i, 1);
p1 = midS(p, i, 1);
if (a1 == p1 || p1 == x) {
mc++;
}
}
}
if (mc == a.length) {
r = true;
}
return r;
} function isdiv(a, b) {
if (b == null) {
b = 2;
}
a *= 1.0;
b *= 1.0;
var r = false;
if (a / b == Math.floor(a / b)) {
r = true;
}
return r;
} function sumDigits(n, m) {
if (m == 0 || m == null) {
m = 1;
}
n += "";
if (m > 0) {
while (n.length > m) {
var r = 0;
for (var i = 1; i <= n.length; i++) {
r += 1.0 * midS(n, i, 1);
}
n = "" + r;
}
} else {
for (var j = 1; j <= Math.abs(m); j++) {
var r = 0;
for (var i = 1; i <= n.length; i++) {
r += 1.0 * midS(n, i, 1);
}
n = "" + r;
}
}
r = n;
return r;
} function makeArray(n) {
this.length = n;
for (var i = 1; i <= n; i++) {
this[i] = 0;
}
return this;
} // End of MilkieX
//var dl=""+document.location; dl=dl.toLowerCase(); if (dl.substring(0,22)!="http://www.elfqrin.com" && dl.substring(0,16)!="file:///c|/data/") {window.location="/";}
init();

改成java写法:

package cn.x.common;

import java.text.DecimalFormat;

public class BankCardGenerator {

    public static void main(String[] args) {
System.out.println(ccngen("6226173107771021",1000));
System.out.println(ccngen("6226173102726523",1000));
System.out.println(ccngen("6226173102070486",1000));
System.out.println(ccngen("6226173100366066",1000));
System.out.println(ccngen("6226173108201666",1000));
System.out.println(ccngen("622617310xxxxxxx",1000));
} private static String ccngen(String pattern, int tryTimes) {
boolean genSuccess = false;
String validCardNo = "";
for(int i = 0; i < tryTimes; i++) {
String randomSevenNum = new DecimalFormat("0000000").format(Math.random() * 10000000);
String testCardNo = "";
if(pattern.contains("xxxxxxx")) {
testCardNo = pattern.replaceAll("x", "") + randomSevenNum ;
} else {
testCardNo = pattern ;
}
System.out.println("test " + i + ": " + testCardNo);
if (chkLCD(testCardNo) && chkCCCksum(testCardNo)) {
genSuccess = true ;
validCardNo = testCardNo;
break;
} if(!pattern.contains("xxxxxxx")) {break;}
}
if(genSuccess) {
return validCardNo;
} else {
return "genCardNoFailed!";
}
} private static boolean chkCCCksum(String cf) {
boolean r = false;
String w = "21";
String ml = "";
int j = 1;
for (int i = 1; i <= cf.length() - 1; i++) {
int m =Integer.valueOf(midS(cf, i, 1)) * Integer.valueOf(midS(w, j, 1));
m =Integer.valueOf( sumDigits(String.valueOf(m),1) );
ml += "" + m;
j++;
if (j > w.length()) {
j = 1;
}
}
int ml2 = Integer.valueOf(sumDigits(ml, -1) );
int ml1 = (Integer.valueOf(sumDigits(String.valueOf(ml2), -1)) * 10 - ml2) % 10;
if (ml1 == rightS(cf, 1)) {
r = true;
}
return r;
} private static int rightS(String aS, int n) {
String rS = "";
if (n >= 1) {
rS = aS.substring(aS.length() - n, aS.length());
}
return Integer.valueOf(rS);
} private static String sumDigits(String n ,int m) {
// if (m == 0 ) {
// m = 1;
// }
int r = 0;
if (m > 0) {
while (n.length() > m) {
for (int i = 1; i <= n.length(); i++) {
r += Integer.valueOf(midS(n, i, 1));
}
n = "" + r;
}
} else {
for (int j = 1; j <= Math.abs(m); j++) {
for (int i = 1; i <= n.length(); i++) {
r += Integer.valueOf( midS(n, i, 1));
}
n = "" + r;
}
}
// r = n;
return n;
} private static boolean chkLCD(String cf) {
boolean r = false;
cf += "";
boolean bl = isdiv(cf.length(), 2);
int ctd = 0;
for (int i = 1; i <= cf.length(); i++) {
int cdg = Integer.valueOf(midS(cf, i, 1));
if (isdiv(i, 2) != bl) {
cdg *= 2;
if (cdg > 9) {
cdg -= 9;
}
}
ctd += cdg * 1.0;
}
if (isdiv(ctd, 10)) {
r = true;
}
return r;
} private static boolean isdiv(int a, int b) {
a *= 1.0;
b *= 1.0;
boolean r = false;
if (a / b == Math.floor(a / b)) {
r = true;
}
return r;
} private static String sbtStringSpRnd(String s1, String s2, String bS) {
if (bS == "") {
bS = "0123456789";
}
String ous = "";
bS += "";
for (int i = 1; i <= s1.length(); i++) {
String c1 = s1.substring(i - 1, i);
int c2 = s2.indexOf(c1);
if (c2 == -1) {
ous += c1;
} else {
ous += midS(bS, (int)Math.floor(Math.random() * (bS.length() - 1)) + 1, 1);
}
}
return ous;
} private static String chkCard(String cdi) {
cdi += "";
int ccn = 0;
String cn = "unknown" ;
String cf = sbtString(cdi, " -/abcdefghijklmnopqrstuvwyzABCDEFGHIJLMNOPQRSTUVWYZ");
if (leftS(cf, 1) == "4") {
cf = leftS(cf, 8);
}
String[] c = {"","622202360547xxxxxxx","622617310xxxxxxx"};
String[] cd = {"","工商银行","民生银行"};
for (int i = 1; i <= 2; i++) {
String cct = sbtString(c[i], " -/abcdefghijklmnopqrstuvwyzABCDEFGHIJLMNOPQRSTUVWYZ");
if (leftS(cf, 1) == "4") {
cct = leftS(cct, 8);
}
boolean ccc = cmpPattern(cf, cct);
if (ccc) {
ccn = i;
break;
}
}
if (ccn > 0) {
cn = cd[ccn];
}
return cn;
} private static String sbtString(String s1, String s2) {
String ous = "";
s1 += "";
s2 += "";
for (int i = 1; i <= s1.length(); i++) {
String c1 = s1.substring(i - 1, i);
int c2 = s2.indexOf(c1);
if (c2 == -1) {
ous += c1;
}
}
return ous;
} private static String leftS(String as, int n) {
as += "";
String rS = "";
if (n >= 1) {
rS = as.substring(0, n);
}
return rS;
} private static boolean cmpPattern(String a, String p) {
String x = null;
if (x == "" || x == null) {
x = "x";
}
x = "" + x.substring(0, 1);
a += "";
p += "";
boolean r = false;
int mc = 0;
if (a.length() == p.length()) {
for (int i = 1; i <= a.length(); i++) {
String a1 = midS(a, i, 1);
String p1 = midS(p, i, 1);
if (a1 == p1 || p1 == x) {
mc++;
}
}
}
if (mc == a.length()) {
r = true;
}
return r;
} private static String midS(String aS, int n, int n2) {
aS += "";
String rS = "";
n *= 1;
n2 *= 1;
if (n < 0) {
n++;
}
rS = aS.substring(n - 1, n - 1 + n2);
return rS;
} }