public override void ViewDidLoad()
        {
            string SQLitePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "cryptedDb.db3");

             base.ViewDidLoad();

             SaveButton.TouchUpInside += (sender, e) =>
             {
            currentTask.Name = TitleText.Text;
            currentTask.Notes = NotesText.Text;
            currentTask.Done = DoneSwitch.On;

            CryptDelegate.SaveTask(currentTask);

            UpdateData(SQLitePath);
             };

             DeleteButton.TouchUpInside += (sender, e) =>
             {
            CryptDelegate.DeleteTask(currentTask);

            DeleteData(SQLitePath);
             };
        }
Example #2
0
        private void process(int action, int padding)
        {
            //Encrypt/Decrypt Stream
            try
            {
                if (InputStream == null || InputStream.Length == 0)
                {
                    GuiLogMessage("No input data, aborting now", NotificationLevel.Error);
                    return;
                }

                byte[] inputbuffer  = new byte[8];
                byte[] outputbuffer = new byte[8];

                uint[] key      = new uint[4];
                long[] longKey  = new long[4];
                long   keybytes = inputKey.Length;
                GuiLogMessage("inputKey length [byte]: " + keybytes.ToString(), NotificationLevel.Debug);

                if (keybytes != 16)
                {
                    GuiLogMessage("Given key has false length. Please provide a key with 128 Bits length. Aborting now.", NotificationLevel.Error);
                    return;
                }

                if (settings.Version != 2)
                {
                    key[0] = BitConverter.ToUInt32(inputKey, 0);
                    key[1] = BitConverter.ToUInt32(inputKey, 4);
                    key[2] = BitConverter.ToUInt32(inputKey, 8);
                    key[3] = BitConverter.ToUInt32(inputKey, 12);
                }
                else
                {
                    longKey[0] = (long)BitConverter.ToUInt32(inputKey, 0);
                    longKey[1] = (long)BitConverter.ToUInt32(inputKey, 4);
                    longKey[2] = (long)BitConverter.ToUInt32(inputKey, 8);
                    longKey[3] = (long)BitConverter.ToUInt32(inputKey, 12);
                }

                //check for a valid IV
                if (this.inputIV == null)
                {
                    //create a trivial IV
                    inputIV = new byte[8];
                    if (settings.Mode != 0)
                    {
                        GuiLogMessage("WARNING - No IV provided. Using 0x000..00!", NotificationLevel.Warning);
                    }
                }
                byte[] IV = new byte[8];
                Array.Copy(inputIV, IV, Math.Min(inputIV.Length, IV.Length));

                DateTime startTime = DateTime.Now;

                uint[] vector     = new uint[2];
                long[] longVector = new long[2];

                CryptDelegate crypfunc = delegates[settings.Action * 3 + settings.Version];
                StatusChanged((int)teaimages[settings.Action * 3 + settings.Version]);

                outputStream = new CStreamWriter();
                ICryptoolStream inputdata = InputStream;

                if (action == 0)
                {
                    inputdata = BlockCipherHelper.AppendPadding(InputStream, settings.padmap[settings.Padding], 8);
                }

                CStreamReader reader = inputdata.CreateReader();

                GuiLogMessage("Starting " + ((action == 0)?"encryption":"decryption") + " [Keysize=128 Bits, Blocksize=64 Bits]", NotificationLevel.Info);

                if (settings.Mode == 0)    // ECB
                {
                    while (reader.Read(inputbuffer, 0, inputbuffer.Length) > 0 && !stop)
                    {
                        Bytes2Vector(vector, inputbuffer);
                        crypfunc(settings.Rounds, vector, key);
                        Vector2Bytes(vector, outputbuffer);
                        outputStream.Write(outputbuffer);
                    }
                }
                else if (settings.Mode == 1)    // CBC
                {
                    if (settings.Action == 0)
                    {
                        while (reader.Read(inputbuffer, 0, inputbuffer.Length) > 0 && !stop)
                        {
                            for (int i = 0; i < 8; i++)
                            {
                                inputbuffer[i] ^= IV[i];
                            }
                            Bytes2Vector(vector, inputbuffer);
                            crypfunc(settings.Rounds, vector, key);
                            Vector2Bytes(vector, outputbuffer);
                            for (int i = 0; i < 8; i++)
                            {
                                IV[i] = outputbuffer[i];
                            }
                            outputStream.Write(outputbuffer);
                        }
                    }
                    else
                    {
                        while (reader.Read(inputbuffer, 0, inputbuffer.Length) > 0 && !stop)
                        {
                            Bytes2Vector(vector, inputbuffer);
                            crypfunc(settings.Rounds, vector, key);
                            Vector2Bytes(vector, outputbuffer);
                            for (int i = 0; i < 8; i++)
                            {
                                outputbuffer[i] ^= IV[i];
                            }
                            for (int i = 0; i < 8; i++)
                            {
                                IV[i] = inputbuffer[i];
                            }
                            outputStream.Write(outputbuffer);
                        }
                    }
                }
                else if (settings.Mode == 2)    // CFB
                {
                    if (settings.Action == 0)
                    {
                        while (reader.Read(inputbuffer, 0, inputbuffer.Length) > 0 && !stop)
                        {
                            Bytes2Vector(vector, IV);
                            crypfunc(settings.Rounds, vector, key);
                            Vector2Bytes(vector, outputbuffer);
                            for (int i = 0; i < 8; i++)
                            {
                                outputbuffer[i] ^= inputbuffer[i];
                            }
                            for (int i = 0; i < 8; i++)
                            {
                                IV[i] = outputbuffer[i];
                            }
                            outputStream.Write(outputbuffer);
                        }
                    }
                    else
                    {
                        crypfunc = delegates[settings.Version]; // choose encrypt function
                        while (reader.Read(inputbuffer, 0, inputbuffer.Length) > 0 && !stop)
                        {
                            Bytes2Vector(vector, IV);
                            crypfunc(settings.Rounds, vector, key);
                            Vector2Bytes(vector, outputbuffer);
                            for (int i = 0; i < 8; i++)
                            {
                                outputbuffer[i] ^= inputbuffer[i];
                            }
                            for (int i = 0; i < 8; i++)
                            {
                                IV[i] = inputbuffer[i];
                            }
                            outputStream.Write(outputbuffer);
                        }
                    }
                }
                else if (settings.Mode == 3)                // OFB - encrypt = decrypt
                {
                    crypfunc = delegates[settings.Version]; // choose encrypt function
                    while (reader.Read(inputbuffer, 0, inputbuffer.Length) > 0 && !stop)
                    {
                        Bytes2Vector(vector, IV);
                        crypfunc(settings.Rounds, vector, key);
                        Vector2Bytes(vector, outputbuffer);
                        for (int i = 0; i < 8; i++)
                        {
                            IV[i] = outputbuffer[i];
                        }
                        for (int i = 0; i < 8; i++)
                        {
                            outputbuffer[i] ^= inputbuffer[i];
                        }
                        outputStream.Write(outputbuffer);
                    }
                }

                long     outbytes = outputStream.Length;
                DateTime stopTime = DateTime.Now;
                TimeSpan duration = stopTime - startTime;

                outputStream.Close();

                if (action == 1)
                {
                    outputStream = BlockCipherHelper.StripPadding(outputStream, settings.padmap[settings.Padding], 8) as CStreamWriter;
                }

                if (!stop)
                {
                    GuiLogMessage("Time used: " + duration.ToString(), NotificationLevel.Debug);
                    OnPropertyChanged("OutputStream");
                }
                else
                {
                    GuiLogMessage("Aborted!", NotificationLevel.Info);
                }
            }
            catch (Exception exception)
            {
                GuiLogMessage(exception.Message, NotificationLevel.Error);
            }
            finally
            {
                ProgressChanged(1, 1);
            }
        }