Beispiel #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;
            }
        }
        /// <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;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Default constructor
        /// </summary>
        public KeyPairSetupDesignModel()
        {
            SelectedOperation = AsymmetricOperation.Encryption;
            Algorithims       = BaseMsdnAsymmetric.GetAlgorthims(SelectedOperation);
            //Algorithims = IAsymmetricCipher.GetBouncyAlgorthims(SelectedOperation);
            SelectedAlgorithimIndex = 0;
            BaseMsdnAsymmetric.GetCipher(Algorithims[SelectedAlgorithimIndex]);

            PrivateKey = ByteConvert.HexStringToBytes("FFFFFFFFF111000");
            PublicKey  = ByteConvert.HexStringToBytes("00000000FFFFFFF");
        }
Beispiel #4
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;
            }
        }
Beispiel #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());
            }
        }
Beispiel #6
0
        /// <summary>
        /// The command method to verify a signature
        /// </summary>
        private void Verify()
        {
            var pubKey    = File.ReadAllBytes(KeyPairSetup.PublicKeyFilePath);
            var signature = ByteConvert.HexStringToBytes(OriginalSignature);

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

            case Format.Text:
                data = ByteConvert.StringToUTF8Bytes(DataInput.Data);
                break;
            }
            SignatureVerified = KeyPairSetup.Verify(signature, pubKey, data);
        }
        /// <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);
            }
        }