|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Security.Cryptography; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.Windows; |
| 9 | +using System.Windows.Controls; |
| 10 | +using System.Windows.Data; |
| 11 | +using System.Windows.Documents; |
| 12 | +using System.Windows.Input; |
| 13 | +using System.Windows.Media; |
| 14 | +using System.Windows.Media.Imaging; |
| 15 | +using System.Windows.Navigation; |
| 16 | +using System.Windows.Shapes; |
| 17 | + |
| 18 | +namespace Encryption_App |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Interaction logic for MainWindow.xaml |
| 22 | + /// </summary> |
| 23 | + public partial class MainWindow : Window |
| 24 | + { |
| 25 | + List<String> DropDownItems = new List<string> { "Choose Option...", "Cow", "Chicken" }; |
| 26 | + |
| 27 | + |
| 28 | + public MainWindow() |
| 29 | + { |
| 30 | + InitializeComponent(); |
| 31 | + DropDown.ItemsSource = DropDownItems; |
| 32 | + DropDown.SelectedIndex = 0; |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + private void MenuItem_Click(object sender, RoutedEventArgs e) |
| 37 | + { |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + private void CheckBox_Click(object sender, RoutedEventArgs e) |
| 42 | + { |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + private void FilePath_Click(object sender, RoutedEventArgs e) |
| 47 | + { |
| 48 | + Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog |
| 49 | + { |
| 50 | + InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) |
| 51 | + }; |
| 52 | + |
| 53 | + bool? result = openFileDialog.ShowDialog(); |
| 54 | + |
| 55 | + if (result == true) |
| 56 | + { |
| 57 | + DFileTxtBox.Text = openFileDialog.FileName; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void Button_Click(object sender, RoutedEventArgs e) |
| 62 | + { |
| 63 | + Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog |
| 64 | + { |
| 65 | + InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) |
| 66 | + }; |
| 67 | + |
| 68 | + bool? result = openFileDialog.ShowDialog(); |
| 69 | + |
| 70 | + if(result == true) |
| 71 | + { |
| 72 | + FileTxtBox.Text = openFileDialog.FileName; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private void Encrypt_Click(object sender, RoutedEventArgs e) |
| 77 | + { |
| 78 | + string pwd = InpTxtBox.Text; |
| 79 | + string filePath = FileTxtBox.Text; |
| 80 | + string cryptFilePath = filePath + ".crypt"; |
| 81 | + |
| 82 | + // Set your salt here, change it to meet your flavor: |
| 83 | + // The salt bytes must be at least 8 bytes. |
| 84 | + byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 85 | + using (var inFile = File.OpenRead(filePath)) |
| 86 | + { |
| 87 | + using (var file = File.Open(cryptFilePath, FileMode.Create)) |
| 88 | + { |
| 89 | + using (AesManaged AES = new AesManaged()) |
| 90 | + { |
| 91 | + AES.KeySize = 256; |
| 92 | + AES.BlockSize = 128; |
| 93 | + |
| 94 | + var key = new Rfc2898DeriveBytes(pwd, saltBytes, 1000); |
| 95 | + AES.Key = key.GetBytes(AES.KeySize / 8); |
| 96 | + AES.IV = key.GetBytes(AES.BlockSize / 8); |
| 97 | + |
| 98 | + AES.Mode = CipherMode.CBC; |
| 99 | + byte[] bArray = new byte[16]; |
| 100 | + using (var cs = new CryptoStream(file, AES.CreateEncryptor(), CryptoStreamMode.Write)) |
| 101 | + { |
| 102 | + using (var br = new BinaryReader(inFile)) |
| 103 | + { |
| 104 | + long fileSize = new FileInfo(filePath).Length; |
| 105 | + for (int count = 0; count < Math.Floor((decimal)fileSize/16); count++) |
| 106 | + { |
| 107 | + bArray = br.ReadBytes((int)count*16); |
| 108 | + cs.Write(bArray, 0, bArray.Length); |
| 109 | + cs.Flush(); |
| 110 | + } |
| 111 | + cs.Close(); |
| 112 | + MessageBox.Show("Successfully Encrypted"); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + private void Decrypt_Click(object sender, RoutedEventArgs e) |
| 122 | + { |
| 123 | + string pwd = PwdTxtBox.Text; |
| 124 | + string filePath = DFileTxtBox.Text; |
| 125 | + string cryptFilePath = filePath + ".crypt"; |
| 126 | + |
| 127 | + // Set your salt here, change it to meet your flavor: |
| 128 | + // The salt bytes must be at least 8 bytes. |
| 129 | + byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 130 | + using (var inFile = File.OpenRead(filePath)) |
| 131 | + { |
| 132 | + using (var file = File.Open(cryptFilePath, FileMode.Create)) |
| 133 | + { |
| 134 | + using (AesManaged AES = new AesManaged()) |
| 135 | + { |
| 136 | + //AES.KeySize = 256; |
| 137 | + AES.BlockSize = 128; |
| 138 | + AES.Padding = PaddingMode.PKCS7; |
| 139 | + |
| 140 | + var key = new Rfc2898DeriveBytes(pwd, saltBytes, 1000); |
| 141 | + AES.Key = key.GetBytes(AES.KeySize / 8); |
| 142 | + AES.IV = key.GetBytes(AES.BlockSize / 8); |
| 143 | + |
| 144 | + AES.Mode = CipherMode.CBC; |
| 145 | + byte[] bArray = new byte[16]; |
| 146 | + using (var cs = new CryptoStream(file, AES.CreateDecryptor(), CryptoStreamMode.Write)) |
| 147 | + { |
| 148 | + using (var br = new BinaryReader(inFile)) |
| 149 | + { |
| 150 | + long fileSize = new FileInfo(filePath).Length; |
| 151 | + for (int count = 0; count < Math.Floor((decimal)fileSize / 16); count++) |
| 152 | + { |
| 153 | + bArray = br.ReadBytes((int)count * 16); |
| 154 | + cs.Write(bArray, 0, bArray.Length); |
| 155 | + cs.Flush(); |
| 156 | + } |
| 157 | + cs.Close(); |
| 158 | + MessageBox.Show("Successfully Encrypted"); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + } |
| 167 | + } |
0 commit comments