Guest User

Untitled

a guest
Nov 24th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. /*
  5. * Code by: Christopher Ardelean
  6. * Version 1.0
  7. * 2015-01-29
  8. *
  9. * I had far too much fun with this one...
  10. */
  11.  
  12. namespace Password_Generation
  13. {
  14. class Program
  15. {
  16. static char[] ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
  17. static char[] alpha = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
  18. static char[] numeric = "0123456789".ToCharArray();
  19. static char[] symbol = "!@#$%^&*()[]{};:'\",.<>/?\\|_+-=`~".ToCharArray();
  20.  
  21. static string keyboard = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
  22. static string KEYBOARD = "~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?";
  23.  
  24. static string[] units = { "Seconds", "Minutes", "Hours", "Days", "Weeks", "Years", "Decades", "Centuries", "Millenia" };
  25. static double[] unitValues = { 1, 60, 60, 24, 7, (365.242196 / 7), 10, 10, 10 }; // 365.242196 is a reasonable estimate for leap years
  26.  
  27. static void Main(string[] args)
  28. {
  29. int length = 0;
  30. int numberOfPass = 1;
  31. bool uppercase = false;
  32. bool lowercase = false;
  33. bool num = false;
  34. bool sym = false;
  35. bool ASCII = false;
  36. bool again = true;
  37.  
  38. while (again)
  39. {
  40. Console.Clear();
  41. Console.WriteLine("Welcome to the password generator.\n");
  42. Console.WriteLine("Your current character sets are as follows:");
  43. Console.WriteLine("-------------------------------------------");
  44. ShowArray("Uppercase", ALPHA);
  45. ShowArray("Lowercase", alpha);
  46. ShowArray("Numerics", numeric);
  47. ShowArray("Symbols", symbol);
  48.  
  49. AskInt("How long would you like your password to be", ref length, 1, 32);
  50. AskInt("How many passwords would you like to generate", ref numberOfPass, 1, 20);
  51. AskBool("the full ASCII page", ref ASCII);
  52.  
  53. if (!ASCII)
  54. {
  55. Console.WriteLine();
  56. AskBool("uppercase", ref uppercase);
  57. AskBool("lowercase", ref lowercase);
  58. AskBool("numerics", ref num);
  59. AskBool("symbols", ref sym);
  60. }
  61.  
  62. generatePassword(ASCII, uppercase, lowercase, num, sym, length, numberOfPass);
  63. AskBool("this tool again", ref again);
  64. }
  65.  
  66. Console.WriteLine("Goodbye!");
  67. }
  68.  
  69. static void ShowArray(string name, char[] array)
  70. {
  71. StringBuilder sb = new StringBuilder(80);
  72. sb.Append(name);
  73. sb.Append(":\t");
  74.  
  75. for (int i = 0; i < array.Length; i++)
  76. sb.Append(array[i]);
  77.  
  78. Console.WriteLine(sb.ToString());
  79. }
  80.  
  81. static void AskBool(string subject, ref bool test)
  82. {
  83. Console.Write("Would you like to use {0}? (y|n): ", subject);
  84.  
  85. ConsoleKeyInfo key = Console.ReadKey(true);
  86.  
  87. while (key.Key != ConsoleKey.Y && key.Key != ConsoleKey.N)
  88. key = Console.ReadKey(true);
  89.  
  90. Console.WriteLine(key.Key);
  91.  
  92. test = key.Key == ConsoleKey.Y;
  93. }
  94.  
  95. static void AskInt(string question, ref int response, int lower, int upper)
  96. {
  97. if (upper > lower) // Make sure a coder hooking into this library doesn't accidently create an infinite loop. Defensive coding!
  98. {
  99. Console.WriteLine("\n{0}? (Valid Range: {1}-{2})", question, lower, upper);
  100.  
  101. while (!Int32.TryParse(Console.ReadLine(), out response) || response > upper || response < lower)
  102. {
  103. Console.WriteLine("That input didn't make sense. Please type a numeric value between {0} and {1}.", lower, upper);
  104. }
  105. }
  106. else
  107. throw new Exception("AskInt requires that your upper bound be higher than your lower bound.");
  108. }
  109.  
  110. static void generatePassword(bool ASCII, bool upper, bool lower, bool num, bool sym, int length, int numOfPass)
  111. {
  112. StringBuilder sb = new StringBuilder();
  113. Random r = new Random();
  114. int RandomLength = 0;
  115. char[] charset;
  116.  
  117. if (length > 0 && length < 33 && (ASCII || upper || lower || num || sym)) // Length should never be either of these values, but this is good defensive coding. You never know when someone else might mess up your main method.
  118. {
  119. for (int j = 0; j < numOfPass; j++)
  120. {
  121. if (ASCII)
  122. {
  123. RandomLength = 255;
  124.  
  125. for (int i = 0; i < length; i++)
  126. {
  127. sb.Append((char)(r.Next(RandomLength)+1)); // 0 is NULL terminator, and not alt-able. 255 total values, 1-255.
  128. }
  129. }
  130. else
  131. {
  132. RandomLength = 0;
  133. RandomLength += upper ? ALPHA.Length : 0;
  134. RandomLength += lower ? alpha.Length : 0;
  135. RandomLength += num ? numeric.Length : 0;
  136. RandomLength += sym ? symbol.Length : 0;
  137.  
  138. charset = new char[RandomLength];
  139. int curLength = 0;
  140.  
  141. BuildCharset(upper, ref curLength, ref charset, ref ALPHA);
  142. BuildCharset(lower, ref curLength, ref charset, ref alpha);
  143. BuildCharset(num, ref curLength, ref charset, ref numeric);
  144. BuildCharset(sym, ref curLength, ref charset, ref symbol);
  145.  
  146. for (int i = 0; i < length; i++)
  147. {
  148. int pos = r.Next(RandomLength);
  149. sb.Append(charset[pos]);
  150. }
  151. }
  152.  
  153. Console.WriteLine("\nYour generated password is: {0}", ASCII ? KeyCombo(sb.ToString()) : sb.ToString());
  154. if (!ASCII)
  155. Console.WriteLine(KeyCombo(sb.ToString()));
  156.  
  157. sb.Clear();
  158. }
  159.  
  160. double stats = Math.Pow((double)RandomLength, (double)length);
  161. double result1 = Math.Ceiling(stats / 1000);
  162. double result2 = Math.Ceiling(stats / 10000000);
  163. double result3 = Math.Ceiling(stats / 100000000000);
  164. string unit1 = "";
  165. string unit2 = "";
  166. string unit3 = "";
  167.  
  168. FriendlyUnits(ref result1, ref unit1);
  169. FriendlyUnits(ref result2, ref unit2);
  170. FriendlyUnits(ref result3, ref unit3);
  171.  
  172. Console.WriteLine("\nPassword Statistics:");
  173. Console.WriteLine("---------------------");
  174.  
  175. if (stats < Int64.MaxValue)
  176. Console.WriteLine("\nYour password's uniqueness is 1 in {0:N0}", stats);
  177. else
  178. Console.WriteLine("\nYour password's uniqueness is 1 in {0}", stats);
  179.  
  180. if (result1 < 100)
  181. Console.WriteLine("At 1,000 guesses per second it would take\t{0:N1}\t{1} to guess.", result1, unit1);
  182. else
  183. Console.WriteLine("At 1,000 guesses per second it would take:\n{0:F1} {1} to guess.", result1, unit1);
  184.  
  185. if (result2 < 100)
  186. Console.WriteLine("At 10,000,000 guesses per second it would take\t{0:N1}\t{1} to guess.", result2, unit2);
  187. else
  188. Console.WriteLine("At 10,000,000 guesses per second it would take:\n{0:F1} {1} to guess.", result2, unit2);
  189.  
  190. if (result3 < 100)
  191. Console.WriteLine("At 100 Billion guesses per second it would take\t{0:N1}\t{1} to guess.\n", result3, unit3);
  192. else
  193. Console.WriteLine("At 100 Billion guesses per second it would take:\n{0:F1} {1} to guess.\n", result3, unit3);
  194. }
  195. else
  196. {
  197. Console.WriteLine("\nNo valid passwords could be generated. Check your criteria, and try again.\n");
  198. }
  199. }
  200.  
  201. static string KeyCombo (string password)
  202. {
  203. StringBuilder sb = new StringBuilder(80);
  204.  
  205. for (int i = 0; i < password.Length; i++ )
  206. {
  207. char c = password.Substring(i, 1).ToCharArray()[0];
  208. int k = keyboard.IndexOf(c);
  209. int K = KEYBOARD.IndexOf(c);
  210.  
  211. if (k > 0)
  212. sb.Append(string.Format("{0} ", keyboard.Substring(k, 1).ToUpper()));
  213. else if (K > 0)
  214. sb.Append(string.Format("Shift+{0} ", keyboard.Substring(K, 1).ToUpper()));
  215. else if (c == ' ')
  216. sb.Append("Spacebar ");
  217. else
  218. sb.Append(string.Format("Alt+{0} ", (byte)c));
  219. }
  220.  
  221. return sb.ToString();
  222. }
  223.  
  224. static void FriendlyUnits (ref double d, ref string unit)
  225. {
  226. double result = d;
  227. int i = 0;
  228.  
  229. // This code might never run if the result is already within range.
  230. while (result >= 1 && i < units.Length - 1)
  231. {
  232. i++;
  233. result = result / unitValues[i];
  234.  
  235. if (result >= 1) // only assign the result if it's in the right range
  236. d = result;
  237. else // This is the final run of the loop, because the result is too coarse. Reduce i so that the appropriate unit is selected.
  238. i--;
  239. }
  240.  
  241. unit = units[i];
  242. }
  243.  
  244. static void BuildCharset (bool test, ref int curLength, ref char[] charset, ref char[] chars)
  245. {
  246. if (test)
  247. {
  248. chars.CopyTo(charset, curLength);
  249. curLength += chars.Length;
  250. }
  251. }
  252. }
  253. }
Add Comment
Please, Sign In to add comment