src/EasyFrameWork/Encrypt/EncryptService.cs

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

/* http://www.zkea.net/ 
 * Copyright (c) ZKEASOFT. All rights reserved. 
 * http://www.zkea.net/licenses */

using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using Easy.Mvc;
using Easy.Mvc.Extend;
using Microsoft.AspNetCore.Hosting;

namespace Easy.Encrypt
{
    public class EncryptService : IEncryptService
    {
        private const string Path = "/Encrypt/";
        private const string PrivateKeyFile = "Private.config";
        private const string PublicKeyFile = "Public.config";
        private readonly string _folder;
        private readonly string _privateKeyFileName;
        private readonly string _publicKeyFileName;
        public EncryptService(IWebHostEnvironment hostingEnvironment)
        {
            _folder = System.IO.Path.Combine(hostingEnvironment.ContentRootPath, Path);
            _privateKeyFileName = System.IO.Path.Combine(_folder, PrivateKeyFile);
            _publicKeyFileName = System.IO.Path.Combine(_folder, PublicKeyFile);
        }
        public byte[] Encrypt(byte[] source)
        {
            Func<byte[], byte[]> encrypt = sou =>
            {

                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
                {                    
                    if (!Directory.Exists(_folder))
                    {
                        Directory.CreateDirectory(_folder);
                    }
                    if (!File.Exists(_privateKeyFileName))
                    {
                        File.WriteAllText(_privateKeyFileName, rsa.ToXmlString(true));
                        File.WriteAllText(_publicKeyFileName, rsa.ToXmlString(false));
                    }
                    rsa.FromXmlString(File.ReadAllText(_publicKeyFileName));
                    int maxBlockSize = rsa.KeySize / 8 - 11;
                    if (sou.Length <= maxBlockSize)
                        return rsa.Encrypt(sou, false);

                    using (MemoryStream plaiStream = new MemoryStream(sou))
                    {
                        using (MemoryStream crypStream = new MemoryStream())
                        {
                            byte[] buffer = new byte[maxBlockSize];
                            int blockSize = plaiStream.Read(buffer, 0, maxBlockSize);

                            while (blockSize > 0)
                            {
                                byte[] toEncrypt = new byte[blockSize];
                                Array.Copy(buffer, 0, toEncrypt, 0, blockSize);
后面还有 98 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。