【问题标题】:Encrypting JWT security token supported algorithms加密 JWT 安全令牌支持的算法
【发布时间】:2019-04-28 11:02:48
【问题描述】:

我正在尝试用这个 sn-p 对我的 JWt 进行签名和编码:

var claims = new Claim[] { new SomeClaimes() };
var scKey = Encoding.UTF8.GetBytes("SOME KEY");
var ecKey = Encoding.UTF8.GetBytes("SOME OTHER KEY");
var tokenDescriptor = new SecurityTokenDescriptor {
    Subject = new ClaimsIdentity(claims),
    SigningCredentials = new SigningCredentials(
        new SymmetricSecurityKey(
            scKey),
            SecurityAlgorithms.HmacSha512),
    EncryptingCredentials = new EncryptingCredentials(
        new SymmetricSecurityKey(
            ecKey),
            // I tryied all possible combination of algorithms here:
            SecurityAlgorithms.XXXX,
            SecurityAlgorithms.YYYY), 
    Issuer = "My Jwt Issuer",
    Audience = "My Jwt Audience",
    IssuedAt = DateTime.UtcNow,
    Expires = DateTime.Now.AddDays(7),
};
var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);
var jwt = tokenHandler.WriteToken(token);

但是当我运行代码时,我得到了错误:

加密失败。不支持:算法:“{0}”,SecurityKey:“{1}”。

在上面的代码中,{0} 和 {1} 是 XXXX 和 YYYY 的任意组合(是的,我写了一个反射 sn-p 并尝试了它们的所有可能组合)。支持哪些算法对签名的 JWT 进行编码(和解码)?

【问题讨论】:

    标签: c# .net-core jwt encryption-symmetric netcoreapp2.1


    【解决方案1】:

    HmacSha512 只使用一个密钥来签署或验证令牌,尝试像 RsaSha256 这样的算法来进行公钥/私钥加密。

    【讨论】:

    • 您的回答能否详细一点?也许附加一些代码示例?
    • 问题不在于拥有一个或多个密钥。请再次阅读问题。
    • 我不是 c# 专家,但 HmacSha512 不是 SymetricSecurityKey 尝试新的 ASymmetricSecurityKey 或类似的东西
    【解决方案2】:

    终于找到答案了:

    var claims = new Claim[] { new SomeClaimes() };
    var scKey = Encoding.UTF8.GetBytes("SOME KEY");
    var ecKeyTemp = Encoding.UTF8.GetBytes("SOME OTHER KEY");
    
    // Note that the ecKey should have 256 / 8 length:
    byte[] ecKey = new byte[256 / 8];
    Array.Copy(ecKeyTemp, ecKey, 256 / 8);
    
    var tokenDescriptor = new SecurityTokenDescriptor {
        Subject = new ClaimsIdentity(claims),
        SigningCredentials = new SigningCredentials(
            new SymmetricSecurityKey(
                scKey),
                SecurityAlgorithms.HmacSha512),
        EncryptingCredentials = new EncryptingCredentials(
            new SymmetricSecurityKey(
                ecKey),
                SecurityAlgorithms.Aes256KW,
                SecurityAlgorithms.Aes256CbcHmacSha512), 
        Issuer = "My Jwt Issuer",
        Audience = "My Jwt Audience",
        IssuedAt = DateTime.UtcNow,
        Expires = DateTime.Now.AddDays(7),
    };
    var tokenHandler = new JwtSecurityTokenHandler();
    var token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);
    var jwt = tokenHandler.WriteToken(token);
    

    如您所见,使用SecurityAlgorithms.Aes256KW 作为密钥加密算法 和SecurityAlgorithms.Aes256CbcHmacSha512 作为加密算法 就可以了。请注意,用于加密算法的密钥的长度应为256 / 8。

    【讨论】:

      猜你喜欢
      • 2013-08-15
      • 2018-11-12
      • 2015-07-05
      • 2019-07-25
      • 2020-07-15
      • 2013-09-20
      • 2019-12-30
      • 2016-07-20
      相关资源
      最近更新 更多