public void SymmetricAlgorithmsEncryptionMatches(SymmetricAlgorithms enumValue)
        {
            string toProcess = "`~1234567890-=qwertyuiop[]\\ASDFGHJKL:\"ZXCVBNM<>?ăîșțâ";
            string key       = "{>@#F09\0";

            EncryptText symmetricAlgorithm = new EncryptText
            {
                Algorithm = enumValue,
                Encoding  = new InArgument <Encoding>(ExpressionServices.Convert((env) => System.Text.Encoding.Unicode))
            };
            Dictionary <string, object> arguments = new Dictionary <string, object>();

            arguments.Add(nameof(EncryptText.Input), toProcess);
            arguments.Add(nameof(EncryptText.Key), key);

            WorkflowInvoker invoker        = new WorkflowInvoker(symmetricAlgorithm);
            string          activityString = (string)invoker.Invoke(arguments)[nameof(symmetricAlgorithm.Result)];

            byte[] algorithmBytes = CryptographyHelper.DecryptData(enumValue, Convert.FromBase64String(activityString), Encoding.Unicode.GetBytes(key));

            Assert.Equal(toProcess, Encoding.Unicode.GetString(algorithmBytes));
        }
예제 #2
0
 public static string GetConnectionString(string connectionStringName, string key)
 {
     return(EncryptText.Decryptor(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString, key));
 }
예제 #3
0
        public static int RunEncrypt(Options.Encrypt EncryptOptions)
        {
            int           inError    = 0;
            StringBuilder errMsgText = new StringBuilder();

            if (EncryptOptions.PlainTextString == null)
            {
                EncryptOptions.PlainTextString = string.Empty;
            }
            if (EncryptOptions.PlanTextFile == null)
            {
                EncryptOptions.PlanTextFile = string.Empty;
            }

            if (EncryptOptions.PlainTextString?.Length > 0 && EncryptOptions.PlanTextFile.Length > 0)
            {
                // Cannot use plaintext string and file option at the same time.
                inError++;
                errMsgText.Append($"- Using both --textstring and --textfile options together is not supported.");
            }

            if (EncryptOptions.PlainTextString.Length == 0 && EncryptOptions.PlanTextFile.Length == 0)
            {
                inError++;
                errMsgText.Append($"- Either --textstring OR --textfile option must be provided. You are not providing anything to decrypt!");
            }

            if (EncryptOptions.PlainTextString.Length == 0 && EncryptOptions.PlanTextFile.Length > 0)
            {
                if (!File.Exists(EncryptOptions.PlanTextFile))
                {
                    inError++;
                    errMsgText.Append($"- Provided plain text file, {EncryptOptions.PlanTextFile}, doesn't exist.");
                }
            }

            if (inError > 0)
            {
                Console.Write($"\nThere is a problem with provided parameters (--help for help):\n\n{errMsgText.ToString()}\n");
                return(inError);
            }

            // work time
            string valueToEncrypt;

            if (EncryptOptions.PlanTextFile.Length > 0)
            {
                try
                {
                    valueToEncrypt = File.ReadAllText(EncryptOptions.PlanTextFile);
                } catch (UnauthorizedAccessException)
                {
                    Console.Write($"- Error reading plain text file, unauthorized.\n");
                    return(101);
                } catch (DirectoryNotFoundException)
                {
                    Console.Write($"- Eror reading plain text file, Directory not found!\n");
                    return(102);
                } catch (Exception ex)
                {
                    Console.Write($"- An unknown error has occurred:\n\n{ex.Message}\n\n");
                    return(110);
                }
            }
            else if (EncryptOptions.PlainTextString.Length > 0)
            {
                valueToEncrypt = EncryptOptions.PlainTextString;
            }
            else
            {
                Console.Write($"- PlainTextString and PlanTextFile are empty. We shouldn't be here, must be missing a check!");
                return(111);
            }

            string result;

            try
            {
                result = EncryptText.Encrypt(valueToEncrypt);
            } catch (Exception ex)
            {
                Console.Write($"- Error while encrypting string:\n\n{ex.Message}\n\n");
                return(112);
            }
            Console.Write(result);
            return(0);
        }
예제 #4
0
파일: Main.cs 프로젝트: ycaihua/CurveLock
        private void MenuEncryptTextClicked(object sender, EventArgs e)
        {
            var encryptText = new EncryptText();

            AddPanel(encryptText);
        }