Advertisement
Guest User

Untitled

a guest
Apr 14th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Security.Cryptography;
  7. using System.Runtime.InteropServices;
  8. using System.Text.RegularExpressions;
  9. using System.Object;
  10.  
  11. namespace ConsoleApplication1
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. private void EncryptFile(string inputFile, string outputFile)
  18. {
  19.  
  20. try
  21. {
  22. string password = @"myKey123"; // Your Key Here
  23. UnicodeEncoding UE = new UnicodeEncoding();
  24. byte[] key = UE.GetBytes(password);
  25.  
  26. string cryptFile = outputFile;
  27. FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
  28.  
  29. RijndaelManaged RMCrypto = new RijndaelManaged();
  30.  
  31. CryptoStream cs = new CryptoStream(fsCrypt,
  32. RMCrypto.CreateEncryptor(key, key),
  33. CryptoStreamMode.Write);
  34.  
  35. FileStream fsIn = new FileStream(inputFile, FileMode.Open);
  36.  
  37. int data;
  38. while ((data = fsIn.ReadByte()) != -1)
  39. cs.WriteByte((byte)data);
  40.  
  41.  
  42. fsIn.Close();
  43. cs.Close();
  44. fsCrypt.Close();
  45. }
  46. catch
  47. {
  48. MessageBox.Show("Encryption failed!", "Error");
  49. }
  50. }
  51. ///<summary>
  52. /// Steve Lydford - 12/05/2008.
  53. ///
  54. /// Decrypts a file using Rijndael algorithm.
  55. ///</summary>
  56. ///<param name="inputFile"></param>
  57. ///<param name="outputFile"></param>
  58. private void DecryptFile(string inputFile, string outputFile)
  59. {
  60.  
  61. {
  62. string password = @"myKey123"; // Your Key Here
  63.  
  64. UnicodeEncoding UE = new UnicodeEncoding();
  65. byte[] key = UE.GetBytes(password);
  66.  
  67. FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
  68.  
  69. RijndaelManaged RMCrypto = new RijndaelManaged();
  70.  
  71. CryptoStream cs = new CryptoStream(fsCrypt,
  72. RMCrypto.CreateDecryptor(key, key),
  73. CryptoStreamMode.Read);
  74.  
  75. FileStream fsOut = new FileStream(outputFile, FileMode.Create);
  76.  
  77. int data;
  78. while ((data = cs.ReadByte()) != -1)
  79. fsOut.WriteByte((byte)data);
  80.  
  81. fsOut.Close();
  82. cs.Close();
  83. fsCrypt.Close();
  84.  
  85. }
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement