예제 #1
0
        private void HandleException(Exception e)
        {
            Logger.LogException(e);
            formFailPopup = new FormPopup(e.Message);
            formFailPopup.ShowDialog();

            //System.Diagnostics.Process.Start(Application.ExecutablePath);

            toRestart = true;
            this.Close();
        }
예제 #2
0
        private void Cipher(int cryptOption)
        {//todo: use properties of a file to determine which encryption used on the file
            CipherBase cipher;

            string inputFileContents  = "";
            string outputFileContents = "";

            string filePath = "";
            string fileName = "";

            string key          = "";
            string cipherChoice = "";

            try
            {
                PathParser.ParseFilePath(inputFileTextBox.Text, out filePath, out fileName);

                key          = keyTextBox.Text;
                cipherChoice = cipherComboBox.Text;

                inputFileContents = File.ReadAllText(filePath + fileName);
            }
            catch (Exception e)
            {
                HandleException(e);
            }

            switch (cipherChoice)
            {
            case nameof(CaesarCipher):    //todo find a better way
                int parsedKeyInt;
                if (!int.TryParse(key, out parsedKeyInt))
                {
                    HandleException(new Exception(KEY_INVALID));
                }
                cipher = new CaesarCipher(parsedKeyInt);
                break;

            case nameof(ColumnarTransposition):
                cipher = new ColumnarTransposition(key);
                break;

            case nameof(XorCipher):
                char parsedKeyChar;
                if (!char.TryParse(key, out parsedKeyChar))
                {
                    HandleException(new Exception(KEY_INVALID));
                }
                cipher = new XorCipher(parsedKeyChar);
                break;

            default:
                cipher = new CaesarCipher(0);
                HandleException(new Exception(CIPHER_INVALID));
                break;
            }

            if (!CheckFileExtension(cryptOption, fileName))
            {
                HandleException(new Exception(FILE_EXT_INVALID));
            }

            switch (cryptOption)
            {
            case (int)CryptOptions.Encrypt:
                outputFileContents = cipher.Encrypt(inputFileContents);
                File.WriteAllText(filePath + fileName + ENCRYPT_EXTENSION, outputFileContents);
                formSuccessPopup = new FormPopup(ENCRYPT_SUCCESS);
                break;

            case (int)CryptOptions.Decrypt:
                outputFileContents = cipher.Decrypt(inputFileContents);
                File.WriteAllText(filePath + fileName.Substring(0, fileName.LastIndexOf(".")), outputFileContents);
                formSuccessPopup = new FormPopup(DECRYPT_SUCCESS);
                break;

            default:
                formSuccessPopup = new FormPopup("");
                HandleException(new Exception(CIPHER_INVALID));
                break;
            }

            formSuccessPopup.ShowDialog();
        }