-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
74 lines (66 loc) · 2.37 KB
/
Program.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
using System;
using System.IO;
using Cocona;
using Newtonsoft.Json;
namespace CryptStr
{
class Program
{
private const string DefaultAlgorithms = "TripleDES";
static void Main(string[] args) => CoconaApp.Run<Program>(args);
[Command(Description = "Encrypt string of value.")]
public int Enc(
[Argument] string value,
[Option('k')] string key,
[Option('v')] string iv,
[Option('a')][Algorithms] string algorithms = DefaultAlgorithms
)
{
ICryptor cryptor = algorithms switch
{
nameof(SupportAlgorithms.TripleDES) => new TripleDESCryptor(key, iv),
nameof(SupportAlgorithms.DES) => new DESCryptor(key, iv),
_ => throw new ArgumentException()
};
Console.WriteLine(cryptor.Encrypt(value));
return 0;
}
[Command(Description = "Encrypt string of value.")]
public int Dec(
[Argument] string value,
[Option('k')] string key,
[Option('v')] string iv,
[Option('a')][Algorithms] string algorithms = DefaultAlgorithms
)
{
ICryptor cryptor = algorithms switch
{
nameof(SupportAlgorithms.TripleDES) => new TripleDESCryptor(key, iv),
nameof(SupportAlgorithms.DES) => new DESCryptor(key, iv),
_ => throw new ArgumentException()
};
Console.WriteLine(cryptor.Decrypt(value));
return 0;
}
[Command(Description = "Generate key and IV to file.")]
public void Gen(
[Option('a')][Algorithms] string algorithms = DefaultAlgorithms
)
{
var keyAndIV = algorithms switch
{
nameof(SupportAlgorithms.TripleDES) => TripleDESCryptor.Generate(),
nameof(SupportAlgorithms.DES) => DESCryptor.Generate(),
_ => throw new ArgumentException()
};
File.WriteAllText(
Path.Combine(Directory.GetCurrentDirectory(), "cryptstr.json"),
JsonConvert.SerializeObject(new
{
keyAndIV.Key,
keyAndIV.IV
})
);
}
}
}