• .Net Core AES加解密


    项目中token在传输过程中采用了AES加密,  网上找到的两篇博文都有写问题,在这里记录一下.Net Core 2.2代码中AES加解密的使用:

     //AES加密  传入,要加密的串和, 解密key
            public static string AESEncrypt(string input, string key = "dataplatform2019")
            {
                var encryptKey = Encoding.UTF8.GetBytes(key);
                var iv = Encoding.UTF8.GetBytes("1012132405963708"); //偏移量,最小为16
                using (var aesAlg = Aes.Create())
                {using (var encryptor = aesAlg.CreateEncryptor(encryptKey, iv))
                    {
                        using (var msEncrypt = new MemoryStream())
                        {
                            using (var csEncrypt = new CryptoStream(msEncrypt, encryptor,
                                CryptoStreamMode.Write))
    
                            using (var swEncrypt = new StreamWriter(csEncrypt))
                            {
                                swEncrypt.Write(input);
                            }
    var decryptedContent = msEncrypt.ToArray();
    
                            return Convert.ToBase64String(decryptedContent);
                        }
                    }
                }
            }

      解密过程:

     public static string DecryptString(string cipherText)
            {
                var fullCipher = Convert.FromBase64String(cipherText);
    
                var iv = Encoding.UTF8.GetBytes("1012132405963708");
            
                var key = Encoding.UTF8.GetBytes("dataplatform2019");
    
                using (var aesAlg = Aes.Create())
                {
                    using (var decryptor = aesAlg.CreateDecryptor(key, iv))
                    {
                        string result;
                        using (var msDecrypt = new MemoryStream(fullCipher))
                        {
                            using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                            {
                                using (var srDecrypt = new StreamReader(csDecrypt))
                                {
                                    result = srDecrypt.ReadToEnd();
                                }
                            }
                        }
    
                        return result;
                    }
                }
            }
  • 相关阅读:
    day22 sys模块(☆☆☆)json & pickle模块(☆☆☆☆)
    day22 OS模块
    day21 time时间模块
    day21 if __name__==""__main__""的用法
    day21 模块
    day20 装饰器 (装饰器=高阶函数+函数嵌套+闭包)加上参数
    day19 生产者模型-next与send用法详解-生产者消费者模型
    day19 生成器函数的好处
    zzq's sort [思维题]
    三元组 [01Trie]
  • 原文地址:https://www.cnblogs.com/gloryhope/p/11733691.html
Copyright © 2020-2023  润新知