当前位置:网站首页>TXT_ File encryption and compression

TXT_ File encryption and compression

2022-06-13 07:03:00 AndroidOrCSharp

WinRar hit tar package

  string WinRarDir = GetWinRarDir();
  WinrarZip(ClearingFileDir, "p_report", dateTime, batchNo.ToString(), Dir, FileName, WinRarDir, "100000000000306");

/// <summary>
        ///  collect files 
        /// </summary>
        /// <param name="OutPutDir"> The output path </param>
        /// <param name="ZipName"></param>
        /// <param name="Date"></param>
        /// <param name="BatchNo"></param>
        /// <param name="FileDir"></param>
        /// <param name="FileName"></param>
        /// <param name="WinRarDir"> Compressor directory </param>
        /// <param name="seatNo"> Organization code </param>
        private void WinrarZip(string OutPutDir, string ZipName, string Date, string BatchNo, string FileDir, string FileName, string WinRarDir, string seatNo)
        {
            try
            {
                ProcessStartInfo psi = new ProcessStartInfo(WinRarDir + "\\winrar.exe");
                psi.WindowStyle = ProcessWindowStyle.Hidden;

                // Source file encryption 
                log.Info("        The source file format is modified to UTF-8");
                SendMsgEdit("        The source file format is modified to UTF-8");
                TransformFile(FileDir + "\\" + FileName, FileDir + "\\" + FileName);
                log.Info("        Source file encryption ");
                SendMsgEdit("        Source file encryption ");
                EncryptFile(FileDir + "\\" + FileName, FileDir + "\\" + ZipName + FileName);
                log.Info("        New encrypted file added to TAR In bag ");
                SendMsgEdit("        New encrypted file added to TAR In bag ");

                // New encrypted file / The original file is added to TAR In bag 
                //psi.Arguments = string.Format("a -df -av- -ep1 -ibck -afzip {0}{1}_{2}_{3}.tar {4}", OutPutDir, ZipName, Date, BatchNo, FileDir + "\\" + FileName);
                psi.Arguments = string.Format("a -df -av- -ep1 -ibck -afzip {0}{1}_{2}_{3}_{4}.tar {5}", OutPutDir, ZipName, seatNo, Date, BatchNo, FileDir + "\\" + ZipName + FileName);
                Process process = Process.Start(psi);
                process.WaitForExit();


                //TAR Add package to send package 
                log.Info("       TAR Add package to send package ");
                SendMsgEdit("       TAR Add package to send package ");
                psi.Arguments = string.Format("a -av- -ep1 -ibck {0}{1}_{2}_{3}_{4}.tar.gz {5}", OutPutDir, ZipName, seatNo, Date, BatchNo, FileDir + "\\" + ZipName + "_" + seatNo + "_" + Date + "_" + BatchNo + ".tar");
                process = Process.Start(psi);
                process.WaitForExit();
            }
            catch (Exception ex) 
            {
                log.Info(string.Format(" Exception occurred in fast money forward file encryption , Abnormal information :{0}",ex.Message));
                SendMsgEdit(string.Format(" Exception occurred in fast money forward file encryption , Abnormal information :{0}", ex.Message));
            }

        }

        // obtain winrar The installation path 
        private string GetWinRarDir()
        {
            RegistryKey rkey = Registry.CurrentUser;
            RegistryKey subKey = rkey.OpenSubKey(@"Software\WinRAR SFX", true);
            log.Info(" Compressor installation path :" + subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString());
            SendMsgEdit(" Compressor installation path :" + subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString());
            return subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString();
        }

        //txt Change the file to UTF-8
        private void TransformFile(string InputFilename, string OutputFilename)
        {
            FileStream fStream = new FileStream(InputFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            StreamReader sReader = new StreamReader(fStream, Encoding.Default);

            string strRead = sReader.ReadToEnd();
            sReader.Close();
            fStream.Close();

            fStream = new FileStream(OutputFilename, FileMode.Create, FileAccess.Write);
            StreamWriter sWriter = new StreamWriter(fStream, Encoding.UTF8);
            sWriter.Write(strRead);

            sWriter.Close();
            fStream.Close();
        }

        // Encrypt files 
        private void EncryptFile(string InputFilename, string OutputFilename)
        {
            string strKey = IniFile.GetCfgValue("SubSystem,QP", "DESKey");
            SendMsgEdit("        Read the fast money key :" + strKey);
            EncryptDESFile.EncryptFile(InputFilename, OutputFilename, strKey, Encoding.UTF8, FileContentModify);
        }

        /// <summary>
        ///  Add... Before the contents of the document 8 individual 8, Fill in blanks after the contents of the document 
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        private string FileContentModify(string strInput)
        {
            string strRead = "88888888" + strInput;
            byte[] byteInput = Encoding.UTF8.GetBytes(strRead);

            return strRead.PadRight(strRead.Length + (8 - byteInput.Length % 8), ' ');
        }



        /// <summary>
        ///  The input content needs to be modified with the specified output file code DES File encryption 
        /// </summary>
        /// <param name="InputFilename"></param>
        /// <param name="OutputFilename"></param>
        /// <param name="sKey"></param>
        /// <param name="EncryptEncode"></param>
        /// <param name="FileContent"></param>
        public static void EncryptFile(string InputFilename, string OutputFilename, string sKey, Encoding EncryptEncode, FileContentHander FileContent)
        {
            FileStream FileInput = null;
            StreamReader sReader = null;
            FileStream FileOutput = null;
            CryptoStream cryptoStream = null;

            try
            {
                // Determine the key length 
                sKey = KeyLengthValid(sKey);
                // Determine the encoding method of the input file 
                Encoding fileEncode = GetType(InputFilename);

                // Read in file encoding 
                FileInput = new FileStream(InputFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                sReader = new StreamReader(FileInput, fileEncode);

                // Read in the data 
                string strRead = sReader.ReadToEnd();
                if (FileContent != null)
                {
                    strRead = FileContent(strRead);
                }

                //DES Encrypting device 
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                DES.Mode = CipherMode.ECB;
                DES.Padding = PaddingMode.None;
                DES.Key = EncryptEncode.GetBytes(sKey);
                DES.IV = EncryptEncode.GetBytes(sKey);

                ICryptoTransform desEncrypt = DES.CreateEncryptor();
                FileOutput = new FileStream(OutputFilename, FileMode.Create, FileAccess.Write);
                cryptoStream = new CryptoStream(FileOutput, desEncrypt, CryptoStreamMode.Write);

                // Write encrypted data with encrypted stream 
                byte[] byteOutput = EncryptEncode.GetBytes(strRead);
                cryptoStream.Write(byteOutput, 0, byteOutput.Length);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                cryptoStream.Close();
                FileOutput.Close();
                sReader.Close();
                FileInput.Close();
            }
        }

Start console call 7zip Command packaging

        // obtain winrar The installation path 
        private string GetRarDir()
        {
            RegistryKey rkey = Registry.CurrentUser;
            RegistryKey subKey = rkey.OpenSubKey(@"Software\7-Zip", true);
            log.Info(" Compressor installation path :" + subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString());
            SendMsgEdit(" Compressor installation path :" + subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString());
            return subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString();
        }

   private void SevenZip(string OutPutDir, string ZipName, string Date, string BatchNo, string FileDir, string FileName, string RarDir, string seatNo)
        {
            try
            {
                FileInfo fileInfo;
                ProcessStartInfo psi = new ProcessStartInfo(RarDir + "\\7z.exe");
                psi.WindowStyle = ProcessWindowStyle.Hidden;

                // Source file encryption 
                log.Info("        The source file format is modified to UTF-8");
                SendMsgEdit("        The source file format is modified to UTF-8");
                TransformFile(FileDir + "\\" + FileName, FileDir + "\\" + FileName);
                log.Info("        Source file encryption ");
                SendMsgEdit("        Source file encryption ");
                EncryptFile(FileDir + "\\" + FileName, FileDir + "\\" + FileName);
                log.Info("        New encrypted file added to TAR In bag ");
                SendMsgEdit("        New encrypted file added to TAR In bag ");

                // start-up CMD  txt The file is compressed into tar package 
                Process process = new Process();
                process.StartInfo.FileName = "cmd.exe";
                string message = "";
                string path = RarDir.Substring(0,1)+":";
                string exePath = @"cd "+ RarDir;
                string command = string.Format(@"7z a " + OutPutDir + ZipName + "_" + seatNo + "_" + Date + "_" + BatchNo + ".tar" + " " + OutPutDir + FileName);

                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;
                process.Start();
                process.StandardInput.WriteLine(path);
                process.StandardInput.WriteLine(exePath);
                process.StandardInput.WriteLine(command);
                process.StandardInput.WriteLine("exit");
                message = process.StandardOutput.ReadToEnd();

                command = string.Format(@"7z a " + OutPutDir + ZipName + "_" + seatNo + "_" + Date + "_" + BatchNo + ".tar.gz" + " " + OutPutDir + ZipName + "_" + seatNo + "_" + Date + "_" + BatchNo + ".tar");
                // Delete after compression txt file 
                fileInfo = new FileInfo(OutPutDir + FileName);
                fileInfo.Delete();
                // Open a new one CMD tar Pack compressed into tar.gz
                Process tarGz = new Process();
                tarGz.StartInfo.FileName = "cmd.exe";
                tarGz.StartInfo.UseShellExecute = false;
                tarGz.StartInfo.RedirectStandardInput = true;
                tarGz.StartInfo.RedirectStandardOutput = true;
                tarGz.StartInfo.RedirectStandardError = true;
                tarGz.StartInfo.CreateNoWindow = true;
                tarGz.Start();
                tarGz.StandardInput.WriteLine(path);
                tarGz.StandardInput.WriteLine(exePath);
                tarGz.StandardInput.WriteLine(command);
                tarGz.StandardInput.WriteLine("exit");
                message = tarGz.StandardOutput.ReadToEnd();
                // Delete after compression tar file 
                fileInfo = new FileInfo(OutPutDir + ZipName + "_" + seatNo + "_" + Date + "_" + BatchNo + ".tar");
                fileInfo.Delete();
            }
            catch (Exception ex)
            {
                log.Info(string.Format(" Exception occurred in fast money forward file encryption , Abnormal information :{0}", ex.Message));
                SendMsgEdit(string.Format(" Exception occurred in fast money forward file encryption , Abnormal information :{0}", ex.Message));
            }

        }

winrar command :

            psi.Arguments = string.Format("a -av- -ep1 -afzip {0}.rar {0}", floderPath);// Compress zip Keep the original file

            psi.Arguments = string.Format("a -av- -ep1 -afrar {0}.rar {0}", floderPath);// Compress rar Keep the original file

            psi.Arguments = string.Format("m -df- -av- -ep1 -afzip {0}.rar {0}", floderPath);   // Compress zip Delete the original file

            psi.Arguments = string.Format("m -df- -av- -ep1 -afrar {0}.rar {0}", floderPath);// Compress rar Delete the original file

原网站

版权声明
本文为[AndroidOrCSharp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270550514126.html