cryptencryptor.cs

来自「这是可以加密&解密字符串」· CS 代码 · 共 57 行

CS
57
字号
using System;
using System.IO;
using System.Security.Cryptography;

namespace EncryptDecrypt
{
    /// <summary>
    /// 加密类。
    /// </summary>
    public class CryptEncryptor
    {
        private Stream inputStream;
        private Stream outputStream;
        private CryptoStream cstream = null;

        public CryptEncryptor(Stream inputStream, Stream outputStream)
        {
            this.inputStream = inputStream;
            this.outputStream = outputStream;
        }

        public Stream InputStream
        {
            get { return inputStream; }
            set { inputStream = value; }
        }

        public Stream OutputStream
        {
            get { return outputStream; }
            set { outputStream = value; }
        }

        public void Execute()
        {
            //定义对称算法对象实例和接口
            SymmetricAlgorithm symm = new RijndaelManaged();
            ICryptoTransform transform = symm.CreateEncryptor();
            cstream = new CryptoStream(outputStream, transform, CryptoStreamMode.Write);

            BinaryReader br = new BinaryReader(inputStream);
            //读取源文件到CryptoStream 
            cstream.Write(br.ReadBytes((int)inputStream.Length), 0, (int)inputStream.Length);
            cstream.FlushFinalBlock();
        }

        private void Close()
        {
            if (cstream != null)
            {
                cstream.Close();
                cstream = null;
            }
        }
    }
}

⌨️ 快捷键说明

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