forked from tejashwikalptaru/csharp-clamAV-antivirus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quarantine.cs
130 lines (118 loc) · 5.13 KB
/
Quarantine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TejashPlayer;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Windows.Forms;
namespace AntiVirus_Project
{
class Quarantine
{
private string loc = string.Empty;
private Encryption enc = new Encryption();
private string password = "NetskyAV_Tejashwi_2014@133$%";
public Quarantine()
{
loc = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
loc = loc + "\\NetSky\\Quarantine\\";
if(!Directory.Exists(loc))
{
Directory.CreateDirectory(loc);
}
}
public List<string> QuarantineItems()
{
List<string> item = new List<string>();
DirectoryInfo d = new DirectoryInfo(loc);
foreach (var file in d.GetFiles("*.*"))
{
item.Add(file.FullName);
}
return item;
}
public string AddQuarantine(string file)
{
string file_name = Path.GetFileName(file);
enc.EncryptFile(file, loc + file_name, password);
return loc + file_name;
}
public void RestoreQuarantine(string file, string loc)
{
string des = loc + "\\" + Path.GetFileName(file);
enc.DecryptFile(file, des, password);
try { File.Delete(file); }
catch { }
}
public void ClearQuarantine()
{
DirectoryInfo d = new DirectoryInfo(loc);
foreach (var file in d.GetFiles("*.*"))
{
try { File.Delete(file.FullName); }
catch { }
}
}
}
class Encryption
{
private readonly byte[] salt = new byte[] { 0x56, 0x47, 0x98, 0x33, 0x88, 0x13, 0x77, 0x10 };
private const int iterations = 1042;
public void DecryptFile(string sourceFilename, string destinationFilename, string password)
{
AesManaged aes = new AesManaged();
aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
aes.KeySize = aes.LegalKeySizes[0].MaxSize;
// NB: Rfc2898DeriveBytes initialization and subsequent calls to GetBytes must be eactly the same, including order, on both the encryption and decryption sides.
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);
aes.Key = key.GetBytes(aes.KeySize / 8);
aes.IV = key.GetBytes(aes.BlockSize / 8);
aes.Mode = CipherMode.CBC;
ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);
using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
{
try
{
using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
source.CopyTo(cryptoStream);
}
}
catch (CryptographicException exception)
{
if (exception.Message == "Padding is invalid and cannot be removed.")
throw new ApplicationException("Universal Microsoft Cryptographic Exception (Not to be believed!)", exception);
else
throw;
}
}
}
}
public void EncryptFile(string sourceFilename, string destinationFilename, string password)
{
AesManaged aes = new AesManaged();
aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
aes.KeySize = aes.LegalKeySizes[0].MaxSize;
// NB: Rfc2898DeriveBytes initialization and subsequent calls to GetBytes must be exactly the same, including order, on both the encryption and decryption sides.
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);
aes.Key = key.GetBytes(aes.KeySize / 8);
aes.IV = key.GetBytes(aes.BlockSize / 8);
aes.Mode = CipherMode.CBC;
ICryptoTransform transform = aes.CreateEncryptor(aes.Key, aes.IV);
using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
{
using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
source.CopyTo(cryptoStream);
}
}
}
}
}
}