-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encrypter.cs
185 lines (164 loc) · 5.41 KB
/
Encrypter.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.ComponentModel;
using System.Diagnostics;
namespace PaJaMa.Common
{
public class EncrypterDecrypter
{
private static EncrypterDecrypter _instance = null;
private bool _cancel = false;
public static EncrypterDecrypter Instance
{
get
{
if (_instance == null) _instance = new EncrypterDecrypter();
return _instance;
}
}
public EncrypterDecrypter()
{
}
public event ProgressChangedEventHandler ProgressChanged;
public event EventHandler Cancelled;
[DebuggerNonUserCode()]
public static byte[] EncryptDecrypt(byte[] data, byte[] Key, byte[] IV, bool decrypt)
{
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms,
(decrypt ? alg.CreateDecryptor() : alg.CreateEncryptor()), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);
cs.Close();
byte[] returnData = ms.ToArray();
return returnData;
}
public void Cancel()
{
_cancel = true;
}
public static string Encrypt(string clearText, string Password)
{
byte[] clearBytes =
System.Text.Encoding.Unicode.GetBytes(clearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
byte[] encryptedData = EncryptDecrypt(clearBytes,
pdb.GetBytes(32), pdb.GetBytes(16), false);
return Convert.ToBase64String(encryptedData);
}
[DebuggerNonUserCode()]
public static string Decrypt(string cipherText, string Password)
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65,
0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
byte[] decryptedData = EncryptDecrypt(cipherBytes,
pdb.GetBytes(32), pdb.GetBytes(16), true);
return System.Text.Encoding.Unicode.GetString(decryptedData);
}
public void EncryptDecryptFile(Stream srcStream, Stream destStream, string password, bool decrypt)
{
CryptoStream cs = null;
try
{
Rijndael cryptic = Rijndael.Create();
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
cryptic.Key = pdb.GetBytes(32);
cryptic.IV = pdb.GetBytes(16);
cs = new CryptoStream(destStream,
(decrypt ? cryptic.CreateDecryptor() : cryptic.CreateEncryptor()), CryptoStreamMode.Write);
int data;
double prog = 0;
int progTot = 0;
while ((data = srcStream.ReadByte()) != -1)
{
if (_cancel)
{
cs.Close();
cs.Dispose();
if (Cancelled != null)
{
Cancelled(this, new EventArgs());
}
return;
}
cs.WriteByte((byte)data);
if (ProgressChanged != null)
{
prog++;
if (Math.Floor((100 * prog) / srcStream.Length) > progTot)
{
progTot = Convert.ToInt16((100 * prog) / srcStream.Length);
ProgressChanged(this, new ProgressChangedEventArgs(progTot,
null));
}
}
}
}
finally
{
cs.Close();
cs.Dispose();
}
}
public void EncryptFile(string srcFile, string destFile, string password)
{
FileStream destStream = new FileStream(destFile, FileMode.Create);
FileStream srcStream = new FileStream(srcFile, FileMode.Open);
EncryptDecryptFile(srcStream, destStream, password, false);
destStream.Close();
srcStream.Close();
}
public void EncryptFile(string srcFile, Stream destStream, string password)
{
FileStream srcStream = new FileStream(srcFile, FileMode.Open);
EncryptDecryptFile(srcStream, destStream, password, false);
srcStream.Close();
}
public void EncryptFile(Stream srcStream, string destFile, string password)
{
FileStream destStream = new FileStream(destFile, FileMode.Create);
EncryptDecryptFile(srcStream, destStream, password, false);
destStream.Close();
}
public void EncryptFile(Stream srcStream, Stream destStream, string password)
{
EncryptDecryptFile(srcStream, destStream, password, false);
}
public void DecryptFile(string srcFile, string destFile, string password)
{
FileStream destStream = new FileStream(destFile, FileMode.Create);
FileStream srcStream = new FileStream(srcFile, FileMode.Open);
EncryptDecryptFile(srcStream, destStream, password, true);
destStream.Close();
srcStream.Close();
}
public void DecryptFile(string srcFile, Stream destStream, string password)
{
FileStream srcStream = new FileStream(srcFile, FileMode.Open);
EncryptDecryptFile(srcStream, destStream, password, true);
srcStream.Close();
}
public void DecryptFile(Stream srcStream, string destFile, string password)
{
FileStream destStream = new FileStream(destFile, FileMode.Create);
EncryptDecryptFile(srcStream, destStream, password, true);
destStream.Close();
}
public void DecryptFile(Stream srcStream, Stream destStream, string password)
{
EncryptDecryptFile(srcStream, destStream, password, true);
}
}
}