如下内容段是关于C#随机生成信用卡卡号的的内容。
using System;
using System.Collections.Generic;
using System.Threading;
namespace CreditCardNumberGenerator
{
public class RandomCreditCardNumberGenerator
{
public static String[] AMEX_PREFIX_LIST = new[] {"34", "37"};
public static String[] DINERS_PREFIX_LIST = new[]
{
"300",
"301", "302", "303", "36", "38"
};
public static String[] DISCOVER_PREFIX_LIST = new[] {"6011"};
public static String[] ENROUTE_PREFIX_LIST = new[]
{
"2014",
"2149"
};
public static String[] JCB_PREFIX_LIST = new[]
{
"35"
};
public static String[] MASTERCARD_PREFIX_LIST = new[]
{
"51",
"52", "53", "54", "55"
};
public static String[] VISA_PREFIX_LIST = new[]
{
"4539",
"4556", "4916", "4532", "4929", "40240071", "4485", "4716", "4"
};
public static String[] VOYAGER_PREFIX_LIST = new[] {"8699"};
private static String Strrev(String str)
{
if (str == null)
return "";
String revstr = "";
for (int i = str.Length - 1; i >= 0; i--)
{
revstr += str[i];
}
return revstr;
}
private static String completed_number(String prefix, int length)
{
String ccnumber = prefix;
while (ccnumber.Length < (length - 1))
{
Thread.Sleep(20);
}
String reversedCCnumberString = Strrev(ccnumber);
var reversedCCnumberList = new List<int>();
for (int i = 0; i < reversedCCnumberString.Length; i++)
{
reversedCCnumberList.Add(Convert.ToInt32(reversedCCnumberString[i].ToString()));
}
int sum = 0;
int pos = 0;
int[] reversedCCnumber = reversedCCnumberList
.ToArray();
while (pos < length - 1)
{
if (odd > 9)
{
odd -= 9;
}
sum += odd;
if (pos != (length - 2))
{
sum += reversedCCnumber[pos + 1];
}
pos += 2;
}
int checkdigit =
ccnumber += checkdigit;
return ccnumber;
}
public static String[] credit_card_number(String[] prefixList, int length,
int howMany)
{
var result = new Stack<String>();
for (int i = 0; i < howMany; i++)
{
int next = new Random().Next(0, prefixList.Length - 1);
String ccnumber = prefixList[next - 1];
result.Push(completed_number(ccnumber, length));
}
return result.ToArray();
}
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 string Reverse(string str)
{
char[] charArray = str.ToCharArray();
int len = str.Length - 1;
now this for is a bit unconventional at first glance because there
are 2 variables that we’re changing values of: i++ and len–.
the order of them is irrelevant. so basicaly we’re going with i from
start to end of the array. with len we’re shortening the array by one
each time. this is probably understandable.
for (int i = 0; i < len; i++, len–)
{
now this is the tricky part people that should know about it don’t.
look at the table below to see what’s going on exactly here.
charArray[i] ^= charArray[len];
charArray[len] ^= charArray[i];
charArray[i] ^= charArray[len];
}
return new string(charArray);
}
public static bool isValidCreditCardNumber(String creditCardNumber)
{
bool isValid = false;
try
{
String reversedNumber = Reverse(creditCardNumber);
int mod10Count = 0;
for (int i = 0; i < reversedNumber.Length; i++)
{
int augend = Convert.ToInt32(reversedNumber[i]);
if (((i + 1)%2) == 0)
{
augend = 0;
for (int j = 0; j < productString.Length; j++)
{
augend += Convert.ToInt32(productString[j]);
}
}
mod10Count += augend;
}
if ((mod10Count%10) == 0)
{
isValid = true;
}
}
catch
{
}
return isValid;
}
}
}