Exemple #1
0
        public void Decrypt()
        {
            if (this.inputStream != null)
            {
                // Decrypt Stream
                try
                {
                    SymmetricAlgorithm p_alg = new PresentManaged();
                    ConfigureAlg(p_alg, true);

                    ICryptoolStream inputdata = InputStream;

                    CStreamReader reader = inputdata.CreateReader();

                    ICryptoTransform p_decryptor = p_alg.CreateDecryptor();
                    outputStream        = new CStreamWriter();
                    p_crypto_stream_dec = new CryptoStream((Stream)reader, p_decryptor, CryptoStreamMode.Read);
                    byte[]   buffer = new byte[p_alg.BlockSize / 8];
                    int      bytesRead;
                    int      position  = 0;
                    DateTime startTime = DateTime.Now;

                    while ((bytesRead = p_crypto_stream_dec.Read(buffer, 0, buffer.Length)) > 0 && !stop)
                    {
                        outputStream.Write(buffer, 0, bytesRead);
                        if ((int)(reader.Position * 100 / inputStream.Length) > position)
                        {
                            position = (int)(reader.Position * 100 / inputStream.Length);
                            Progress(reader.Position, inputStream.Length);
                        }
                    }

                    p_crypto_stream_dec.Flush();
                    p_crypto_stream_dec.Close();

                    DateTime stopTime = DateTime.Now;
                    TimeSpan duration = stopTime - startTime;

                    outputStream.Close();

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

                    if (!stop)
                    {
                        GuiLogMessage("Decryption complete! (in: " + inputStream.Length.ToString() + " bytes, out: " + outputStream.Length.ToString() + " bytes)", NotificationLevel.Info);
                        GuiLogMessage("Time used: " + duration.ToString(), NotificationLevel.Debug);
                        OnPropertyChanged("OutputStream");
                    }
                    else
                    {
                        GuiLogMessage("Aborted!", NotificationLevel.Info);
                    }
                }
                catch (CryptographicException cryptographicException)
                {
                    // TODO: For an unknown reason p_crypto_stream can not be closed after exception.
                    // Trying so makes p_crypto_stream throw the same exception again. So in Dispose
                    // the error messages will be doubled.
                    // As a workaround we set p_crypto_stream to null here.
                    p_crypto_stream_dec = null;
                    GuiLogMessage(cryptographicException.Message, NotificationLevel.Error);
                }
                catch (Exception exception)
                {
                    GuiLogMessage(exception.Message, NotificationLevel.Error);
                }
                finally
                {
                    Progress(1, 1);
                }
            }
        }
Exemple #2
0
        public void Encrypt()
        {
            if (this.inputStream != null)
            {
                // Encrypt Stream
                try
                {
                    SymmetricAlgorithm p_alg = new PresentManaged();
                    ConfigureAlg(p_alg, true);

                    ICryptoolStream inputdata = InputStream;

                    inputdata = BlockCipherHelper.AppendPadding(InputStream, settings.padmap[settings.Padding], p_alg.BlockSize / 8);

                    CStreamReader reader = inputdata.CreateReader();

                    if ((this.presentation != null) & (p_alg.KeySize == 80))
                    {
                        byte[] block = new byte[8];
                        byte[] key   = (byte[])p_alg.Key.Clone();
                        int    r     = reader.Read(block, 0, 8);
                        if (reader.CanSeek)
                        {
                            reader.Position = 0;
                        }
                        if (r < 8)
                        {
                            for (int i = 0; i < r; i++)
                            {
                                block[7 - i] = block[r - i - 1];
                            }
                            byte p;
                            if (p_alg.Padding == PaddingMode.PKCS7)
                            {
                                p = (byte)(8 - r);
                            }
                            else
                            {
                                p = (byte)0;
                            }
                            for (int i = 0; i < 8 - r; i++)
                            {
                                block[i] = p;
                            }
                        }
                        this.presentation.Assign_Values(key, block);
                    }

                    ICryptoTransform p_encryptor = p_alg.CreateEncryptor();

                    outputStream        = new CStreamWriter();
                    p_crypto_stream_enc = new CryptoStream((Stream)reader, p_encryptor, CryptoStreamMode.Read);
                    byte[]   buffer = new byte[p_alg.BlockSize / 8];
                    int      bytesRead;
                    int      position  = 0;
                    DateTime startTime = DateTime.Now;
                    while ((bytesRead = p_crypto_stream_enc.Read(buffer, 0, buffer.Length)) > 0 && !stop)
                    {
                        outputStream.Write(buffer, 0, bytesRead);
                        if ((int)(reader.Position * 100 / inputStream.Length) > position)
                        {
                            position = (int)(reader.Position * 100 / inputStream.Length);
                            Progress(reader.Position, inputStream.Length);
                        }
                    }
                    p_crypto_stream_enc.Flush();
                    // p_crypto_stream_enc.Close();

                    DateTime stopTime = DateTime.Now;
                    TimeSpan duration = stopTime - startTime;

                    outputStream.Close();

                    if (!stop)
                    {
                        GuiLogMessage("Encryption complete! (in: " + inputStream.Length.ToString() + " bytes, out: " + outputStream.Length.ToString() + " bytes)", NotificationLevel.Info);
                        GuiLogMessage("Time used: " + duration.ToString(), NotificationLevel.Debug);
                        OnPropertyChanged("OutputStream");
                    }
                    else
                    {
                        GuiLogMessage("Aborted!", NotificationLevel.Info);
                    }
                }
                catch (CryptographicException cryptographicException)
                {
                    // TODO: For an unknown reason p_crypto_stream can not be closed after exception.
                    // Trying so makes p_crypto_stream throw the same exception again. So in Dispose
                    // the error messages will be doubled.
                    // As a workaround we set p_crypto_stream to null here.
                    p_crypto_stream_enc = null;
                    GuiLogMessage(cryptographicException.Message, NotificationLevel.Error);
                }
                catch (Exception exception)
                {
                    GuiLogMessage(exception.Message, NotificationLevel.Error);
                }
                finally
                {
                    Progress(1, 1);
                }
            }
        }