private void ENC(bool encryptionOn)
        {
            MainForm.Frm.data.working = true;

            if (encryptionOn == false)
            {
                return;
            }

            string SKey       = "_?73^?dVTMokahar3";
            string SaltKey    = "!2S@LT&MoKat5har3EY";
            int    Iterations = 1510;
            var    bytes      = new byte[SaltKey.Length * sizeof(char)];

            Buffer.BlockCopy(SaltKey.ToCharArray(), 0, bytes, 0, bytes.Length);
            var aes = new AesManaged();

            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize   = aes.LegalKeySizes[0].MaxSize;
            var salt = bytes;
            var key  = new Rfc2898DeriveBytes(SKey, salt, Iterations);

            aes.Key  = key.GetBytes(aes.KeySize / 8);
            aes.IV   = key.GetBytes(aes.BlockSize / 8);
            aes.Mode = CipherMode.CBC;
            ICryptoTransform transform = aes.CreateEncryptor(aes.Key, aes.IV);

            using (var dest = new FileStream((DestFilePath + "Enc"), FileMode.CreateNew, FileAccess.Write, FileShare.None))
            {
                using (var cryptoStream = new CryptoStream(dest, transform, CryptoStreamMode.Write))
                {
                    using (var source = new FileStream(SourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        source.CopyToAsync(cryptoStream).Wait();
                    }
                }
            }
            File.Delete(DestFilePath);
            File.Move(DestFilePath + "Enc", DestFilePath.Replace("Enc", ""));
            MainForm.Frm.data.working = false;
        }
Esempio n. 2
0
        protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
        {
            PropertyDescriptor property = context.DataContext.GetProperties()[ExcelCreate.GetExcelAppTag];
            Excel::Application excelApp = property.GetValue(context.DataContext) as Excel::Application;

            try
            {
                string       cellName_Begin = CellName_Begin.Get(context);
                string       cellName_End = CellName_End.Get(context);
                int          cellRow_Begin = CellRow_Begin.Get(context);
                int          cellColumn_Begin = CellColumn_Begin.Get(context);
                int          cellRow_End = CellRow_End.Get(context);
                int          cellColumn_End = CellColumn_End.Get(context);
                int          destCellRow = DestCellRow.Get(context);
                int          destCellColumn = DestCellColumn.Get(context);
                string       copySheet = CopySheet.Get(context);
                string       destSheet = DestSheet.Get(context);
                string       destCell = DestCell.Get(context);
                string       destDestFilePath = DestFilePath.Get(context);
                Excel::Range range1, range2;

                Excel::_Worksheet sheet;
                if (copySheet != null)
                {
                    sheet = excelApp.ActiveWorkbook.Sheets[copySheet];
                }
                else
                {
                    sheet = excelApp.ActiveSheet;
                }
                range1 = cellName_Begin == null ? sheet.Cells[cellRow_Begin, cellColumn_Begin] : sheet.Range[cellName_Begin];
                range2 = cellName_End == null ? sheet.Cells[cellRow_End, cellColumn_End] : sheet.Range[cellName_End];
                sheet.Range[range1, range2].Copy(Type.Missing);

                Excel::_Worksheet pasteSheet;
                if (destDestFilePath != null && destDestFilePath != "")
                {
                    if (!File.Exists(destDestFilePath))
                    {
                        SharedObject.Instance.Output(SharedObject.enOutputType.Error, "文件不存在,请检查路径有效性", destDestFilePath);
                        new CommonVariable().realaseProcessExit(excelApp);
                    }
                    else
                    {
                        Excel::Workbook workbook2 = excelApp.Workbooks._Open(destDestFilePath);
                        if (destSheet != null)
                        {
                            pasteSheet = workbook2.Sheets[destSheet];
                        }
                        else
                        {
                            pasteSheet = workbook2.ActiveSheet;
                        }

                        Excel::Range pasteRange = destCell == null ? pasteSheet.Cells[destCellRow, destCellColumn] : pasteSheet.Range[destCell];
                        pasteSheet.Paste(pasteRange);
                        workbook2.Save();
                        workbook2.Close();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pasteSheet);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pasteRange);
                        pasteRange = null;
                    }
                }
                else
                {
                    if (destSheet != null)
                    {
                        pasteSheet = excelApp.ActiveWorkbook.Sheets[destSheet];
                    }
                    else
                    {
                        pasteSheet = excelApp.ActiveSheet;
                    }

                    Excel::Range pasteRange = destCell == null ? sheet.Cells[destCellRow, destCellColumn] : sheet.Range[destCell];
                    pasteSheet.Paste(pasteRange);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pasteSheet);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pasteRange);
                    pasteRange = null;
                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(range1);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(range2);
                range1 = null; range2 = null;
                sheet  = null; pasteSheet = null;
                GC.Collect();
            }
            catch (Exception e)
            {
                SharedObject.Instance.Output(SharedObject.enOutputType.Error, "EXCEL复制粘贴过程出错", e.Message);
                new CommonVariable().realaseProcessExit(excelApp);
            }
            m_Delegate = new runDelegate(Run);
            return(m_Delegate.BeginInvoke(callback, state));
        }