diff --git a/src/Security.Console/Infrastructure/EntryPoint.cs b/src/Security.Console/Infrastructure/EntryPoint.cs index 40363f6..f78223f 100644 --- a/src/Security.Console/Infrastructure/EntryPoint.cs +++ b/src/Security.Console/Infrastructure/EntryPoint.cs @@ -13,7 +13,7 @@ public static class EntryPoint { public static int Main(string[] args) { - return Parser.Default.ParseArguments(args) + return Parser.Default.ParseArguments(args) .MapResult( (GenerateKeyOptions options) => GenerateKey(options), (SplitSecretOptions options) => SplitSecret(options), @@ -21,6 +21,7 @@ public static int Main(string[] args) (ToBase64Options options) => ToBase64(options), (FromBase64Options options) => FromBase64(options), (EncryptOptions options) => Encrypt(options), + (DecryptOptions options) => Decrypt(options), _ => 1); } @@ -38,7 +39,26 @@ private static int Encrypt(EncryptOptions options) var encrypted = cipher.Encrypt(data, key, iv, salt); - Output($"\n Encrypted data: {Convert.ToBase64String(encrypted)}"); + Output($"\n Encrypted data: {Convert.ToBase64String(encrypted)}\n"); + + return 0; + } + + private static int Decrypt(DecryptOptions options) + { + var key = Convert.FromBase64String(options.Key); + + var iv = Convert.FromBase64String(options.Iv); + + var salt = Convert.FromBase64String(options.Salt); + + var secret = Convert.FromBase64String(options.Data); + + var cipher = new SymmetricCipher(); + + var encrypted = cipher.Decrypt(secret, key, iv, salt); + + Output($"\n Decrypted data: {Convert.ToBase64String(encrypted)}\n"); return 0; } @@ -57,7 +77,7 @@ private static int GenerateKey(GenerateKeyOptions options) rng.GetBytes(bytes); - Output($"\n Key: {Convert.ToBase64String(bytes)}"); + Output($"\n Key: {Convert.ToBase64String(bytes)}\n"); return 0; } @@ -105,7 +125,7 @@ private static int SplitSecret(SplitSecretOptions options) Output($" {i + 1, 2}: {Convert.ToBase64String(parts[i])}"); } - Output($"\n Any {options.Minimum} of these can be combined to obtain the original secret."); + Output($"\n Any {options.Minimum} of these can be combined to obtain the original secret.\n"); return 0; } @@ -151,7 +171,7 @@ private static int Combine(CombineOptions options) var secret = joiner.Join(parts); - Output($"\n Secret: {Convert.ToBase64String(secret)}"); + Output($"\n Secret: {Convert.ToBase64String(secret)}\n"); return 0; } @@ -165,7 +185,7 @@ private static int ToBase64(ToBase64Options options) } Output($"\n Original text: {options.Text}"); - Output($"\n Base 64: {Convert.ToBase64String(Encoding.Unicode.GetBytes(options.Text))}"); + Output($"\n Base 64: {Convert.ToBase64String(Encoding.Unicode.GetBytes(options.Text))}\n"); return 0; } @@ -184,14 +204,14 @@ private static int FromBase64(FromBase64Options options) return 0; } - Output($"\n Original text: {Encoding.Unicode.GetString(bytes)}"); + Output($"\n Original text: {Encoding.Unicode.GetString(bytes)}\n"); return 0; } private static void Error(string message) { - System.Console.WriteLine($"ERROR:\n {message}"); + System.Console.WriteLine($"ERROR:\n {message}\n"); } private static void Output(string message) diff --git a/src/Security.Console/Infrastructure/Settings/DecryptOptions.cs b/src/Security.Console/Infrastructure/Settings/DecryptOptions.cs new file mode 100644 index 0000000..f5f8b4e --- /dev/null +++ b/src/Security.Console/Infrastructure/Settings/DecryptOptions.cs @@ -0,0 +1,19 @@ +using CommandLine; + +namespace Security.Console.Infrastructure.Settings; + +[Verb("Decrypt", HelpText = "Decrypt some data.")] +public class DecryptOptions +{ + [Option('k', "Key", Required = true, HelpText = "Base 64 encoded key.")] + public string Key { get; set; } + + [Option('i', "IV", Required = true, HelpText = "Base 64 encoded initialisation vector.")] + public string Iv { get; set; } + + [Option('s', "Salt", Required = true, HelpText = "Base 64 encoded salt.")] + public string Salt { get; set; } + + [Option('d', "Data", Required = true, HelpText = "Base 64 encoded secret to be decrypted.")] + public string Data { get; set; } +} \ No newline at end of file