Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace MojRandsomeware2
- {
- public partial class Form1 : Form
- {
- [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
- public static extern bool ZeroMemory(IntPtr Destination, int Length);
- public Form1()
- {
- InitializeComponent();
- }
- private void btnBrowseFile_Click(object sender, EventArgs e)
- {
- using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "All Files | *.*" })
- {
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- txtFilePath.Text = ofd.FileName;
- }
- }
- }
- private void FileEncrypt(string inputFileName, string password)
- {
- byte[] passwords = Encoding.UTF8.GetBytes(password);
- byte[] salt = new byte[32];
- RijndaelManaged AES = new RijndaelManaged();
- AES.KeySize = 256;
- AES.BlockSize = 128;
- AES.Padding = PaddingMode.PKCS7;
- var key = new Rfc2898DeriveBytes(password, salt, 50000);
- AES.Key = key.GetBytes(AES.KeySize / 8);
- AES.IV = key.GetBytes(AES.BlockSize / 8);
- AES.Mode = CipherMode.CFB;
- using (FileStream fsCrypt = new FileStream(
- inputFileName + ".aes",
- FileMode.Create))
- {
- fsCrypt.Write(salt, 0, salt.Length);
- using (CryptoStream cs = new CryptoStream(
- fsCrypt,
- AES.CreateEncryptor(),
- CryptoStreamMode.Write))
- {
- using (FileStream fs =new FileStream(
- inputFileName,
- FileMode.Open))
- {
- byte[] buffer = new byte[1048576];
- int read;
- while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
- {
- cs.Write(buffer, 0, read);
- }
- }
- }
- }
- }
- private void btnEncrypt_Click(object sender, EventArgs e)
- {
- GCHandle gCHandle = GCHandle.Alloc(txtPassword.Text, GCHandleType.Pinned);
- FileEncrypt(txtFilePath.Text, txtPassword.Text);
- ZeroMemory(gCHandle.AddrOfPinnedObject(), txtPassword.Text.Length * 2);
- gCHandle.Free();
- MessageBox.Show("Encryption Done!");
- }
- private void FileDecrypt(string inputFileName, string outputFileName, string password)
- {
- byte[] passwrods = Encoding.UTF8.GetBytes(password);
- byte[] salt = new byte[32];
- using(FileStream fsCrypt = new FileStream(
- inputFileName,
- FileMode.Open))
- {
- fsCrypt.Read(salt, 0, salt.Length);
- RijndaelManaged AES = new RijndaelManaged();
- AES.KeySize = 256;
- AES.BlockSize = 128;
- AES.Padding = PaddingMode.PKCS7;
- var key = new Rfc2898DeriveBytes(password, salt, 50000);
- AES.Key = key.GetBytes(AES.KeySize / 8);
- AES.IV = key.GetBytes(AES.BlockSize / 8);
- AES.Mode = CipherMode.CFB;
- using( CryptoStream cs = new CryptoStream(
- fsCrypt,
- AES.CreateDecryptor(),
- CryptoStreamMode.Read))
- {
- using (FileStream fso = new FileStream(
- outputFileName,
- FileMode.Create))
- {
- byte[] buffer = new byte[1048576];
- int read;
- while((read = cs.Read(buffer, 0, buffer.Length)) > 0)
- {
- fso.Write(buffer, 0, read);
- }
- }
- }
- }
- }
- private void btnDecrypt_Click(object sender, EventArgs e)
- {
- GCHandle gCHandle = GCHandle.Alloc(txtPassword.Text, GCHandleType.Pinned);
- FileDecrypt(txtFilePath.Text, txtFilePath.Text.Replace(".aes", ""), txtPassword.Text);
- ZeroMemory(gCHandle.AddrOfPinnedObject(), txtPassword.Text.Length * 2);
- gCHandle.Free();
- MessageBox.Show("Decryption Done!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement