-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.cs
More file actions
65 lines (56 loc) · 2.53 KB
/
encrypt.cs
File metadata and controls
65 lines (56 loc) · 2.53 KB
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
using System;
using System.IO;
using System.Security.Cryptography;
internal class Program
{
public static void Main(string[] args) {
// INTENT: To ensure the operator has only one argument which should be a file.
bool incorrectUsage = args.Length != 1;
if (incorrectUsage)
{
Console.WriteLine("Fuck outta here!");
}
else
{
// INTENT: To accept a file as an argument and change its extension.
FileInfo file = new FileInfo(args[0]);
string sourceFilename = file.Name;
string destinationFilename = Path.ChangeExtension(sourceFilename, ".damnmayne");
/* INTENT: To build the seed; the integer that makes the secret key and the
initialization vector reproducable. */
int num = 1234;
Random seed = new Random(Convert.ToInt32(num));
// INTENT: To build the initialization vector.
byte[] iv = new byte[16];
seed.NextBytes(iv);
// INTENT: To build the secret key.
byte[] key = new byte[32];
seed.NextBytes(key);
// INTENT: TO build the encrypted file.
Program.EncryptFile(sourceFilename, destinationFilename, key, iv);
}
}
// Intent: To build an encryption stream.
private static byte[] EncryptFile(string sourceFile, string destinationFile, byte[] key, byte[] iv) {
using (RijndaelManaged rijndaelManaged = new RijndaelManaged()) {
//Intent: To set up the final encrypted file.
using (FileStream fileStream = new FileStream(destinationFile, FileMode.Create)) {
//Intent: To activate the encryption.
using (ICryptoTransform cryptoTransform = rijndaelManaged.CreateEncryptor(key, iv)) {
using (CryptoStream cryptoStream = new CryptoStream(fileStream, cryptoTransform, CryptoStreamMode.Write)) {
// Intent: To stream in the original file.
using (FileStream fileStream2 = new FileStream(sourceFile, FileMode.Open))
{
byte[] array = new byte[1024];
int x;
while ((x = fileStream2.Read(array, 0, array.Length)) != 0) {
cryptoStream.Write(array, 0, x);
}
}
}
}
}
}
return null;
}
}