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

来自 , 2021-04-01, 写在 C#, 查看 146 次.
URL http://www.code666.cn/view/a6d8ecc0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. namespace CreditCardNumberGenerator
  6. {
  7.    public class RandomCreditCardNumberGenerator
  8.    {
  9.  
  10.       /*This is a port of the port of of the Javascript credit card number generator now in C#
  11.        
  12.        * by Kev Hunter http://kevhunter.wordpress.com
  13.        */
  14. /*
  15.  * /**
  16. * See the license below. Obviously, this is not a Javascript credit card number
  17. * generator. However, The following class is a port of a Javascript credit card
  18. * number generator.
  19. *
  20. * @author robweber
  21. *
  22.  
  23.  
  24. * Javascript credit card number generator Copyright (C) 2006 Graham King
  25. * graham@darkcoding.net
  26. *
  27. * This program is free software; you can redistribute it and/or modify it
  28. * under the terms of the GNU General Public License as published by the
  29. * Free Software Foundation; either version 2 of the License, or (at your
  30. * option) any later version.
  31. *
  32. * This program is distributed in the hope that it will be useful, but
  33. * WITHOUT ANY WARRANTY; without even the implied warranty of
  34. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  35. * Public License for more details.
  36. *
  37. * You should have received a copy of the GNU General Public License along
  38. * with this program; if not, write to the Free Software Foundation, Inc.,
  39. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  40. *
  41. * www.darkcoding.net
  42. */
  43.       public static String[] AMEX_PREFIX_LIST = new[] {"34", "37"};
  44.  
  45.       public static String[] DINERS_PREFIX_LIST = new[]
  46.                                                      {
  47.                                                         "300",
  48.                                                         "301", "302", "303", "36", "38"
  49.                                                      };
  50.  
  51.       public static String[] DISCOVER_PREFIX_LIST = new[] {"6011"};
  52.  
  53.       public static String[] ENROUTE_PREFIX_LIST = new[]
  54.                                                       {
  55.                                                          "2014",
  56.                                                          "2149"
  57.                                                       };
  58.  
  59.       public static String[] JCB_PREFIX_LIST = new[]
  60.                                                      {
  61.                                                         "35"
  62.                                                      };
  63.  
  64.       public static String[] MASTERCARD_PREFIX_LIST = new[]
  65.                                                          {
  66.                                                             "51",
  67.                                                             "52", "53", "54", "55"
  68.                                                          };
  69.  
  70.       public static String[] VISA_PREFIX_LIST = new[]
  71.                                                    {
  72.                                                       "4539",
  73.                                                       "4556", "4916", "4532", "4929", "40240071", "4485", "4716", "4"
  74.                                                    };
  75.  
  76.       public static String[] VOYAGER_PREFIX_LIST = new[] {"8699"};
  77.  
  78.       private static String Strrev(String str)
  79.       {
  80.          if (str == null)
  81.             return "";
  82.          String revstr = "";
  83.          for (int i = str.Length - 1; i >= 0; i--)
  84.          {
  85.             revstr += str[i];
  86.          }
  87.          return revstr;
  88.       }
  89.  
  90. /*
  91. * 'prefix' is the start of the CC number as a string, any number of digits.
  92. * 'length' is the length of the CC number to generate. Typically 13 or 16
  93. */
  94.  
  95.       private static String completed_number(String prefix, int length)
  96.       {
  97.          String ccnumber = prefix;
  98. // generate digits
  99.          while (ccnumber.Length < (length - 1))
  100.          {
  101.             double rnd = (new Random().NextDouble()*1.0f - 0f);
  102.             ccnumber += Math.Floor(rnd*10);
  103.             Thread.Sleep(20);
  104.          }
  105. // reverse number and convert to int
  106.          String reversedCCnumberString = Strrev(ccnumber);
  107.          var reversedCCnumberList = new List<int>();
  108.          for (int i = 0; i < reversedCCnumberString.Length; i++)
  109.          {
  110.             reversedCCnumberList.Add(Convert.ToInt32(reversedCCnumberString[i].ToString()));
  111.          }
  112. // calculate sum
  113.          int sum = 0;
  114.          int pos = 0;
  115.          int[] reversedCCnumber = reversedCCnumberList
  116.             .ToArray();
  117.          while (pos < length - 1)
  118.          {
  119.             int odd = reversedCCnumber[pos]*2;
  120.             if (odd > 9)
  121.             {
  122.                odd -= 9;
  123.             }
  124.             sum += odd;
  125.             if (pos != (length - 2))
  126.             {
  127.                sum += reversedCCnumber[pos + 1];
  128.             }
  129.             pos += 2;
  130.          }
  131. // calculate check digit
  132.          int checkdigit =
  133.             Convert.ToInt32((Math.Floor((decimal) sum/10) + 1)*10 - sum)%10;
  134.          ccnumber += checkdigit;
  135.          return ccnumber;
  136.       }
  137.  
  138.       public static String[] credit_card_number(String[] prefixList, int length,
  139.                                                 int howMany)
  140.       {
  141.          var result = new Stack<String>();
  142.          for (int i = 0; i < howMany; i++)
  143.          {
  144.             int next = new Random().Next(0, prefixList.Length - 1);
  145.             String ccnumber = prefixList[next - 1];
  146.             result.Push(completed_number(ccnumber, length));
  147.          }
  148.          return result.ToArray();
  149.       }
  150.  
  151.       public static String[] generateMasterCardNumbers(int howMany)
  152.       {
  153.          return credit_card_number(MASTERCARD_PREFIX_LIST, 16, howMany);
  154.       }
  155.  
  156.       public static String generateMasterCardNumber()
  157.       {
  158.          return credit_card_number(MASTERCARD_PREFIX_LIST, 16, 1)[0];
  159.       }
  160.  
  161.       public static string Reverse(string str)
  162.       {
  163. // convert the string to char array
  164.          char[] charArray = str.ToCharArray();
  165.          int len = str.Length - 1;
  166. /*
  167. now this for is a bit unconventional at first glance because there
  168. are 2 variables that we're changing values of: i++ and len--.
  169. the order of them is irrelevant. so basicaly we're going with i from
  170. start to end of the array. with len we're shortening the array by one
  171. each time. this is probably understandable.
  172. */
  173.          for (int i = 0; i < len; i++, len--)
  174.          {
  175. /*
  176. now this is the tricky part people that should know about it don't.
  177. look at the table below to see what's going on exactly here.
  178. */
  179.             charArray[i] ^= charArray[len];
  180.             charArray[len] ^= charArray[i];
  181.             charArray[i] ^= charArray[len];
  182.          }
  183.          return new string(charArray);
  184.       }
  185.  
  186.       public static bool isValidCreditCardNumber(String creditCardNumber)
  187.       {
  188.          bool isValid = false;
  189.          try
  190.          {
  191.             String reversedNumber = Reverse(creditCardNumber);
  192.             int mod10Count = 0;
  193.             for (int i = 0; i < reversedNumber.Length; i++)
  194.             {
  195.                int augend = Convert.ToInt32(reversedNumber[i]);
  196.                if (((i + 1)%2) == 0)
  197.                {
  198.                   String productString = (augend*2).ToString();
  199.                   augend = 0;
  200.                   for (int j = 0; j < productString.Length; j++)
  201.                   {
  202.                      augend += Convert.ToInt32(productString[j]);
  203.                   }
  204.                }
  205.                mod10Count += augend;
  206.             }
  207.             if ((mod10Count%10) == 0)
  208.             {
  209.                isValid = true;
  210.             }
  211.          }
  212.          catch
  213.          {
  214.          }
  215.          return isValid;
  216.       }
  217.    }
  218. }
  219. //csharp/7104

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

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

captcha