From 071ced5be2bbb0c29c4ceb4e5757edf416231550 Mon Sep 17 00:00:00 2001 From: vijayshinva Date: Tue, 17 Nov 2020 13:53:40 +0530 Subject: [PATCH] Base64Url --- Kryptos/Kryptos/Base64Url.cs | 38 +++++++ Kryptos/Kryptos/Kryptos.csproj | 4 +- Kryptos/Kryptos/Program.cs | 1 + Kryptos/Kryptos/WireUpBase64UrlExtensions.cs | 110 +++++++++++++++++++ Kryptos/Kryptos/WireUpUuidExtensions.cs | 3 - 5 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 Kryptos/Kryptos/Base64Url.cs create mode 100644 Kryptos/Kryptos/WireUpBase64UrlExtensions.cs diff --git a/Kryptos/Kryptos/Base64Url.cs b/Kryptos/Kryptos/Base64Url.cs new file mode 100644 index 0000000..21d036e --- /dev/null +++ b/Kryptos/Kryptos/Base64Url.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; + +namespace Kryptos +{ + public class Base64Url + { + private static readonly char base64PadCharacter = '='; + private static readonly string doubleBase64PadCharacter = "=="; + private static readonly char base64Character62 = '+'; + private static readonly char base64Character63 = '/'; + private static readonly char base64UrlCharacter62 = '-'; + private static readonly char base64UrlCharacter63 = '_'; + public static string Encode(string text) + { + return Convert.ToBase64String(Encoding.UTF8.GetBytes(text)) + .Split(base64PadCharacter)[0] + .Replace(base64Character62, base64UrlCharacter62) + .Replace(base64Character63, base64UrlCharacter63); + } + public static string Decode(string text) + { + text = text.Replace(base64UrlCharacter62, base64Character62) + .Replace(base64UrlCharacter63, base64Character63); + + text += (text.Length % 4) switch + { + 2 => doubleBase64PadCharacter, + 3 => base64PadCharacter, + _ => throw new FormatException("Base64Url encoded string is not good."), + }; + + return Encoding.UTF8.GetString(Convert.FromBase64String(text)); + } + } +} diff --git a/Kryptos/Kryptos/Kryptos.csproj b/Kryptos/Kryptos/Kryptos.csproj index be1f543..b95b8fa 100644 --- a/Kryptos/Kryptos/Kryptos.csproj +++ b/Kryptos/Kryptos/Kryptos.csproj @@ -3,10 +3,10 @@ Exe netcoreapp3.1 - 0.0.2 + 0.0.3 Vijayshinva Karnure A .NET core tool for cryptography. - (c) 2020 Vijayshinva Karnure + 2020 Vijayshinva Karnure LICENSE https://github.com/vijayshinva/kryptos https://github.com/vijayshinva/kryptos diff --git a/Kryptos/Kryptos/Program.cs b/Kryptos/Kryptos/Program.cs index 420e6cd..89e38f3 100644 --- a/Kryptos/Kryptos/Program.cs +++ b/Kryptos/Kryptos/Program.cs @@ -15,6 +15,7 @@ static async Task Main(string[] args) rootCommand.WireUpUuidCommands() .WireUpBase64Commands() + .WireUpBase64UrlCommands() .WireUpMd5Commands() .WireUpSha1Commands() .WireUpSha256Commands() diff --git a/Kryptos/Kryptos/WireUpBase64UrlExtensions.cs b/Kryptos/Kryptos/WireUpBase64UrlExtensions.cs new file mode 100644 index 0000000..07050aa --- /dev/null +++ b/Kryptos/Kryptos/WireUpBase64UrlExtensions.cs @@ -0,0 +1,110 @@ +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.Text; + +namespace Kryptos +{ + public static class WireUpBase64UrlExtensions + { + public static RootCommand WireUpBase64UrlCommands(this RootCommand rootCommand) + { + var base64UrlCommand = new Command("base64url", "Base64 URL"); + var base64UrlEncCommand = new Command("encode", "Encode"); + base64UrlEncCommand.AddAlias("enc"); + base64UrlEncCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text") + { + Argument = new Argument("text") + }); + base64UrlEncCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") + { + Argument = new Argument("input") + }); + base64UrlEncCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") + { + Argument = new Argument("output") + }); + base64UrlEncCommand.Handler = CommandHandler.Create(async (text, input, output, console) => + { + try + { + string base64UrlEncodedText = null; + + if (text != null) + { + base64UrlEncodedText = Base64Url.Encode(text); + } + if (input != null) + { + base64UrlEncodedText = Base64Url.Encode(File.ReadAllText(input.FullName)); + } + + if (output == null) + { + console.Out.WriteLine(base64UrlEncodedText); + } + else + { + File.WriteAllText(output.FullName, base64UrlEncodedText); + } + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + } + }); + var base64UrlDecCommand = new Command("decode", "Decode"); + base64UrlDecCommand.AddAlias("dec"); + base64UrlDecCommand.AddOption(new Option(new string[] { "--text", "-t" }, "Input Text") + { + Argument = new Argument("text") + }); + base64UrlDecCommand.AddOption(new Option(new string[] { "--input", "-i" }, "Input file path") + { + Argument = new Argument("input") + }); + base64UrlDecCommand.AddOption(new Option(new string[] { "--output", "-o" }, "Output file path") + { + Argument = new Argument("output") + }); + base64UrlDecCommand.Handler = CommandHandler.Create(async (text, input, output, console) => + { + try + { + string base64UrlDecodedText = null; + + if (text != null) + { + base64UrlDecodedText = Base64Url.Decode(text); + } + if (input != null) + { + base64UrlDecodedText = Base64Url.Decode(File.ReadAllText(input.FullName)); + } + + if (output == null) + { + console.Out.WriteLine(base64UrlDecodedText); + } + else + { + File.WriteAllText(output.FullName, base64UrlDecodedText); + } + } + catch (Exception ex) + { + console.Out.WriteLine(ex.Message); + } + }); + base64UrlCommand.Add(base64UrlEncCommand); + base64UrlCommand.Add(base64UrlDecCommand); + rootCommand.AddCommand(base64UrlCommand); + + return rootCommand; + } + } +} diff --git a/Kryptos/Kryptos/WireUpUuidExtensions.cs b/Kryptos/Kryptos/WireUpUuidExtensions.cs index e86553c..e05a344 100644 --- a/Kryptos/Kryptos/WireUpUuidExtensions.cs +++ b/Kryptos/Kryptos/WireUpUuidExtensions.cs @@ -49,9 +49,6 @@ public static RootCommand WireUpUuidCommands(this RootCommand rootCommand) { console.Out.WriteLine(ex.Message); } - finally - { - } }); rootCommand.AddCommand(guidCommand);