Exemple #1
0
        /// <summary>
        /// The command method to decrypt the given data
        /// </summary>
        private void Decrypt()
        {
            byte[] encrypted;
            byte[] decrypted;

            //Decryption sequence differs according to the selected data format
            switch (DataInput.DataFormatSelected)
            {
            //Decrypt file
            case Format.File:
                //Get the encrypted file bytes
                encrypted = ByteConvert.FileToBytes(EncryptedFilePath);

                //Decrypt
                decrypted = KeyPairSetup.Decrypt(encrypted);

                //true if decrypted not null
                if (decrypted != null)
                {
                    //Create an encrypted file path
                    DecryptedFilePath = DataInput.GetDecryptedFilePath(EncryptedFilePath);

                    //Write decrypted bytes to file
                    File.WriteAllBytes(DecryptedFilePath, decrypted);
                }
                break;

            //Decrypt a text
            case Format.Text:
                //Convert the encrypted hex string to bytes
                encrypted = ByteConvert.HexStringToBytes(EncryptedText);

                //Decrypt as text
                DecryptedText = KeyPairSetup.DecryptToText(encrypted);
                break;

            //Decrypt a hex value
            case Format.Hex:
                //Convert the encrypted hex string to bytes
                encrypted = ByteConvert.HexStringToBytes(EncryptedText);

                //Decrypt
                decrypted = KeyPairSetup.Decrypt(encrypted);

                //true if decrypted not null
                if (decrypted != null)
                {
                    //Convert decrypted bytes to hex string
                    DecryptedText = ByteConvert.BytesToHexString(decrypted);
                }
                break;
            }
        }
        /// <summary>
        /// transforms the data to bytes according to the selected format option
        /// </summary>
        /// <param name="format">The format option to use for byte extraction</param>
        /// <param name="data">The data to extract the bytes from</param>
        /// <returns>the extracted bytes</returns>
        private byte[] GetBytesAccordingToFormatSelected(Format format, string data)
        {
            byte[] bytes = null;
            switch (format)
            {
            case Format.File:
                bytes = ByteConvert.FileToBytes(data);
                break;

            case Format.Text:
                bytes = ByteConvert.StringToAsciiBytes(data);
                break;

            case Format.Hex:
                bytes = ByteConvert.HexStringToBytes(data);
                break;

            default:
                Debugger.Break();
                break;
            }
            return(bytes);
        }
        /// <summary>
        /// The command method to decrypt the given data
        /// </summary>
        private void Decrypt()
        {
            //field for saving the encrypted and decrypted byte arrays
            byte[] encrypted;
            byte[] decryptedBytes;

            //Convert the secret key and iv to byte arrays
            var secretKey = ByteConvert.HexStringToBytes(SecretKey);
            var iv        = ByteConvert.HexStringToBytes(IV);

            try
            {
                //Decrypt the data according to the selected file format
                switch (DataInput.DataFormatSelected)
                {
                //Decrypt to a regular string
                case Format.Text:

                    //Convert the text string to a byte array
                    encrypted = ByteConvert.HexStringToBytes(EncryptedText);

                    //Decrypt the byte array to a text string
                    DecryptedText = SelectedCipherApi.DecryptToText(SelectedAlgorithim, SelectedKeySize, secretKey, iv, encrypted);


                    break;

                case Format.Hex:

                    //Convert the hex string to a byte array
                    encrypted = ByteConvert.HexStringToBytes(EncryptedText);

                    //Decrypt the byte array to a decrypted byte array
                    decryptedBytes = SelectedCipherApi.DecryptToBytes(SelectedAlgorithim, SelectedKeySize, secretKey, iv, encrypted);

                    //Convert the decrypted byte array to a hex string
                    DecryptedText = ByteConvert.BytesToHexString(decryptedBytes);
                    break;

                case Format.File:

                    //Get the encrypted file as a byte array
                    encrypted = ByteConvert.FileToBytes(EncryptedFilePath);

                    //Decrypt the byte array to a decrypted byte array
                    decryptedBytes = SelectedCipherApi.DecryptToBytes(SelectedAlgorithim, SelectedKeySize, secretKey, iv, encrypted);

                    //Create a new file name with the encrypted file path and the "Decrypted" text
                    DecryptedFilePath = Path.Combine(Directory.GetParent(EncryptedFilePath).ToString(), Path.GetFileName(EncryptedFilePath).Replace("Encrypted", "Decrypted"));

                    //Write all byte to the decrypted file
                    File.WriteAllBytes(DecryptedFilePath, decryptedBytes);
                    break;
                }
            }
            //catches msdn exceptions
            catch (CryptographicException msdnException)
            {
                //Open a error message box
                Dialog.OpenErrorMessageBoxAsync(msdnException, "Decryption Failure", WindowDialogType.Error);
            }
            //catches bouncy castle exceptions
            catch (CryptoException bouncyException)
            {
                //Open a error message box
                Dialog.OpenErrorMessageBoxAsync(bouncyException, "Decryption Failure", WindowDialogType.Error);
            }
            //Catch any other errors
            catch (Exception exception)
            {
                //Open a error message box
                Dialog.OpenErrorMessageBoxAsync(exception, "Unknown Error, Contact Developer", WindowDialogType.Error);
            }
        }