Esempio n. 1
0
        private void AESDecrypt(string cipherText, string key, bool readFromFile = false, bool printToFile = false)
        {
            if (readFromFile && srcStreamReader != null)
            {
                cipherText = srcStreamReader.ReadToEnd();
            }
            if (cipherText.Length <= 0)
            {
                throw new Exception("No text found");
            }
            AESCryptor cryptor = new AESCryptor(getData(cipherText), key);

            cryptor.Decrypt();

            if (printToFile)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog()
                {
                    Filter = "Text Documents(*.txt)|*.txt"
                };
                if (saveFileDialog.ShowDialog() == true)
                {
                    File.WriteAllText(saveFileDialog.FileName, Encoding.Unicode.GetString(cryptor.PlainText));
                }
                else
                {
                    throw new Exception("Unfinished execution. Terminating proces");
                }
            }
            else
            {
                dstText.AppendText(Encoding.Unicode.GetString(cryptor.PlainText));
            }
        }
Esempio n. 2
0
        private void AESEncrypt(string plaintext, string key, bool readFromFile = false, bool printToFile = false)
        {
            if (readFromFile && srcStreamReader != null)
            {
                plaintext = srcStreamReader.ReadToEnd();
            }
            if (plaintext.Length <= 0)
            {
                throw new Exception("No text found");
            }

            AESCryptor cryptor = new AESCryptor(plaintext, key);

            cryptor.Encrypt();

            if (printToFile)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog()
                {
                    Filter = "Text Documents(*.txt)|*.txt"
                };
                if (saveFileDialog.ShowDialog() == true)
                {
                    File.WriteAllText(saveFileDialog.FileName, BitConverter.ToString(cryptor.EncryptedMessage));
                }
                else
                {
                    throw new Exception("Unfinished execution. Terminating proces");
                }
            }
            else
            {
                dstText.AppendText(BitConverter.ToString(cryptor.EncryptedMessage));
            }
        }