zoukankan      html  css  js  c++  java
  • netcore 下加密遇到的问题

       KeyedHashAlgorithm algorithm = KeyedHashAlgorithm.Create(algorithmName.ToString().ToUpper(CultureInfo.InvariantCulture));
                    if (null == algorithm)
                        throw new InvalidOperationException("Please specify a KeyedHashAlgorithm to use.");
    
                    try
                    {
                        algorithm.Key = key;
                        byte[] bytes = algorithm.ComputeHash(data);
                        return bytes;
                    }
                    finally
                    {
                        algorithm.Clear();
                    }

    这个代码会报异常,出现PNSE

    经查找各种文档发现一下使用办法是Ok的,有没有其它的办法呢?

      try
                    {
                        var keyBytes = Encoding.UTF8.GetBytes(key);
                        var hmac = new HMACSHA1(keyBytes);
                        byte[] bytes = hmac.ComputeHash(data);
                        return Convert.ToBase64String(bytes);
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                    finally
                    {
                    }

    HMACSHA256 hashAlgorithm = new HMACSHA256(key); return hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(data));
  • 相关阅读:
    自定义一个运行时异常
    对象的知识点正确解释
    decimal模块
    B+树
    Web框架系列之Tornado
    初识git
    Mysql表的操作
    MySQl创建用户和授权
    MySql安装和基本管理
    为什么用Mysql?
  • 原文地址:https://www.cnblogs.com/xyfy/p/7845235.html
Copyright © 2011-2022 走看看