Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 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.  
  7. namespace Sifrator
  8. {
  9.  
  10. using System;
  11. using System.IO;
  12. using System.Security.Cryptography;
  13.  
  14. public class AesCipher
  15. {
  16. public static byte[] AES_Encrypt(string output, byte[] bytesToBeEncrypted, byte[] passwordBytes)
  17. {
  18. byte[] encryptedBytes = null;
  19.  
  20. byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  21.  
  22. using (var memoryStream = new MemoryStream())
  23. {
  24. using (RijndaelManaged AES = new RijndaelManaged())
  25. {
  26. AES.KeySize = 256;
  27. AES.BlockSize = 128;
  28.  
  29. var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  30. AES.Key = key.GetBytes(AES.KeySize / 8);
  31. AES.IV = key.GetBytes(AES.BlockSize / 8);
  32.  
  33. AES.Mode = CipherMode.CBC;
  34.  
  35. using (var cs = new CryptoStream(memoryStream, AES.CreateEncryptor(), CryptoStreamMode.Write))
  36. {
  37. byte[] tmp = passwordBytes.Concat(bytesToBeEncrypted).ToArray();
  38. //new byte[passwordBytes.Length + bytesToBeEncrypted.Length];
  39. cs.Write(tmp, 0, tmp.Length);
  40. cs.Close();
  41. }
  42.  
  43. encryptedBytes = memoryStream.ToArray();
  44. }
  45. }
  46.  
  47. return encryptedBytes;
  48. }
  49.  
  50. public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
  51. {
  52. byte[] decryptedBytes = null;
  53.  
  54. // Set your salt here, change it to meet your flavor:
  55. // The salt bytes must be at least 8 bytes.
  56. byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  57.  
  58. using (MemoryStream ms = new MemoryStream())
  59. {
  60. using (RijndaelManaged AES = new RijndaelManaged())
  61. {
  62. AES.KeySize = 256;
  63. AES.BlockSize = 128;
  64.  
  65. var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  66. AES.Key = key.GetBytes(AES.KeySize / 8);
  67. AES.IV = key.GetBytes(AES.BlockSize / 8);
  68.  
  69. AES.Mode = CipherMode.CBC;
  70.  
  71. using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
  72. {
  73. cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
  74. cs.FlushFinalBlock();
  75. cs.Close();
  76. }
  77. decryptedBytes = ms.ToArray();
  78. }
  79. }
  80.  
  81. return decryptedBytes;
  82. }
  83.  
  84. public static void EncryptFile(string filePath)
  85. {
  86.  
  87. byte[] bytesToBeEncrypted = File.ReadAllBytes(filePath);
  88.  
  89. Random rnd = new Random();
  90. byte[] passwordBytesENCR = new byte[2];//Encoding.UTF8.GetBytes(password);
  91. rnd.NextBytes(passwordBytesENCR);
  92.  
  93. // Hash the password with SHA256
  94. passwordBytesENCR = SHA256.Create().ComputeHash(passwordBytesENCR);
  95.  
  96. string fileEncrypted = "crypted_" + filePath.Split('\\')[filePath.Split('\\').Length - 1];
  97. byte[] bytesEncrypted = AES_Encrypt(fileEncrypted, bytesToBeEncrypted, passwordBytesENCR);
  98.  
  99. File.WriteAllBytes(fileEncrypted, bytesEncrypted);
  100. }
  101.  
  102. public static void DecryptFile(string filePath)
  103. {
  104. byte[] bytesToBeDecrypted = File.ReadAllBytes(filePath);
  105. byte[] passwordBytesDECR = new Byte[2]; //Encoding.UTF8.GetBytes(password);
  106.  
  107. for (int i = 0; i < 2; ++i)
  108. passwordBytesDECR[i] = bytesToBeDecrypted[i];
  109.  
  110. passwordBytesDECR = SHA256.Create().ComputeHash(passwordBytesDECR);
  111.  
  112. byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytesDECR);
  113.  
  114. string file = "decrypted_" + filePath.Split('\\')[filePath.Split('\\').Length - 1];
  115. File.WriteAllBytes(file, bytesDecrypted);
  116. }
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement