Unity AES加密代码

Unity笔记,需要给项目资源加密,因此查询对比下来,加密速度和安全度都是AES加密最好,因此编写了加密代码,包括、byte[]->byte[]、byte[]->base64string、string->base64string、string->byte[]等加密解密方法,以及异步方法,另外,Key密钥必须是16位字节倍数,Vector密钥向量必须是16位,都以处理,Key会使用32位,Key和Vector长度可随意填写,超过的会截断,缺失的会重复填补进行补足。

脚本看下面,另下面脚本中提供的异步方法需要扩写Task.GetAwaiter()否则会报错,本人提供了扩写好的工具类,获取请关注后,私信发送:40002,获取下载地址。

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);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43872129/article/details/131842098