/verve patch/base/Applications/BetterExplorer/BExplorer/BetterExplorer/Networks/AccountData.cs

https://bitbucket.org/jdm7dvmoore/msft-research-verve · C# · 119 lines · 62 code · 23 blank · 34 comment · 0 complexity · d7e1d5aeb22c2ade734e88b8b1e79c71 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. namespace BetterExplorer.Networks {
  7. /// <summary>
  8. /// This class manages all account-related information for Better Explorer.
  9. /// </summary>
  10. /// <remarks>
  11. /// this class will be used to manage the account information file, including anything security-related,
  12. /// and will be a point of communication between the main UI and this information
  13. /// </remarks>
  14. [Obsolete("Never Actually Used. We should remove this!!!")]
  15. public class NetworkAccountManager : List<NetworkItem> {
  16. // Call this function to remove the key from memory after use for security.
  17. [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
  18. private static extern bool ZeroMemory(ref string Destination, int Length);
  19. // TO_DO: Add code for storing and retrieving from file
  20. // TO_DO: Decide upon an encryption service (DES, Rjindael, etc.) and add code for
  21. // "decrypt file to memory" and "encrypt memory to file".
  22. /*
  23. * Account File Structure:
  24. *
  25. * "
  26. * ;account:key:secret:username:password;account:username:password;account:address:username:password;
  27. * "
  28. *
  29. */
  30. //private string _PassFile;
  31. //private string _DataFile;
  32. //private bool _MasterUsed;
  33. //private SecureString _MasterPW;
  34. private string _dataloc = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BExplorer\\NetworkAccounts\\";
  35. public string DataLocation {
  36. get {
  37. return _dataloc;
  38. }
  39. set {
  40. _dataloc = value;
  41. }
  42. }
  43. // Function to Generate a 64 bits Key.
  44. static string GenerateKey() {
  45. // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
  46. DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
  47. // Use the Automatically generated key for Encryption.
  48. return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
  49. }
  50. static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey) {
  51. FileStream fsInput = new FileStream(sInputFilename,
  52. FileMode.Open,
  53. FileAccess.Read);
  54. FileStream fsEncrypted = new FileStream(sOutputFilename,
  55. FileMode.Create,
  56. FileAccess.Write);
  57. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  58. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  59. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  60. ICryptoTransform desencrypt = DES.CreateEncryptor();
  61. CryptoStream cryptostream = new CryptoStream(fsEncrypted,
  62. desencrypt,
  63. CryptoStreamMode.Write);
  64. byte[] bytearrayinput = new byte[fsInput.Length];
  65. fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
  66. cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
  67. cryptostream.Close();
  68. fsInput.Close();
  69. fsEncrypted.Close();
  70. }
  71. static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey) {
  72. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  73. //A 64 bit key and IV is required for this provider.
  74. //Set secret key For DES algorithm.
  75. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  76. //Set initialization vector.
  77. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  78. //Create a file stream to read the encrypted file back.
  79. FileStream fsread = new FileStream(sInputFilename,
  80. FileMode.Open,
  81. FileAccess.Read);
  82. //Create a DES decryptor from the DES instance.
  83. ICryptoTransform desdecrypt = DES.CreateDecryptor();
  84. //Create crypto stream set to read and do a
  85. //DES decryption transform on incoming bytes.
  86. CryptoStream cryptostreamDecr = new CryptoStream(fsread,
  87. desdecrypt,
  88. CryptoStreamMode.Read);
  89. //Print the contents of the decrypted file.
  90. StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
  91. fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
  92. fsDecrypted.Flush();
  93. fsDecrypted.Close();
  94. }
  95. }
  96. }