Exemplo n.º 1
0
        public SharedObject encrypt(SharedObject inputObject)
        {
            string[] subKeys = createSubkeys(Utility.String2HexArray(inputObject.Key, 16)[0]);

            /* Convert the string into hexadecimal form and split it as 16 character hex.
             * If the splitted string is not of 16 character it right pads with 0 */
            string[] hexDataArray = Utility.String2HexArray(inputObject.PlainText, 16);

            string encryptedData = "";
            string encyptedBlock = "";
            string inputVector = "";

            for (int i = 0; i < hexDataArray.Length; i++)
            {
                    if (inputObject.Mode == 1)
                    {
                        if (i == 0)
                        {
                            inputVector = Utility.String2HexArray(inputObject.InputVector, 16)[0];
                            hexDataArray[i] = Utility.Binary2Hex(Utility.XORBinaryString(Utility.Hex2Binary(hexDataArray[i]),Utility.Hex2Binary(inputVector)));
                        }
                        else hexDataArray[i] = Utility.Binary2Hex(Utility.XORBinaryString(Utility.Hex2Binary(hexDataArray[i]), Utility.Hex2Binary(encyptedBlock)));
                    }
                    encyptedBlock = encodeData(hexDataArray[i], subKeys);
                    encryptedData += encyptedBlock;
            }
            SharedObject resultObject = new SharedObject();
            resultObject.PlainText = inputObject.PlainText;
            resultObject.CipherText = encryptedData;
            resultObject.InputVector = Utility.Hex2String(inputVector);
            Logger.Log(this.GetType(), "Encrypted Data" + encryptedData);
            return resultObject;
        }
Exemplo n.º 2
0
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            txtPlain.Text = "";
            if (string.IsNullOrWhiteSpace(txtCypher.Text) || string.IsNullOrWhiteSpace(txtKey.Text))
            {
                MessageBox.Show("Please enter the Plain text and the Key for encryption.");
                return;
            }
            if (txtKey.Text.Length < 8)
            {
                MessageBox.Show("Key is too short.Should be of length 8");
                txtKey.Focus();
                return;
            }

            if (comboModes.SelectedIndex == 1 && string.IsNullOrWhiteSpace(txtInputVector.Text))
            {
                MessageBox.Show("Please enter the Input Vector(IV) for CBC Mode.");
                txtInputVector.Focus();
                return;
            }

            toolStripProgressBar1.Style = ProgressBarStyle.Marquee;
            toolStripProgressBar1.MarqueeAnimationSpeed = 30;
            toolStripProgressBar1.Visible = true;

            SharedObject sharedObject = new SharedObject();

            sharedObject.Algorithm = sltAlgorithm.SelectedIndex;
            sharedObject.Mode = comboModes.SelectedIndex;
            sharedObject.PlainText = txtPlain.Text;
            sharedObject.CipherText = txtCypher.Text;
            sharedObject.InputVector = txtInputVector.Text;
            sharedObject.Key = txtKey.Text;
            sharedObject.IsEncrypt = false;

            backgroundWorkerThreadOne.RunWorkerAsync(sharedObject);
        }
Exemplo n.º 3
0
        public SharedObject decrypt(SharedObject inputObject)
        {
            string[] subKeys = createSubkeys(Utility.String2HexArray(inputObject.Key, 16)[0]);
            Array.Reverse(subKeys);

            /* Convert the string into hexadecimal form and split it as 16 character hex.
             * If the splitted string is not of 16 character it right pads with 0 */
            string[] hexDataArray = Regex.Replace(inputObject.CipherText, @"(.{16})", "$1 ").Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
            /* To Test hexDataArray = new string[]{"0123456789ABCDEF"};*/

            string decryptedData = "";
            string decryptedBlock = "";
            string inputVector = "";
            for (int i = 0; i < hexDataArray.Length; i++)
            {
              decryptedBlock = encodeData(hexDataArray[i], subKeys);
              if (inputObject.Mode == 1)
              {
                  if (i == 0)
                  {
                      inputVector = Utility.String2HexArray(inputObject.InputVector, 16)[0];
                      decryptedBlock = Utility.XORBinaryString(Utility.Hex2Binary(decryptedBlock), Utility.Hex2Binary(inputVector));
                  }
                  else
                      decryptedBlock = Utility.XORBinaryString(Utility.Hex2Binary(hexDataArray[i-1]), Utility.Hex2Binary(decryptedBlock));
              }

              decryptedData += Utility.Binary2Hex(decryptedBlock);
            }

            SharedObject resultObject = new SharedObject();
            resultObject.PlainText = Utility.Hex2String(decryptedData);
            resultObject.CipherText = inputObject.PlainText;

            Logger.Log(this.GetType(), "Decrypted Data" + decryptedData);
            return resultObject;
        }
Exemplo n.º 4
0
        private void DoWork(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            DateTime begin = DateTime.UtcNow;

            SharedObject sharedObject = doWorkEventArgs.Argument as SharedObject;
            SharedObject resultObject = new SharedObject();
            switch (sharedObject.Algorithm)
            {
                case 1:

                    DESAlgorithm des = new DESAlgorithm();
                    if (sharedObject.IsEncrypt)
                        resultObject = des.encrypt(sharedObject);
                    else resultObject = des.decrypt(sharedObject);

                    break;
                case 2: break;
                case 3: break;
            }
            DateTime end = DateTime.UtcNow;

            resultObject.TimeTaken = (end - begin).ToString("c");
            resultObject.IsEncrypt = sharedObject.IsEncrypt;

            doWorkEventArgs.Result = resultObject;
        }