diff --git a/Kryptos/Kryptos/Program.cs b/Kryptos/Kryptos/Program.cs index eec838a..6538094 100644 --- a/Kryptos/Kryptos/Program.cs +++ b/Kryptos/Kryptos/Program.cs @@ -27,8 +27,9 @@ static async Task Main(string[] args) .WireUpHmacSha256Commands() .WireUpHmacSha384Commands() .WireUpHmacSha512Commands() - .WireUpSriCommands(). - WireUpOidCommands(); + .WireUpSriCommands() + .WireUpOidCommands() + .WireUpPfxCommands(); return await rootCommand.InvokeAsync(args); } diff --git a/Kryptos/Kryptos/WireUpBase64Extensions.cs b/Kryptos/Kryptos/WireUpBase64Extensions.cs index 6310746..367660f 100644 --- a/Kryptos/Kryptos/WireUpBase64Extensions.cs +++ b/Kryptos/Kryptos/WireUpBase64Extensions.cs @@ -74,18 +74,10 @@ public static RootCommand WireUpBase64Commands(this RootCommand rootCommand) }); var base64DecCommand = new Command("decode", "Decode"); base64DecCommand.AddAlias("dec"); - base64DecCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text") - { - Argument = new Argument("text") - }); - base64DecCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") - { - Argument = new Argument("input") - }); - base64DecCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") - { - Argument = new Argument("output") - }); + base64DecCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text")); + base64DecCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path")); + base64DecCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path")); + base64DecCommand.Handler = CommandHandler.Create(async (text, input, output, console) => { Stream outputStream = null; diff --git a/Kryptos/Kryptos/WireUpPfxExtensions.cs b/Kryptos/Kryptos/WireUpPfxExtensions.cs new file mode 100644 index 0000000..ef3c1ec --- /dev/null +++ b/Kryptos/Kryptos/WireUpPfxExtensions.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.CommandLine.IO; +using System.IO; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Text; + +namespace Kryptos +{ + public static class WireUpPfxExtensions + { + public static RootCommand WireUpPfxCommands(this RootCommand rootCommand) + { + var pfxCommand = new Command("pfx", "PFX, PKCS #12"); + var pfxEncCommand = new Command("encrypt", "Encrypt"); + pfxEncCommand.AddAlias("enc"); + pfxEncCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text")); + pfxEncCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path")); + pfxEncCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path")); + pfxEncCommand.AddOption(new Option(new string[] { "--certinput", "-ci" }, "Certificate file path (.pfx)")); + pfxEncCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text")); + + pfxEncCommand.Handler = CommandHandler.Create(async (text, input, output, certInput, keyText, console) => + { + try + { + using var certificate = new X509Certificate2(certInput.FullName, keyText); + using var rsa = certificate.GetRSAPublicKey(); + + if (input != null) + { + text = await File.ReadAllTextAsync(input.FullName).ConfigureAwait(false); + } + var encryptedText = rsa.Encrypt(Encoding.UTF8.GetBytes(text), RSAEncryptionPadding.Pkcs1); + if (output == null) + { + console.Out.WriteLine(Convert.ToBase64String(encryptedText)); + } + else + { + var outputStream = output.OpenWrite(); + await outputStream.WriteAsync(encryptedText).ConfigureAwait(false); + } + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + return 22; + } + return 0; + }); + + var pfxDecCommand = new Command("decrypt", "Decrypt"); + pfxDecCommand.AddAlias("dec"); + pfxDecCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text")); + pfxDecCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path")); + pfxDecCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path")); + pfxDecCommand.AddOption(new Option(new string[] { "--certinput", "-ci" }, "Certificate file path (.pfx)")); + pfxDecCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text")); + + pfxDecCommand.Handler = CommandHandler.Create(async (text, input, output, certInput, keyText, console) => + { + try + { + using var certificate = new X509Certificate2(certInput.FullName, keyText, X509KeyStorageFlags.EphemeralKeySet); + using var rsa = certificate.GetRSAPrivateKey(); + + var textBytes = Convert.FromBase64String(text); + if (input != null) + { + textBytes = await File.ReadAllBytesAsync(input.FullName).ConfigureAwait(false); + } + var decryptedText = rsa.Decrypt(textBytes, RSAEncryptionPadding.Pkcs1); + if (output == null) + { + console.Out.WriteLine(Encoding.UTF8.GetString(decryptedText)); + } + else + { + using var outputStream = output.OpenWrite(); + await outputStream.WriteAsync(decryptedText).ConfigureAwait(false); + } + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + return 22; + } + return 0; + }); + + var pfxInfoCommand = new Command("info", "Information"); + pfxInfoCommand.AddAlias("info"); + pfxInfoCommand.AddOption(new Option(new string[] { "--certinput", "-ci" }, "Certificate file path (.pfx)")); + pfxInfoCommand.AddOption(new Option(new string[] { "--keytext", "-kt" }, "Key Text")); + + pfxInfoCommand.Handler = CommandHandler.Create((certInput, keyText, console) => + { + try + { + using var certificate = new X509Certificate2(certInput.FullName, keyText); + console.Out.WriteLine($"Thumbprint\t: {certificate.Thumbprint}"); + console.Out.WriteLine($"Subject\t\t: {certificate.Subject}"); + console.Out.WriteLine($"FriendlyName\t: {certificate.FriendlyName}"); + console.Out.WriteLine($"Issuer\t\t: {certificate.Issuer}"); + console.Out.WriteLine($"NotAfter\t: {certificate.NotAfter:dd-MMM-yyyy}"); + console.Out.WriteLine($"NotBefore\t: {certificate.NotBefore:dd-MMM-yyyy}"); + console.Out.WriteLine($"HasPrivateKey\t: {certificate.HasPrivateKey}"); + console.Out.Write($"Oid\t\t: {certificate.PublicKey.Oid.FriendlyName}"); + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + return 22; + } + return 0; + }); + + pfxCommand.Add(pfxEncCommand); + pfxCommand.Add(pfxDecCommand); + pfxCommand.Add(pfxInfoCommand); + rootCommand.AddCommand(pfxCommand); + + return rootCommand; + } + } +}