Ejemplo n.º 1
0
        /// <summary>
        /// The command method to encrypt the given data
        /// </summary>
        private void Encrypt()
        {
            //Convert the string key and iv to bytes
            var secretKey = ByteConvert.HexStringToBytes(SecretKey);
            var iv        = ByteConvert.HexStringToBytes(IV);

            //fields for saving the plain and encrypted byte arrays
            byte[] encrypted;
            byte[] plainBytes;

            //Get the data and encrypt it acording to the selected format
            switch (DataInput.DataFormatSelected)
            {
            //Encrypt a text string
            case Format.Text:

                //Encrypt with the selected cipher and return the encrypted byte array
                encrypted = SelectedCipherApi.EncryptText(SelectedAlgorithim, SelectedKeySize, secretKey, iv, DataInput.Data);

                //Converts the byte array to a hex string
                EncryptedText = ByteConvert.BytesToHexString(encrypted);
                break;

            //Encrypt a hex string
            case Format.Hex:

                //Convert the plain hex string to byte array
                plainBytes = ByteConvert.HexStringToBytes(DataInput.Data);

                //Encrypt with the selected cipher and return the encrypted byte array
                encrypted = SelectedCipherApi.EncryptBytes(SelectedAlgorithim, SelectedKeySize, secretKey, iv, plainBytes);

                //Converts the byte array to a hex string
                EncryptedText = ByteConvert.BytesToHexString(encrypted);
                break;

            //Encrypt a file
            case Format.File:

                //Gets the file as a byte array
                plainBytes = File.ReadAllBytes(DataInput.Data);

                //Encrypt with the selected cipher and return the encrypted byte array
                encrypted = SelectedCipherApi.EncryptBytes(SelectedAlgorithim, SelectedKeySize, secretKey, iv, plainBytes);

                //Gets the file extension of the plain original file
                var extension = Path.GetExtension(DataInput.Data);

                //Adds the "Encrypted" text to the encrypted file name
                EncryptedFilePath = Path.Combine(Directory.GetParent(DataInput.Data).ToString(), Path.GetFileNameWithoutExtension(DataInput.Data) + ".Encrypted" + extension);

                //Writes all the bytes to the encrypted file
                File.WriteAllBytes(EncryptedFilePath, encrypted);
                break;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The command method when a different algrithim is selected
        /// </summary>
        private void ChangedAlgorithim()
        {
            //Gets the available key size of the selected algorithim
            KeySizes = SelectedCipherApi.GetKeySizes(SelectedAlgorithim);

            //Set default key size, first one in the list
            SelectedKeySize = KeySizes[0];

            //Gets the available iv size of the selected algorithim
            IvSize = SelectedCipherApi.GetIvSize(SelectedAlgorithim);

            ClearKeyValue();

            ClearEnDecryptedValues();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// The command methods to generate a random key
        /// </summary>
        private void GenerateKey()
        {
            //Gets a list of byte arrays containing the secret key and if available the iv
            var keyAndIv = SelectedCipherApi.GenerateKey(SelectedAlgorithim, SelectedKeySize);

            //Convert the byte array to a string
            SecretKey = ByteConvert.BytesToHexString(keyAndIv[0]);

            //true if an iv byte array exists
            if (keyAndIv.Count > 1)
            {
                //Convert the byte array to a string
                IV = ByteConvert.BytesToHexString(keyAndIv[1]);
            }
        }
Ejemplo n.º 4
0
        /// <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);
            }
        }