Advertisement
Guest User

USB Auth

a guest
Aug 13th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Diagnostics;
  8. using System.Net;
  9. using Renci.SshNet;
  10. using System.Threading;
  11. using System.Xml;
  12.  
  13. namespace USB_auth
  14. {
  15. class Program
  16. {
  17. static void Main(string[] args)
  18. {
  19. string Host = "ts.norwhales.com";
  20. int Port = 20522;
  21. String RemoteFileName = "/home/henrik/passwd/passwd.kdbx";
  22. string dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"passwd\\");
  23. if (!Directory.Exists(dir))
  24. {
  25. Directory.CreateDirectory(dir);
  26. }
  27. var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "passwd\\passwd.kdbx");
  28. Console.WriteLine(fileName);
  29. String LocalDestinationFilename = fileName;
  30. String Username = "henrik";
  31. string pass = "";
  32. bool tryagain = true;
  33. while (tryagain)
  34. {
  35. try
  36. {
  37. Console.Write("Passord: ");
  38. ConsoleKeyInfo key;
  39. pass = "";
  40. do
  41. {
  42. key = Console.ReadKey(true);
  43.  
  44. if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
  45. {
  46. pass += key.KeyChar;
  47. Console.Write("*");
  48. }
  49. else
  50. {
  51. if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
  52. {
  53. pass = pass.Substring(0, (pass.Length - 1));
  54. Console.Write("\b \b");
  55. }
  56. }
  57. }
  58. while (key.Key != ConsoleKey.Enter);
  59. Console.WriteLine();
  60. tryagain = false;
  61. using (var sftp = new SftpClient(Host, Port, Username, pass))
  62. {
  63. sftp.Connect();
  64.  
  65. using (var file = File.OpenWrite(LocalDestinationFilename))
  66. {
  67. sftp.DownloadFile(RemoteFileName, file);
  68. }
  69.  
  70. sftp.Disconnect();
  71. }
  72. }
  73. catch (Renci.SshNet.Common.SshAuthenticationException)
  74. {
  75. Console.WriteLine("Feil passord!");
  76. tryagain = true;
  77. }
  78. }
  79. Console.WriteLine("Ferdig!");
  80. Console.WriteLine("Åpner Keepass");
  81. try
  82. {
  83. string xmlFile = "KeePass.config.xml";
  84. System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
  85. xmlDoc.PreserveWhitespace = true;
  86. xmlDoc.Load(xmlFile);
  87. xmlDoc.SelectSingleNode("Configuration/Application/LastUsedFile/Path").InnerText = fileName;
  88. xmlDoc.Save(xmlFile);
  89.  
  90.  
  91. Process.Start("KeePass.exe");
  92. }
  93. catch (System.ComponentModel.Win32Exception)
  94. {
  95. Console.WriteLine("Kunne ikke åpne Keepass! Vær sikker på at Keepass er installert i samme mappe som dette programmet!");
  96. }
  97. catch (FileNotFoundException)
  98. {
  99. Console.WriteLine("Keepass konfigurasjonsfil ikke funnet! Kontroller at KeePass er installert i samme mappe som dette programmet");
  100. }
  101.  
  102. Thread.Sleep(3000);
  103.  
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement