Unity AES encryption code

Unity notes need to encrypt project resources, so after query comparison, AES encryption is the best in terms of encryption speed and security, so I wrote encryption code, including, byte[]->byte[], byte[]->base64string, String->base64string, string->byte[] and other encryption and decryption methods, as well as asynchronous methods. In addition, the Key key must be a multiple of 16-bit bytes, and the Vector key vector must be 16-bit. All can be processed, and the Key will use 32 Bits, Key and Vector lengths can be filled in at will, the excess will be truncated, and the missing ones will be filled in repeatedly to make up for it.

See the script below. The asynchronous method provided in the script below needs to be extended by Task.GetAwaiter() otherwise an error will be reported. I have provided the extended tool class. Please pay attention to obtain it and send a private message to: 40002 to obtain the download address.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public class AESCryptoTool
{
    
    
    /// <summary>
    /// 密钥
    /// </summary>
    public static readonly string Key = "Rain-ProjectKeys";

    /// <summary>
    /// 缓存(10kb)
    /// </summary>
    private const int bufferSize = 1024 * 8 * 10;

    /// <summary>
    /// AES加密,任意文件
    /// </summary>
    /// <param name="data">被加密的明文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>密文</returns>
    public static byte[] Encrypt(byte[] data, string key=null, string vector=null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];//采用32位密码加密
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);//如果用户输入的密码不足32位,自动填充空格至32位
        byte[] bVector = new byte[16];//密钥向量,为16位
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);//如果用户定义的密钥向量不足16位,自动填充空格至16位
        byte[] Cryptograph = null;//加密后的密文
        Aes aes = Aes.Create();
        try
        {
    
    
            using (MemoryStream Memory = new MemoryStream())
            {
    
    
                //把内存流对象包装成加密流对象
                using (CryptoStream Encryptor = new CryptoStream(Memory, aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                {
    
    
                    using (MemoryStream tempMemory = new MemoryStream(data))
                    {
    
    
                        tempMemory.CopyTo(Encryptor, bufferSize);
                        Encryptor.FlushFinalBlock();
                        Cryptograph = Memory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            Cryptograph = null;
        }
        return Cryptograph;
    }

    /// <summary>
    /// AES加密,任意文件
    /// </summary>
    /// <param name="data">被加密的明文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>密文</returns>
    public static string EncryptToString(byte[] data, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];//采用32位密码加密
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);//如果用户输入的密码不足32位,自动填充空格至32位
        byte[] bVector = new byte[16];//密钥向量,为16位
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);//如果用户定义的密钥向量不足16位,自动填充空格至16位
        byte[] Cryptograph = null;//加密后的密文
        Aes aes = Aes.Create();
        try
        {
    
    
            using (MemoryStream Memory = new MemoryStream())
            {
    
    
                //把内存流对象包装成加密流对象
                using (CryptoStream Encryptor = new CryptoStream(Memory, aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                {
    
    
                    using (MemoryStream tempMemory = new MemoryStream(data))
                    {
    
    
                        tempMemory.CopyTo(Encryptor, bufferSize);
                        Encryptor.FlushFinalBlock();
                        Cryptograph = Memory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            Cryptograph = null;
        }
        return Cryptograph != null ? Convert.ToBase64String(Cryptograph) : string.Empty;
    }

    /// <summary>
    /// AES加密,任意文件
    /// </summary>
    /// <param name="data">被加密的明文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>密文</returns>
    public static async void Encrypt(byte[] data, Action<bool, byte[]>completed, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];//采用32位密码加密
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);//如果用户输入的密码不足32位,自动填充空格至32位
        byte[] bVector = new byte[16];//密钥向量,为16位
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);//如果用户定义的密钥向量不足16位,自动填充空格至16位
        byte[] Cryptograph = null;//加密后的密文
        Aes aes = Aes.Create();
        try
        {
    
    
            using (MemoryStream Memory = new MemoryStream())
            {
    
    
                //把内存流对象包装成加密流对象
                using (CryptoStream Encryptor = new CryptoStream(Memory, aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                {
    
    
                    using (MemoryStream tempMemory = new MemoryStream(data))
                    {
    
    
                        await tempMemory.CopyToAsync(Encryptor, bufferSize);
                        Encryptor.FlushFinalBlock();
                        Cryptograph = Memory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            Cryptograph = null;
        }
        completed?.Invoke(Cryptograph!=null&& Cryptograph.Length>0, Cryptograph);
    }

    /// <summary>
    /// AES加密,任意文件
    /// </summary>
    /// <param name="data">被加密的明文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>密文</returns>
    public static async void EncryptToString(byte[] data, Action<bool, string> completed, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];//采用32位密码加密
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);//如果用户输入的密码不足32位,自动填充空格至32位
        byte[] bVector = new byte[16];//密钥向量,为16位
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);//如果用户定义的密钥向量不足16位,自动填充空格至16位
        byte[] Cryptograph = null;//加密后的密文
        Aes aes = Aes.Create();
        try
        {
    
    
            using (MemoryStream Memory = new MemoryStream())
            {
    
    
                //把内存流对象包装成加密流对象
                using (CryptoStream Encryptor = new CryptoStream(Memory, aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                {
    
    
                    using (MemoryStream tempMemory = new MemoryStream(data))
                    {
    
    
                        await tempMemory.CopyToAsync(Encryptor, bufferSize);
                        Encryptor.FlushFinalBlock();
                        Cryptograph = Memory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            Cryptograph = null;
        }
        string content= Cryptograph != null ? Convert.ToBase64String(Cryptograph) : string.Empty;
        completed?.Invoke(content!=string.Empty, content);
    }

    /// <summary>
    /// AES解密,任意文件
    /// </summary>
    /// <param name="data">被解密的密文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>明文</returns>
    public static byte[] Decrypt(byte[] data, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
        byte[] bVector = new byte[16];
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);
        byte[] original = null;//解密后的明文
        Aes aes = Aes.Create();
        try
        {
    
    
            using (MemoryStream Memory = new MemoryStream(data))
            {
    
    
                //把内存流对象包装成加密对象
                using (CryptoStream Decryptor = new CryptoStream(Memory, aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                {
    
    
                    //明文存储区
                    using (MemoryStream originalMemory = new MemoryStream())
                    {
    
    
                        Decryptor.CopyTo(originalMemory, bufferSize);

                        original = originalMemory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            original = null;
        }
        return original;
    }

    /// <summary>
    /// AES解密,任意文件
    /// </summary>
    /// <param name="data">被解密的密文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>明文</returns>
    public static string DecryptToString(byte[] data, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
        byte[] bVector = new byte[16];
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);
        byte[] original = null;//解密后的明文
        Aes aes = Aes.Create();
        try
        {
    
    
            using (MemoryStream Memory = new MemoryStream(data))
            {
    
    
                //把内存流对象包装成加密对象
                using (CryptoStream Decryptor = new CryptoStream(Memory, aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                {
    
    
                    //明文存储区
                    using (MemoryStream originalMemory = new MemoryStream())
                    {
    
    
                        Decryptor.CopyTo(originalMemory, bufferSize);

                        original = originalMemory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            original = null;
        }
        return original != null ? Encoding.Default.GetString(original) : string.Empty;
    }

    /// <summary>
    /// AES解密,任意文件
    /// </summary>
    /// <param name="data">被解密的密文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>明文</returns>
    public static async void Decrypt(byte[] data, Action<bool,byte[]> completed, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
        byte[] bVector = new byte[16];
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);
        byte[] original = null;//解密后的明文
        Aes aes = Aes.Create();
        try
        {
    
    
            using (MemoryStream Memory = new MemoryStream(data))
            {
    
    
                //把内存流对象包装成加密对象
                using (CryptoStream Decryptor = new CryptoStream(Memory, aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                {
    
    
                    //明文存储区
                    using (MemoryStream originalMemory = new MemoryStream())
                    {
    
    
                        await Decryptor.CopyToAsync(originalMemory, bufferSize);

                        original = originalMemory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            original = null;
        }
        completed?.Invoke(original!=null&& original.Length>0, original);
        //return original;
    }

    /// <summary>
    /// AES解密,任意文件
    /// </summary>
    /// <param name="data">被解密的密文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>明文</returns>
    public static async void DecryptToString(byte[] data, Action<bool, string> completed, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
        byte[] bVector = new byte[16];
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);
        byte[] original = null;//解密后的明文
        Aes aes = Aes.Create();
        try
        {
    
    
            using (MemoryStream Memory = new MemoryStream(data))
            {
    
    
                //把内存流对象包装成加密对象
                using (CryptoStream Decryptor = new CryptoStream(Memory, aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                {
    
    
                    //明文存储区
                    using (MemoryStream originalMemory = new MemoryStream())
                    {
    
    
                        await Decryptor.CopyToAsync(originalMemory, bufferSize);
                        original = originalMemory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            original = null;
        }
        string content= original != null ? Encoding.Default.GetString(original) : string.Empty; 
        completed?.Invoke(content!=string.Empty, content);
    }

    /// <summary>
    /// AES加密,任意文件
    /// </summary>
    /// <param name="content">被加密的明文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>密文</returns>
    public static string Encrypt(string content, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];//采用32位密码加密
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);//如果用户输入的密码不足32位,自动填充空格至32位
        byte[] bVector = new byte[16];//密钥向量,为16位
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);//如果用户定义的密钥向量不足16位,自动填充空格至16位
        byte[] Cryptograph = null;//加密后的密文
        Aes aes = Aes.Create();
        try
        {
    
    
            var data = Encoding.Default.GetBytes(content);
            using (MemoryStream Memory = new MemoryStream())
            {
    
    
                //把内存流对象包装成加密流对象
                using (CryptoStream Encryptor = new CryptoStream(Memory, aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                {
    
    
                    using (MemoryStream tempMemory = new MemoryStream(data))
                    {
    
    
                        tempMemory.CopyTo(Encryptor, bufferSize);
                        Encryptor.FlushFinalBlock();
                        Cryptograph = Memory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            Cryptograph = null;
        }
        string backContent = Cryptograph != null ? Convert.ToBase64String(Cryptograph) : string.Empty;
        return backContent;
    }

    /// <summary>
    /// AES加密,任意文件
    /// </summary>
    /// <param name="content">被加密的明文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>密文</returns>
    public static async void Encrypt(string content,Action<bool,string>output, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];//采用32位密码加密
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);//如果用户输入的密码不足32位,自动填充空格至32位
        byte[] bVector = new byte[16];//密钥向量,为16位
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);//如果用户定义的密钥向量不足16位,自动填充空格至16位
        byte[] Cryptograph = null;//加密后的密文
        Aes aes = Aes.Create();
        try
        {
    
    
            var data = Encoding.Default.GetBytes(content);
            using (MemoryStream Memory = new MemoryStream())
            {
    
    
                //把内存流对象包装成加密流对象
                using (CryptoStream Encryptor = new CryptoStream(Memory, aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                {
    
    
                    using (MemoryStream tempMemory = new MemoryStream(data))
                    {
    
    
                        await tempMemory.CopyToAsync(Encryptor, bufferSize);
                        Encryptor.FlushFinalBlock();
                        Cryptograph = Memory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            Cryptograph = null;
        }
        string backContent = Cryptograph != null ? Convert.ToBase64String(Cryptograph) : string.Empty;
        output?.Invoke(backContent!=string.Empty, backContent);
    }

    /// <summary>
    /// AES加密,任意文件
    /// </summary>
    /// <param name="content">被加密的明文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>密文</returns>
    public static byte[] EncryptToBytes(string content, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];//采用32位密码加密
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);//如果用户输入的密码不足32位,自动填充空格至32位
        byte[] bVector = new byte[16];//密钥向量,为16位
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);//如果用户定义的密钥向量不足16位,自动填充空格至16位
        byte[] Cryptograph = null;//加密后的密文
        Aes aes = Aes.Create();
        try
        {
    
    
            var data = Encoding.Default.GetBytes(content);
            using (MemoryStream Memory = new MemoryStream())
            {
    
    
                //把内存流对象包装成加密流对象
                using (CryptoStream Encryptor = new CryptoStream(Memory, aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                {
    
    
                    using (MemoryStream tempMemory = new MemoryStream(data))
                    {
    
    
                        tempMemory.CopyTo(Encryptor, bufferSize);
                        Encryptor.FlushFinalBlock();
                        Cryptograph = Memory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            Cryptograph = null;
        }
        //string backContent = Cryptograph != null ? Convert.ToBase64String(Cryptograph) : string.Empty;
        return Cryptograph;
    }

    /// <summary>
    /// AES加密,任意文件
    /// </summary>
    /// <param name="content">被加密的明文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>密文</returns>
    public static async void EncryptToBytes(string content, Action<bool, byte[]>output, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];//采用32位密码加密
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);//如果用户输入的密码不足32位,自动填充空格至32位
        byte[] bVector = new byte[16];//密钥向量,为16位
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);//如果用户定义的密钥向量不足16位,自动填充空格至16位
        byte[] Cryptograph = null;//加密后的密文
        Aes aes = Aes.Create();
        try
        {
    
    
            var data = Encoding.Default.GetBytes(content);
            using (MemoryStream Memory = new MemoryStream())
            {
    
    
                //把内存流对象包装成加密流对象
                using (CryptoStream Encryptor = new CryptoStream(Memory, aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                {
    
    
                    using (MemoryStream tempMemory = new MemoryStream(data))
                    {
    
    
                        await tempMemory.CopyToAsync(Encryptor, bufferSize);
                        Encryptor.FlushFinalBlock();
                        Cryptograph = Memory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            Cryptograph = null;
        }
        //string backContent = Cryptograph != null ? Convert.ToBase64String(Cryptograph) : string.Empty;
        output?.Invoke(Cryptograph!=null, Cryptograph);
    }

    /// <summary>
    /// AES解密,任意文件
    /// </summary>
    /// <param name="content">被解密的密文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>明文</returns>
    public static string Decrypt(string content, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
        byte[] bVector = new byte[16];
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);
        byte[] original = null;//解密后的明文
        Aes aes = Aes.Create();
        try
        {
    
    
            var data = Convert.FromBase64String(content);
            using (MemoryStream Memory = new MemoryStream(data))
            {
    
    
                //把内存流对象包装成加密对象
                using (CryptoStream Decryptor = new CryptoStream(Memory, aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                {
    
    
                    //明文存储区
                    using (MemoryStream originalMemory = new MemoryStream())
                    {
    
    
                        Decryptor.CopyTo(originalMemory, bufferSize);
                        original = originalMemory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            original = null;
        }
        string backContent = original != null ? Encoding.Default.GetString(original) : string.Empty;
        return backContent;
    }

    /// <summary>
    /// AES解密,任意文件
    /// </summary>
    /// <param name="content">被解密的密文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>明文</returns>
    public static async void Decrypt(string content,Action<bool,string>output, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
        byte[] bVector = new byte[16];
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);
        byte[] original = null;//解密后的明文
        Aes aes = Aes.Create();
        try
        {
    
    
            var data = Convert.FromBase64String(content);
            using (MemoryStream Memory = new MemoryStream(data))
            {
    
    
                //把内存流对象包装成加密对象
                using (CryptoStream Decryptor = new CryptoStream(Memory, aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                {
    
    
                    //明文存储区
                    using (MemoryStream originalMemory = new MemoryStream())
                    {
    
    
                        await Decryptor.CopyToAsync(originalMemory, bufferSize);
                        original = originalMemory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            original = null;
        }
        string backContent = original != null ? Encoding.Default.GetString(original) : string.Empty;
        output?.Invoke(backContent!=string.Empty, backContent);
    }

    /// <summary>
    /// AES解密,任意文件
    /// </summary>
    /// <param name="content">被解密的密文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>明文</returns>
    public static byte[] DecryptToBytes(string content, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
        byte[] bVector = new byte[16];
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);
        byte[] original = null;//解密后的明文
        Aes aes = Aes.Create();
        try
        {
    
    
            var data = Convert.FromBase64String(content);
            using (MemoryStream Memory = new MemoryStream(data))
            {
    
    
                //把内存流对象包装成加密对象
                using (CryptoStream Decryptor = new CryptoStream(Memory, aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                {
    
    
                    //明文存储区
                    using (MemoryStream originalMemory = new MemoryStream())
                    {
    
    
                        Decryptor.CopyTo(originalMemory, bufferSize);
                        original = originalMemory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            original = null;
        }
        return original;
    }

    /// <summary>
    /// AES解密,任意文件
    /// </summary>
    /// <param name="content">被解密的密文</param>
    /// <param name="key">密钥</param>
    /// <param name="vector">密钥向量</param>
    /// <returns>明文</returns>
    public static async void DecryptToBytes(string content, Action<bool, byte[]>output, string key = null, string vector = null)
    {
    
    
        key = key == null || key == string.Empty ? Key : key;
        vector = vector == null || vector == string.Empty ? Key : vector;

        byte[] bKey = new byte[32];
        Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
        byte[] bVector = new byte[16];
        Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(bVector.Length)), bVector, bVector.Length);
        byte[] original = null;//解密后的明文
        Aes aes = Aes.Create();
        try
        {
    
    
            var data = Convert.FromBase64String(content);
            using (MemoryStream Memory = new MemoryStream(data))
            {
    
    
                //把内存流对象包装成加密对象
                using (CryptoStream Decryptor = new CryptoStream(Memory, aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                {
    
    
                    //明文存储区
                    using (MemoryStream originalMemory = new MemoryStream())
                    {
    
    
                        await Decryptor.CopyToAsync(originalMemory, bufferSize);
                        original = originalMemory.ToArray();
                    }
                }
            }
        }
        catch
        {
    
    
            original = null;
        }
        output?.Invoke(original!=null, original);
    }
}

Guess you like

Origin blog.csdn.net/weixin_43872129/article/details/131842098