/DevilStudioCrypter/Program.cs

https://github.com/arvitaly/SimpleWindowsCrypter · C# · 111 lines · 88 code · 11 blank · 12 comment · 2 complexity · e604ec97cf2f5faf8b8310c3e85db662 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. namespace DevilStudioCrypter
  9. {
  10. class Program
  11. {
  12. // Call this function to remove the key from memory after use for security
  13. [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
  14. public static extern bool ZeroMemory(IntPtr Destination, int Length);
  15. // Function to Generate a 64 bits Key.
  16. static string GenerateKey()
  17. {
  18. // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
  19. DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
  20. // Use the Automatically generated key for Encryption.
  21. return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
  22. }
  23. static void EncryptFile(string sInputFilename,
  24. string sOutputFilename,
  25. string sKey)
  26. {
  27. FileStream fsInput = new FileStream(sInputFilename,
  28. FileMode.Open,
  29. FileAccess.Read);
  30. FileStream fsEncrypted = new FileStream(sOutputFilename,
  31. FileMode.Create,
  32. FileAccess.Write);
  33. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  34. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  35. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  36. ICryptoTransform desencrypt = DES.CreateEncryptor();
  37. CryptoStream cryptostream = new CryptoStream(fsEncrypted,
  38. desencrypt,
  39. CryptoStreamMode.Write);
  40. byte[] bytearrayinput = new byte[fsInput.Length];
  41. fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
  42. cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
  43. cryptostream.Close();
  44. fsInput.Close();
  45. fsEncrypted.Close();
  46. }
  47. static void DecryptFile(string sInputFilename,
  48. string sOutputFilename,
  49. string sKey)
  50. {
  51. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  52. //A 64 bit key and IV is required for this provider.
  53. //Set secret key For DES algorithm.
  54. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  55. //Set initialization vector.
  56. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  57. //Create a file stream to read the encrypted file back.
  58. FileStream fsread = new FileStream(sInputFilename,
  59. FileMode.Open,
  60. FileAccess.Read);
  61. //Create a DES decryptor from the DES instance.
  62. ICryptoTransform desdecrypt = DES.CreateDecryptor();
  63. //Create crypto stream set to read and do a
  64. //DES decryption transform on incoming bytes.
  65. CryptoStream cryptostreamDecr = new CryptoStream(fsread,
  66. desdecrypt,
  67. CryptoStreamMode.Read);
  68. //Print the contents of the decrypted file.
  69. StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
  70. fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
  71. fsDecrypted.Flush();
  72. fsDecrypted.Close();
  73. }
  74. static void Main()
  75. {
  76. string[] args = Environment.GetCommandLineArgs();
  77. if (args.Length < 5)
  78. {
  79. Console.WriteLine("Too small arguments");
  80. Environment.Exit(1);
  81. }
  82. string file_source = args[3];
  83. string file_dest = args[4];
  84. string sSecretKey = args[2];
  85. switch (args[1])
  86. {
  87. case "e":
  88. File.WriteAllBytes(file_dest, EncDec.Encrypt(File.ReadAllBytes(file_source), sSecretKey));
  89. break;
  90. case "d":
  91. File.WriteAllBytes(file_dest, EncDec.Decrypt(File.ReadAllBytes(file_source), sSecretKey));
  92. break;
  93. default:
  94. Console.WriteLine("Invalid argument " + args[1]);
  95. Environment.Exit(2);
  96. break;
  97. }
  98. }
  99. }
  100. }