Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehjohn committed Jan 13, 2024
1 parent 71b52eb commit 7c57a92
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/Security.Console/Infrastructure/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ public static class EntryPoint
{
public static int Main(string[] args)
{
return Parser.Default.ParseArguments<GenerateKeyOptions, SplitSecretOptions, CombineOptions, ToBase64Options, FromBase64Options, EncryptOptions>(args)
return Parser.Default.ParseArguments<GenerateKeyOptions, SplitSecretOptions, CombineOptions, ToBase64Options, FromBase64Options, EncryptOptions, DecryptOptions>(args)
.MapResult(
(GenerateKeyOptions options) => GenerateKey(options),
(SplitSecretOptions options) => SplitSecret(options),
(CombineOptions options) => Combine(options),
(ToBase64Options options) => ToBase64(options),
(FromBase64Options options) => FromBase64(options),
(EncryptOptions options) => Encrypt(options),
(DecryptOptions options) => Decrypt(options),
_ => 1);
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions src/Security.Console/Infrastructure/Settings/DecryptOptions.cs
Original file line number Diff line number Diff line change
@@ -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; }
}

0 comments on commit 7c57a92

Please sign in to comment.