/// <summary>
        ///     Method for getting encrypted or decrypted text from a file.
        /// </summary>
        /// <param name="file">A DOCX or TXT file.</param>
        /// <param name="language">The language in which the key was entered.</param>
        /// <param name="key">Key for encryption or decryption.</param>
        /// <param name="crypt">Mode of text conversion(encryption or decryption).</param>
        /// <returns>Text as a result of encryption or decryption.</returns>
        /// <exception cref="Exception">Error reading the file.</exception>
        public static string GetResultFile(Document file, Cipher.LanguageMode language, string key,
                                           Cipher.CryptMode crypt)
        {
            if (file.GetContent() is byte[] bytesOfDocx)
            {
                using var stream = new MemoryStream();
                var result = new StringBuilder();
                stream.Write(bytesOfDocx, 0, bytesOfDocx.Length);
                using var wordDocument = WordprocessingDocument.Open(stream, true);
                var paragraphs = wordDocument.MainDocumentPart.Document.Body.Elements <Paragraph>();
                foreach (var paragraph in paragraphs)
                {
                    foreach (var run in paragraph.Elements <Run>())
                    {
                        foreach (var text in run.Elements <Text>())
                        {
                            result.AppendLine(new Cipher(text.Text, language, key, crypt).GetResultText());
                        }
                    }
                }

                return(result.ToString());
            }

            if (!(file.GetContent() is string content))
            {
                throw new Exception("The file content was not detected.");
            }
            return(new Cipher(content, language, key, crypt).GetResultText());
        }
Example #2
0
        /// <summary>
        ///     Displays a notification with information about successful encryption or decryption.
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="crypt">Mode of text conversion(encryption or decryption).</param>
        /// <exception cref="ArgumentOutOfRangeException">Unknown conversion mode.</exception>
        public static void ShowSuccessCrypt(this IMatToaster notification, Cipher.CryptMode crypt)
        {
            switch (crypt)
            {
            case Cipher.CryptMode.Decrypt:
                notification.Add("The text has been successfully decrypted.", MatToastType.Success, "Successfully");
                break;

            case Cipher.CryptMode.Encrypt:
                notification.Add("The text has been successfully encrypted.", MatToastType.Success, "Successfully");
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(crypt), crypt, null);
            }
        }
 /// <summary>
 ///     Asynchronous method that returns the result of encrypting or decrypting text.
 /// </summary>
 /// <param name="text">Text that will be encrypted or decrypted.</param>
 /// <param name="language">The language in which the key was entered.</param>
 /// <param name="key">Key for encryption or decryption.</param>
 /// <param name="crypt">Mode of text conversion(encryption or decryption).</param>
 /// <returns>Text as a result of encryption or decryption.</returns>
 public static Task <string> GetResultTextAsync(string text, Cipher.LanguageMode language, string key,
                                                Cipher.CryptMode crypt)
 {
     return(Task.FromResult(new Cipher(text, language, key, crypt).GetResultText()));
 }