Encryption.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace ArcToolkitCLI.Util
  5. {
  6. public static class Encryption
  7. {
  8. public static byte[] ReadKeyFromFile(string filename)
  9. {
  10. using (var br = new BinaryReader(File.OpenRead(filename)))
  11. {
  12. return br.ReadBytes(2048);
  13. }
  14. }
  15. public static int GetDecryptionKeys(IDecryptionOptions opts, out Dictionary<string, byte[]> keys)
  16. {
  17. keys = new Dictionary<string, byte[]>(StringComparer.InvariantCultureIgnoreCase);
  18. if (!string.IsNullOrWhiteSpace(opts.KeyFile))
  19. {
  20. if (!File.Exists(opts.KeyFile))
  21. return Errors.Error(Errors.ErrorCodes.KeyNotFound);
  22. foreach (var line in File.ReadAllLines(opts.KeyFile))
  23. {
  24. var parts = line.Split(':');
  25. if (parts.Length != 2)
  26. return Errors.Error(Errors.ErrorCodes.InvalidKeyFile);
  27. keys[parts[0].Trim()] = Convert.FromBase64String(parts[1].Trim());
  28. }
  29. }
  30. else if (!string.IsNullOrWhiteSpace(opts.DecryptionKey))
  31. {
  32. keys["*"] = Convert.FromBase64String(opts.DecryptionKey);
  33. }
  34. else if (string.IsNullOrWhiteSpace(opts.ArcDirectory) && string.IsNullOrWhiteSpace(opts.WarcFile))
  35. {
  36. return Errors.Error(Errors.ErrorCodes.KeyNotFound);
  37. }
  38. return 0;
  39. }
  40. }
  41. }