예제 #1
0
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                // System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed.
                if (!(e.Error is CryptographicException))
                {
                    MessageBox.Show(e.Error.ToString());
                }
            }
            else
            {
                CryptoAction action = (CryptoAction)e.Result;


                if (action.ActionType == CryptoActionType.Encrypt)
                {
                    IsEncrypted = true;
                }
                else if (action.ActionType == CryptoActionType.Decrypt)
                {
                    IsEncrypted = false;
                }
            }

            IsCrypting = false;

            if (!IsEncrypted)
            {
                CryptProgress = -0.05;
            }

            StateChange();
        }
예제 #2
0
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            CryptoAction action = (CryptoAction)e.Argument;

            if (action.ActionType == CryptoActionType.Encrypt)
            {
                string newPath = Path + ".crypter";

                using (FileStream encryptedFS = new FileStream(newPath, FileMode.Create))
                {
                    using (RijndaelManaged AES = new RijndaelManaged())
                    {
                        AES.KeySize   = 256;
                        AES.BlockSize = 128;

                        var key = new Rfc2898DeriveBytes(Crypto.GetSecureStringBytes(action.Password), Crypto.saltBytes, 1000);
                        AES.Key = key.GetBytes(AES.KeySize / 8);
                        AES.IV  = key.GetBytes(AES.BlockSize / 8);

                        AES.Mode = CipherMode.CBC;

                        using (var cs = new CryptoStream(encryptedFS, AES.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            using (FileStream fsIn = new FileStream(Path, FileMode.Open))
                            {
                                int  data;
                                long finishedBytes = 0;

                                long reportProgressEvery_bytes = fsIn.Length / 500;

                                long report_per_cent_Every_bytes = fsIn.Length / 100;

                                if (report_per_cent_Every_bytes < 1)
                                {
                                    report_per_cent_Every_bytes = 1;
                                }

                                if (reportProgressEvery_bytes < 1)
                                {
                                    reportProgressEvery_bytes = 1;
                                }

                                while ((data = fsIn.ReadByte()) != -1)
                                {
                                    cs.WriteByte((byte)data);
                                    finishedBytes++;
                                    //worker.ReportProgress(0, (double)((double)finishedBytes / (double)fsIn.Length));
                                    if (finishedBytes % reportProgressEvery_bytes == 0)
                                    {
                                        CryptProgress = (double)((double)finishedBytes / (double)fsIn.Length);
                                    }

                                    if (finishedBytes % report_per_cent_Every_bytes == 0)
                                    {
                                        StateChange();
                                    }
                                }

                                fsIn.Close();
                            }

                            cs.Close();
                        }

                        encryptedFS.Close();
                    }
                }

                File.Delete(Path);
                Path = newPath;
            }
            else if (action.ActionType == CryptoActionType.Decrypt)
            {
                string newPath = System.IO.Path.GetDirectoryName(Path) + "\\" + OGName;

                try
                {
                    using (FileStream decryptedFS = new FileStream(newPath, FileMode.Create))
                    {
                        using (RijndaelManaged AES = new RijndaelManaged())
                        {
                            AES.KeySize   = 256;
                            AES.BlockSize = 128;

                            var key = new Rfc2898DeriveBytes(Crypto.GetSecureStringBytes(action.Password), Crypto.saltBytes, 1000);
                            AES.Key = key.GetBytes(AES.KeySize / 8);
                            AES.IV  = key.GetBytes(AES.BlockSize / 8);

                            AES.Mode = CipherMode.CBC;

                            using (var cs = new CryptoStream(decryptedFS, AES.CreateDecryptor(), CryptoStreamMode.Write))
                            {
                                using (FileStream fsIn = new FileStream(Path, FileMode.Open))
                                {
                                    int  data;
                                    long finishedBytes = 0;

                                    long reportProgressEvery_bytes = fsIn.Length / 500;

                                    long report_per_cent_Every_bytes = fsIn.Length / 100;

                                    if (report_per_cent_Every_bytes < 1)
                                    {
                                        report_per_cent_Every_bytes = 1;
                                    }

                                    if (reportProgressEvery_bytes < 1)
                                    {
                                        reportProgressEvery_bytes = 1;
                                    }

                                    while ((data = fsIn.ReadByte()) != -1)
                                    {
                                        cs.WriteByte((byte)data);
                                        finishedBytes++;
                                        //worker.ReportProgress(0, (double)((double)finishedBytes / (double)fsIn.Length));
                                        if (finishedBytes % reportProgressEvery_bytes == 0)
                                        {
                                            CryptProgress = 0.95 - (double)((double)finishedBytes / (double)fsIn.Length);
                                        }

                                        if (finishedBytes % report_per_cent_Every_bytes == 0)
                                        {
                                            StateChange();
                                        }
                                    }

                                    fsIn.Close();
                                }

                                cs.Close();
                            }
                        }

                        decryptedFS.Close();
                    }

                    File.Delete(Path);

                    Path = newPath;
                }
                catch (CryptographicException cex)
                {
                    if (cex.Message == "Padding is invalid and cannot be removed.")
                    {
                        File.Delete(newPath);
                        MessageBox.Show("Invalid padding exception.\n\n(This most likely means your key is incorrect.)");
                    }
                    else
                    {
                        MessageBox.Show(cex.ToString());
                    }

                    throw cex;
                }
            }

            e.Result = action;
        }