⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 自己写的可扩展插件文件管理器
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Security.Cryptography;
using Core.Services.Sdk;
using System.Windows.Forms;
using System.Threading;

namespace cryptprov
{
    

    [Plugin(true)]
    public class CryptProvider : IPlugin
    {
        private PluginInfo p;
        private FileStream fs;
        private string passcode;
        private PassDialog f;

        private string PrevFileName;
        private int FileLen=0;
        private int Max = 0, Curr = 0;

        const int FIXED_HEADER_LEN = 24;//文件头定长,不写入文件

        private byte[] HEADER = { 0x06, 0x8b, 0xc9, 0x3e };//表示文件为uef文件,4bytes

        private int FILE_LEN;//FILE块长度,4bytes
        private byte[] FILE;//原文件名(Unicode)
        private byte[] HASH;//DATA块的哈希值,16bytes

        private byte[] ProcessData(byte[] Data)
        {
            byte[] ret = new byte[Data.Length];
            int codeLen=passcode.Length;
            for (int i = 0; i < Data.Length; i++)
            {
                byte xorVal = (byte)passcode[i % codeLen];
                ret[i] = (byte)(Data[i] ^ xorVal);
                Curr++;
                OnProgressChanged(new ProgressChangedEventArgs(Curr,Max+FIXED_HEADER_LEN +FileLen+1));
            }
            return ret;
        }

        private void WriteHeader(FileStream targetFile)
        {
            targetFile.Seek(0, SeekOrigin.Begin);
            BinaryWriter bw = new BinaryWriter(targetFile);
            bw.Write(HEADER);
            bw.Write(FILE_LEN);
            bw.Write(FILE);
            bw.Write(HASH);
        }

        private bool ReadHeader(FileStream targetFile)
        {
            byte[] rHEADER = new byte[4];
            byte[] cHASH = new byte[16];
            byte[] rHASH = new byte[16];
            int rFILE_LEN;
            BinaryReader br = new BinaryReader(targetFile);
            rHEADER=br.ReadBytes(4);
            if ((rHEADER[0] != HEADER[0]) || (rHEADER[1] != HEADER[1]) || (rHEADER[2] != HEADER[2]) || (rHEADER[3] != HEADER[3]))
                return false;
            rFILE_LEN = br.ReadInt32();
            byte[] rFILE = new byte[rFILE_LEN];
            rFILE=br.ReadBytes(rFILE_LEN);
            rHASH = br.ReadBytes(16);
            MD5CryptoServiceProvider md = new MD5CryptoServiceProvider();
            cHASH=md.ComputeHash(br.ReadBytes((int)(br.BaseStream.Length - FIXED_HEADER_LEN - rFILE.Length)));
            int i=0;
            foreach (byte b in cHASH)
            {
                if (b!=rHASH[i])
                    return false;
                else i++;
            }
            PrevFileName= Encoding.Unicode.GetString(rFILE);
            FileLen = rFILE_LEN;
            return true;
        }

        public CryptProvider()
        {
            p = new PluginInfo("Signercompress Exclusive De-En-Cryptor", "Signergenix", "使用简单的算法加密/解密文件", new Version(1, 0, 3051, 24320), new ToolStripMenuItem("加密/解密"));
        }

        #region IPlugin 成员


        public void AssignTask(System.Collections.ArrayList Targets)
        {
                foreach (string s in Targets)
                {
                    FileInfo fi = new FileInfo(s);
                    Max += (int)fi.Length;
                }
                if (Max > 0)
                {
                    foreach (string s in Targets)
                    {
                        Directory.SetCurrentDirectory(new FileInfo(s).DirectoryName);
                        fs = new FileStream(s, FileMode.Open);
                        //////////////////////////////////////////////////////////////////////////////////
                        if (ReadHeader(fs))
                        {
                            fs.Position = FIXED_HEADER_LEN + FileLen;
                            byte[] buff = new byte[(int)fs.Length - FIXED_HEADER_LEN - FileLen];
                            fs.Read(buff, 0, buff.Length);
                            FileStream f = new FileStream(PrevFileName, FileMode.Create);
                            f.Write(ProcessData(buff), 0, buff.Length);
                            fs.Close();
                            f.Close();
                            File.Delete(s);
                            OnProgressChanged(new ProgressChangedEventArgs(Max, Max));
                        }
                        ///////////////////////////////////////////////////////////////////////////////
                        else
                        {
                            byte[] buff = new byte[(int)fs.Length];
                            fs.Seek(0, SeekOrigin.Begin);
                            fs.Read(buff, 0, buff.Length);
                            fs.Close();
                            buff = ProcessData(buff);
                            FILE = Encoding.Unicode.GetBytes(new FileInfo(s).Name);
                            FILE_LEN = FILE.Length;
                            MD5CryptoServiceProvider md = new MD5CryptoServiceProvider();
                            HASH = new byte[16];
                            HASH = md.ComputeHash(buff);
                            File.Delete(s);
                            try
                            {
                                fs = new FileStream(s.Remove(s.LastIndexOf(".")) + ".uef", FileMode.Create);
                                WriteHeader(fs);
                                fs.Write(buff, 0, buff.Length);
                                fs.Close();
                                OnProgressChanged(new ProgressChangedEventArgs(Max, Max));
                            }
                            catch (Exception e)
                            {
                                MessageBox.Show(e.Message);
                            }
                        }
                    }
                }
        }

        public PluginInfo PluginInfo
        {
            get { return p; }
        }

        public event ProgressChangedEventHandler ProgressChanged;

        protected virtual void OnProgressChanged(ProgressChangedEventArgs e)
        {
            if (ProgressChanged != null)
            {
                ProgressChanged(this, e);
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            try
            {
                fs.Close();
            }
            finally
            {
                
            }
        }

        #endregion

        #region IPlugin 成员

        public Form Window
        {
            get
            {
                f=new PassDialog();
                f.FormClosing += new FormClosingEventHandler(f_FormClosing);
                return f;
            }
        }

        void f_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (f.DialogResult == DialogResult.OK)
                passcode = f.Passcode;
            else
                OnProgressChanged(new ProgressChangedEventArgs(1, 1));
        }

        #endregion
    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -