首页 > 编程语言 >C# Aes对称加密

C# Aes对称加密

时间:2023-02-21 15:36:40浏览次数:38  
标签:Aes 加密 string C# aes dataArr result byte

Aes Base64加密

    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="data"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string Encryption(string data, string key)
    {
        byte[] keyArr = Encoding.UTF8.GetBytes(key);
        byte[] dataArr = Encoding.UTF8.GetBytes(data);

        using var aes = Aes.Create();
        aes.Key = keyArr;
        aes.Mode = CipherMode.ECB;
        aes.Padding = PaddingMode.PKCS7;

        using var cryptoTransform = aes.CreateEncryptor();
        byte[] result = cryptoTransform.TransformFinalBlock(dataArr, 0, dataArr.Length);

        return Convert.ToBase64String(result);
    }

Aes Base64解密

    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="data"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string Decryption(string data, string key)
    {
        byte[] keyArr = Encoding.UTF8.GetBytes(key);
        byte[] dataArr = Convert.FromBase64String(data);

        using var aes = Aes.Create();
        aes.Key = keyArr;
        aes.Mode = CipherMode.ECB;
        aes.Padding = PaddingMode.PKCS7;

        using var cryptoTransform = aes.CreateDecryptor();
        byte[] result = cryptoTransform.TransformFinalBlock(dataArr, 0, dataArr.Length);

        return Convert.ToBase64String(result);
    }

标签:Aes,加密,string,C#,aes,dataArr,result,byte
From: https://www.cnblogs.com/kele-cc/p/17141153.html

相关文章