forked from cryptordev/Cryptor-1.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cryptor.cs
278 lines (247 loc) · 10 KB
/
Cryptor.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace EncryptDecrypt
{
public class MyEncryptor
{
// Internal value of the phrase used to generate the secret key
private string _Phrase = "";
//contains input file path and name
private string _inputFile = "";
//contains output file path and name
private string _outputFile = "";
enum TransformType { ENCRYPT = 0, DECRYPT = 1 }
/// <value>Set the phrase used to generate the secret key.</value>
public string Phrase
{
set
{
this._Phrase = value;
this.GenerateKey(this._Phrase);
}
}
// Internal initialization vector value to
// encrypt/decrypt the first block
private byte[] _IV;
// Internal secret key value
private byte[] _Key;
/// <summary>
/// Constructor
/// </summary>
/// <param name="SecretPhrase">Secret phrase to generate key</param>
public MyEncryptor(string SecretPhrase)
{
this.Phrase = SecretPhrase;
}
/// <summary>
/// Encrypt the given value with the Rijndael algorithm.
/// </summary>
/// <param name="EncryptValue">Value to encrypt</param>
/// <returns>Encrypted value. </returns>
public string Encrypt(string EncryptValue)
{
try
{
if (EncryptValue.Length > 0)
{
// Write the encrypted value into memory
byte[] input = Encoding.UTF8.GetBytes(EncryptValue);
// Retrieve the encrypted value and return it
return (Convert.ToBase64String(Transform(input,TransformType.ENCRYPT)));
}
else
{
return "";
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Decrypt the given value with the Rijndael algorithm.
/// </summary>
/// <param name="DecryptValue">Value to decrypt</param>
/// <returns>Decrypted value. </returns>
public string Decrypt(string DecryptValue)
{
try
{
if (DecryptValue.Length > 0)
{
// Write the encrypted value into memory
byte[] input = Convert.FromBase64String(DecryptValue);
// Retrieve the decrypted value and return it
return (Encoding.UTF8.GetString(Transform(input,TransformType.DECRYPT)));
}
else
{
return "";
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Encrypt the given value with the Rijndael algorithm.
/// </summary>
/// <param name="EncryptValue">Value to encrypt</param>
/// <returns>Encrypted value. </returns>
public void Encrypt(string InputFile, string OutputFile)
{
try
{
if ((InputFile != null) && (InputFile.Length > 0))
{
_inputFile = InputFile;
}
if ((OutputFile != null) && (OutputFile.Length > 0))
{
_outputFile = OutputFile;
}
Transform(null, TransformType.ENCRYPT);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Decrypt the given value with the Rijndael algorithm.
/// </summary>
/// <param name="DecryptValue">Value to decrypt</param>
/// <returns>Decrypted value. </returns>
public void Decrypt(string InputFile, string OutputFile)
{
try
{
if ((InputFile != null) && (InputFile.Length > 0))
{
_inputFile = InputFile;
}
if ((OutputFile != null) && (OutputFile.Length > 0))
{
_outputFile = OutputFile;
}
Transform(null, TransformType.DECRYPT);
}
catch (Exception ex)
{
throw ex;
}
}
/*****************************************************************
* Generate an encryption key based on the given phrase. The
* phrase is hashed to create a unique 32 character (256-bit)
* value, of which 24 characters (192 bit) are used for the
* key and the remaining 8 are used for the initialization
* vector (IV).
*
* Parameters: SecretPhrase - phrase to generate the key and
* IV from.
*
* Return Val: None
***************************************************************/
private void GenerateKey(string SecretPhrase)
{
// Initialize internal values
this._Key = new byte[24];
this._IV = new byte[16];
// Perform a hash operation using the phrase. This will
// generate a unique 32 character value to be used as the key.
byte[] bytePhrase = Encoding.ASCII.GetBytes(SecretPhrase);
SHA384Managed sha384 = new SHA384Managed();
sha384.ComputeHash(bytePhrase);
byte[] result = sha384.Hash;
// Transfer the first 24 characters of the hashed value to the key
// and the remaining 8 characters to the intialization vector.
for (int loop = 0; loop < 24; loop++) this._Key[loop] = result[loop];
for (int loop = 24; loop < 40; loop++) this._IV[loop - 24] = result[loop];
}
/*****************************************************************
* Transform one form to anoter based on CryptoTransform
* It is used to encrypt to decrypt as well as decrypt to encrypt
* Parameters: input [byte array] - which needs to be transform
* transformType - encrypt/decrypt transform
*
* Return Val: byte array - transformed value.
***************************************************************/
private byte[] Transform(byte[] input, TransformType transformType)
{
CryptoStream cryptoStream = null; // Stream used to encrypt
RijndaelManaged rijndael = null; // Rijndael provider
ICryptoTransform rijndaelTransform = null;// Encrypting object
FileStream fsIn = null; //input file
FileStream fsOut = null; //output file
MemoryStream memStream = null; // Stream to contain data
try
{
// Create the crypto objects
rijndael = new RijndaelManaged();
rijndael.Key = this._Key;
rijndael.IV = this._IV;
if (transformType == TransformType.ENCRYPT)
{
rijndaelTransform = rijndael.CreateEncryptor();
}
else
{
rijndaelTransform = rijndael.CreateDecryptor();
}
if ((input != null) && (input.Length > 0))
{
memStream = new MemoryStream();
cryptoStream = new CryptoStream(
memStream, rijndaelTransform, CryptoStreamMode.Write);
cryptoStream.Write(input, 0, input.Length);
cryptoStream.FlushFinalBlock();
return memStream.ToArray();
}
else if ((_inputFile.Length > 0) && (_outputFile.Length > 0))
{
// First we are going to open the file streams
fsIn = new FileStream(_inputFile,
FileMode.Open, FileAccess.Read);
fsOut = new FileStream(_outputFile,
FileMode.OpenOrCreate, FileAccess.Write);
cryptoStream = new CryptoStream(
fsOut, rijndaelTransform, CryptoStreamMode.Write);
// Now will will initialize a buffer and will be
// processing the input file in chunks.
// This is done to avoid reading the whole file (which can be
// huge) into memory.
int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int bytesRead;
do
{
// read a chunk of data from the input file
bytesRead = fsIn.Read(buffer, 0, bufferLen);
// Encrypt it
cryptoStream.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
cryptoStream.FlushFinalBlock();
}
return null;
}
catch (CryptographicException)
{
throw new CryptographicException("Password salah. Mohon ulangi lagi.");
}
finally
{
if (rijndael != null) rijndael.Clear();
if (rijndaelTransform != null) rijndaelTransform.Dispose();
if (cryptoStream != null) cryptoStream.Close();
if (memStream != null) memStream.Close();
if (fsOut != null) fsOut.Close();
if (fsIn != null) fsIn.Close();
}
}
}
}