Example #1
0
        private static void Encrypt(string secret, string text, string file)
        {
            var passPhrase = secret ?? Environment.GetEnvironmentVariable(KeyId, EnvironmentVariableTarget.User);

            if (passPhrase != null)
            {
                if (text != null)
                {
                    var cipher = CipherService.Encrypt(text, passPhrase);
                    Console.WriteLine($"The cipher is: {cipher}");
                }
                else if (file != null)
                {
                    Console.WriteLine("The ciphers are:");
                    foreach (var line in File.ReadLines(file, Encoding.UTF8))
                    {
                        var cipher = CipherService.Encrypt(line, passPhrase);
                        Console.WriteLine($"{cipher}");
                    }
                }
                else
                {
                    Console.WriteLine("Text to be encrypted was not provided.");
                }
            }
            else
            {
                Console.WriteLine("Something went wrong while encrypting the text. Secret not provided or configured.....");
            }
        }
Example #2
0
        private static void Decrypt(string secret, string cipher, string file)
        {
            try
            {
                var passPhrase = secret ?? Environment.GetEnvironmentVariable(KeyId, EnvironmentVariableTarget.User);

                if (passPhrase != null)
                {
                    if (cipher != null)
                    {
                        var text = CipherService.Decrypt(cipher, passPhrase);
                        Console.WriteLine($"The text is: {text}");
                    }
                    else if (file != null)
                    {
                        Console.WriteLine("The ciphers are:");
                        foreach (var line in File.ReadLines(file, Encoding.UTF8))
                        {
                            var text = CipherService.Decrypt(line, passPhrase);
                            Console.WriteLine($"{text}");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Text to be decrypted was not provided.");
                    }
                }
                else
                {
                    Console.WriteLine("Something went wrong while encrypting the text. Secret not provided or configured.....");
                }
            }
            catch (CryptographicException)
            {
                Console.WriteLine("Something went wrong while decrypting the cipher....");
            }
        }