Advertisement
Mihao

ase

Oct 9th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13.  
  14. namespace MojRandsomeware2
  15. {
  16. public partial class Form1 : Form
  17. {
  18. [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
  19. public static extern bool ZeroMemory(IntPtr Destination, int Length);
  20.  
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. }
  25.  
  26. private void btnBrowseFile_Click(object sender, EventArgs e)
  27. {
  28. using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "All Files | *.*" })
  29. {
  30. if (ofd.ShowDialog() == DialogResult.OK)
  31. {
  32. txtFilePath.Text = ofd.FileName;
  33. }
  34. }
  35. }
  36.  
  37. private void FileEncrypt(string inputFileName, string password)
  38. {
  39. byte[] passwords = Encoding.UTF8.GetBytes(password);
  40. byte[] salt = new byte[32];
  41.  
  42. RijndaelManaged AES = new RijndaelManaged();
  43. AES.KeySize = 256;
  44. AES.BlockSize = 128;
  45. AES.Padding = PaddingMode.PKCS7;
  46.  
  47. var key = new Rfc2898DeriveBytes(password, salt, 50000);
  48. AES.Key = key.GetBytes(AES.KeySize / 8);
  49. AES.IV = key.GetBytes(AES.BlockSize / 8);
  50.  
  51. AES.Mode = CipherMode.CFB;
  52.  
  53. using (FileStream fsCrypt = new FileStream(
  54. inputFileName + ".aes",
  55. FileMode.Create))
  56. {
  57. fsCrypt.Write(salt, 0, salt.Length);
  58.  
  59. using (CryptoStream cs = new CryptoStream(
  60. fsCrypt,
  61. AES.CreateEncryptor(),
  62. CryptoStreamMode.Write))
  63. {
  64. using (FileStream fs =new FileStream(
  65. inputFileName,
  66. FileMode.Open))
  67. {
  68. byte[] buffer = new byte[1048576];
  69. int read;
  70. while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
  71. {
  72. cs.Write(buffer, 0, read);
  73. }
  74. }
  75. }
  76. }
  77.  
  78. }
  79.  
  80. private void btnEncrypt_Click(object sender, EventArgs e)
  81. {
  82. GCHandle gCHandle = GCHandle.Alloc(txtPassword.Text, GCHandleType.Pinned);
  83. FileEncrypt(txtFilePath.Text, txtPassword.Text);
  84. ZeroMemory(gCHandle.AddrOfPinnedObject(), txtPassword.Text.Length * 2);
  85. gCHandle.Free();
  86. MessageBox.Show("Encryption Done!");
  87. }
  88.  
  89. private void FileDecrypt(string inputFileName, string outputFileName, string password)
  90. {
  91. byte[] passwrods = Encoding.UTF8.GetBytes(password);
  92. byte[] salt = new byte[32];
  93.  
  94. using(FileStream fsCrypt = new FileStream(
  95. inputFileName,
  96. FileMode.Open))
  97. {
  98. fsCrypt.Read(salt, 0, salt.Length);
  99.  
  100. RijndaelManaged AES = new RijndaelManaged();
  101. AES.KeySize = 256;
  102. AES.BlockSize = 128;
  103. AES.Padding = PaddingMode.PKCS7;
  104.  
  105. var key = new Rfc2898DeriveBytes(password, salt, 50000);
  106. AES.Key = key.GetBytes(AES.KeySize / 8);
  107. AES.IV = key.GetBytes(AES.BlockSize / 8);
  108.  
  109. AES.Mode = CipherMode.CFB;
  110.  
  111. using( CryptoStream cs = new CryptoStream(
  112. fsCrypt,
  113. AES.CreateDecryptor(),
  114. CryptoStreamMode.Read))
  115. {
  116. using (FileStream fso = new FileStream(
  117. outputFileName,
  118. FileMode.Create))
  119. {
  120. byte[] buffer = new byte[1048576];
  121. int read;
  122. while((read = cs.Read(buffer, 0, buffer.Length)) > 0)
  123. {
  124. fso.Write(buffer, 0, read);
  125. }
  126. }
  127. }
  128. }
  129. }
  130.  
  131. private void btnDecrypt_Click(object sender, EventArgs e)
  132. {
  133. GCHandle gCHandle = GCHandle.Alloc(txtPassword.Text, GCHandleType.Pinned);
  134. FileDecrypt(txtFilePath.Text, txtFilePath.Text.Replace(".aes", ""), txtPassword.Text);
  135. ZeroMemory(gCHandle.AddrOfPinnedObject(), txtPassword.Text.Length * 2);
  136. gCHandle.Free();
  137. MessageBox.Show("Decryption Done!");
  138. }
  139. }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement