Пример #1
0
        /// <summary>
        /// The command method to encrypt the given data
        /// </summary>
        private void Encrypt()
        {
            byte[] encrypted;
            byte[] plainBytes;

            //Encryption sequence differs according to the selected data format
            switch (DataInput.DataFormatSelected)
            {
            //Encrypt file
            case Format.File:
                //Get plain file bytes
                plainBytes = DataInput.GetBytesFromFile();

                //Encrypt
                encrypted = KeyPairSetup.Encrypt(plainBytes);

                //Verify that the encryption is successfull
                if (encrypted != null)
                {
                    //Create encrypted file path
                    EncryptedFilePath = DataInput.GetEncryptedFilePath();

                    //Write the encrypted bytes to the new file path
                    File.WriteAllBytes(EncryptedFilePath, encrypted);
                }
                break;

            //Encrypt text
            case Format.Text:
                //Encrypt
                encrypted = KeyPairSetup.Encrypt(DataInput.Data);

                //Verify that the encryption is successfull
                if (encrypted != null)
                {
                    //Comvert the encrypted bytes to hex string
                    EncryptedText = ByteConvert.BytesToHexString(encrypted);
                }
                break;

            //Encrypt hex string
            case Format.Hex:
                //Convert hex string to plain bytes
                plainBytes = ByteConvert.HexStringToBytes(DataInput.Data);

                //Encrypt
                encrypted = KeyPairSetup.Encrypt(plainBytes);

                //Verify that the encryption is successfull
                if (encrypted != null)
                {
                    //Convert encrypted bytes to hex string
                    EncryptedText = ByteConvert.BytesToHexString(encrypted);
                }
                break;
            }
        }
Пример #2
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;
            }
        }
Пример #3
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;
            }
        }
Пример #4
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]);
            }
        }
Пример #5
0
        /* Attempt to send data */
        private void SendData()
        {
            if (serialPort == null || !serialPort.IsOpen)
            {
                PutLog("请先连接串口后再发送数据!");
                return;
            }

            try
            {
                byte[] buffer;

                if (SendDataType == 0)
                {
                    buffer = Encoding.GetEncoding(setting.sendDataEncoding).GetBytes(SendDataText);
                    PutSendDataLog(SendDataType, SendDataText);
                }
                else if (SendDataType == 1)
                {
                    buffer = ByteConvert.HexStringToBytes(SendDataText);
                    PutSendDataLog(SendDataType, ByteConvert.BytesToHexString(buffer));
                }
                else
                {
                    buffer = ByteConvert.BinStringToBytes(SendDataText);
                    PutSendDataLog(SendDataType, ByteConvert.BytesToBinString(buffer));
                }

                if (buffer.Length == 0)
                {
                    PutLog("发送数据为空!");
                    return;
                }

                serialPort.Write(buffer, 0, buffer.Length);

                numberBytesSendInt += buffer.Length;

                NumberBytesSend = numberBytesSendInt.ToString();
            }
            catch (Exception e)
            {
                PutLog(e.ToString());
            }
        }
Пример #6
0
        /// <summary>
        /// The command method to sign some data
        /// </summary>
        private void Sign()
        {
            var privKey = File.ReadAllBytes(KeyPairSetup.PrivateKeyFilePath);

            byte[] data = null;
            switch (DataInput.DataFormatSelected)
            {
            case Format.File:
                data = File.ReadAllBytes(DataInput.Data);
                break;

            case Format.Text:
                data = ByteConvert.StringToUTF8Bytes(DataInput.Data);
                break;
            }
            var signature = KeyPairSetup.Sign(privKey, data);

            OriginalSignature = ByteConvert.BytesToHexString(signature);
        }
Пример #7
0
        /// <summary>
        /// The command method to derive a shared secret key from the other party public key and our own keys
        /// </summary>
        private void DeriveKey()
        {
            var derivedKey = KeyPairSetup.DeriveKey();

            DerivedKey = ByteConvert.BytesToHexString(derivedKey);
        }
Пример #8
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);
            }
        }
Пример #9
0
        /* Displays data according to the display type set by the user */
        private void DisplayReceiveData()
        {
            if (receiveDisplayType >= 0 && receiveDisplayType <= 2)
            {
                byte[] buffer = receiveBuffer.ToArray();

                int offset = 0, count = buffer.Length;
                if (count > setting.maxShowByteCount)
                {
                    offset = count - setting.maxShowByteCount;
                    count  = setting.maxShowByteCount;
                }

                if (receiveDisplayType == 0)
                {
                    ReceiveData = ByteConvert.BytesToHexString(buffer, offset, count);
                }
                else if (receiveDisplayType == 1)
                {
                    ReceiveData = Encoding.GetEncoding(setting.receiveDataEncoding).GetString(buffer, offset, count);
                }
                else
                {
                    ReceiveData = ByteConvert.BytesToBinString(buffer, offset, count);
                }
            }
            else
            {
                int count = 0;

                ReceiveBuffer.Block tempBlock;
                Stack <string>      stack = new Stack <string>();

                for (int i = receiveBuffer.BlockCount - 1; i >= 0; --i)
                {
                    tempBlock = receiveBuffer.FindBlock(i);

                    int putCount = setting.maxShowByteCount - count;
                    if (putCount > tempBlock.Data.Length)
                    {
                        putCount = tempBlock.Data.Length;
                    }

                    if (putCount == 0)
                    {
                        break;
                    }

                    string head = tempBlock.Time.ToString(setting.receiveLogTimeFormat) + (setting.receiveLogLineFeedSplitsTimeAndContent ? '\n' : ':');

                    if (receiveDisplayType == 3)
                    {
                        stack.Push(head + ByteConvert.BytesToHexString(tempBlock.Data, tempBlock.Data.Length - putCount, putCount) + '\n');
                    }
                    else if (receiveDisplayType == 4)
                    {
                        stack.Push(head + Encoding.GetEncoding(setting.receiveDataEncoding).GetString(tempBlock.Data, tempBlock.Data.Length - putCount, putCount) + '\n');
                    }
                    else
                    {
                        stack.Push(head + ByteConvert.BytesToBinString(tempBlock.Data, tempBlock.Data.Length - putCount, putCount) + '\n');
                    }

                    count += tempBlock.Data.Length;
                    if (count > setting.maxShowByteCount)
                    {
                        break;
                    }
                }

                StringBuilder finalDisplay = new StringBuilder();
                while (stack.Count > 0)
                {
                    finalDisplay.Append(stack.Pop());
                }
                ReceiveData = finalDisplay.ToString();
            }
        }