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

📄 binaryencryptor.cs

📁 这是可以加密&解密字符串
💻 CS
字号:
using System.Security.Cryptography;
using System.IO;

namespace GTT
{
    public class BinaryEncryptor : BinaryCrypto
    {
        /// <remarks>
        /// 使用.Net SymmetricAlgorithm 类的构造器.
        /// </remarks>
        public BinaryEncryptor(SymmProvEnum netSelected)
            : base(netSelected)
        {
        }
        /// <remarks>
        /// 使用自定义SymmetricAlgorithm类的构造器.
        /// </remarks>
        public BinaryEncryptor(SymmetricAlgorithm ServiceProvider)
            : base(ServiceProvider)
        {
        }

        public override byte[] ExecuteBytes(byte[] inputBytes)
        {
            // create a MemoryStream so that the process can be done without I/O files
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            byte[] bytKey = GetLegalKey(_key);
            // set the private key
            _mobjCryptoService.Key = bytKey;
            _mobjCryptoService.IV = bytKey;
            // create an Encryptor from the Provider Service instance
            ICryptoTransform encrypto = _mobjCryptoService.CreateEncryptor();
            // create Crypto Stream that transforms a stream using the encryption
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
            // write out encrypted content into MemoryStream
            cs.Write(inputBytes, 0, inputBytes.Length);
            cs.FlushFinalBlock();
            // get the output and trim the '\0' bytes
            byte[] bytOut = ms.GetBuffer();
            return bytOut;
        }

        public override void ExecuteStream(Stream inputStream, Stream outputStream)
        {
            byte[] buffer = new byte[1024];
            byte[] buf = new byte[1032];
            int temp;
                // 读取文件
                do
                {
                    temp = inputStream.Read(buffer, 0, 1024);
                    buf = ExecuteBytes(buffer);
                    if (temp > 0)
                    {
                        outputStream.Write(buf, 0, temp + 8);
                    }
                    else
                        break;
                }
                while (true);
        }

        public override bool ExecuteFile(string inputfile, string outputfile)
        {
            byte[] buffer = new byte[1024];
            byte[] buf = new byte[1032];
            if (System.IO.File.Exists(inputfile))
            {
                // 定义文件读取类
                Stream inputStream = File.Open(inputfile, FileMode.Open);
                Stream outputStream = File.Create(outputfile);
                // 执行加密
                ExecuteStream(inputStream, outputStream);
                // 释放流资源
                inputStream.Close();
                outputStream.Close();
            }
            return true;
        }
    }
}

⌨️ 快捷键说明

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