.net core 使用Apereo CAS发布的网芯加密JWT解码
Apereo CAS单一登录发布了以下JWT.net core 使用Apereo CAS发布的网芯加密JWT解码,.net-core,jwt,cas,jwe,.net Core,Jwt,Cas,Jwe,Apereo CAS单一登录发布了以下JWT eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.ZXlKNmFYQWlPaUpFUlVZaUxDSmhiR2NpT2lKa2FYSWlMQ0psYm1NaU9pSkJNVEk0UTBKRExVaFRNalUySWl3aWRIbHdJam9pU2xkVUluMC4uREJXMFNUa19OSUVrMmFCVElJNHVnUS5qX3l4a1BVOHNzQ2tCUkdrdjN2RGI0QjFyVDRHWEhzRUJtTlJvTV
eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.ZXlKNmFYQWlPaUpFUlVZaUxDSmhiR2NpT2lKa2FYSWlMQ0psYm1NaU9pSkJNVEk0UTBKRExVaFRNalUySWl3aWRIbHdJam9pU2xkVUluMC4uREJXMFNUa19OSUVrMmFCVElJNHVnUS5qX3l4a1BVOHNzQ2tCUkdrdjN2RGI0QjFyVDRHWEhzRUJtTlJvTVR0d0hvMjc0NF96cHIzaW1JUk15ek1nU0RIcF9xcXFIQVVnNUNvM3JuM0FTQWUzS1VaMmFsR1B1SmIzT3UxN0hGR2t6a0RDS29CamdHdU9LVHowMlpfVWpYcm1hZXg0RDVyOUt1blNZYWxTWVBDTFU3TncyZEZVWHBwWnlrb3pQdXl6RFFadjZVMDlQaXgxa1gtLW9obUpFVUZJWlN6Vi1DQUs1RTRsSmtWU3ZrZ1gxSmVBMkVjNlY4aFJUSzFIMXRmb2t6Y09nNlJXNElWN05JNVJUQW8ycmlIVlVnLV94czZIY2JFcFZxR0hlVW5QNmF0Nl9zLTFnbGMzTTFzeHBJcmxBN3FEblhPUGt1ZjFmbnpwcEpFVmZWVTV2LWpOUDZQQ0ZnMjA2S2pjeDBYYlRVX0hWN1NyUUhyWUx2Y0gwVWlWVjljZGNOWmJ0Q2hjQjkwX2M2YXVRVmZjeEJZZjE1c2V0M2g3MzA0T0w5RmM5MG5fbXNyV3RIMnVYcGxfUEEwc29IMnJMM0xVV3YyS1E0WE5lR0V5eXpqRXBZWTU3VzJMazZreWQ2UU13RV9ndy5TenQ3WGFqbTBnSy11bk05djl5azdB.V50nzzET85j2FAMRGCLqN1sLXZ8WZrfH0G5__WL6UwvrjAZbvj9tjXAnwcIoBeyFU-zvIsjom520-p2JCNoqEg
我知道这是一个加密的JWT使用。加密密钥如下所示
9O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O8
9O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O89O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O8
签名秘密如下
9O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O8
9O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O89O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O8
用于正确解码此JWT的java代码如下所示
9O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O8
9O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O89O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O8
公共断言验证(字符串票证)抛出TicketValidationException{
试一试{
System.out.println(“票证=”+票证);
最终密钥=新AesKey(signingKey.getBytes(StandardCharsets.UTF_8));
final JsonWebSignature jws=新JsonWebSignature();
jws.setCompactSerialization(票据);
设置键(key);
如果(!jws.verifySignature()){
抛出新的TicketValidationException(“JWT验证失败”);
}
最终字节[]decodedBytes=Base64.decodeBase64(jws.getEncodedPayload().getBytes(StandardCharsets.utf8));
最终字符串decodedPayload=新字符串(decodedBytes,StandardCharsets.UTF_8);
final JsonWebEncryption jwe=新的JsonWebEncryption();
最终JsonWebKey JsonWebKey=JsonWebKey.Factory
.newJwk(“\n”+“{kty\”:\“oct\”,\n”+“\“k\”:\“+encryptionKey+”\n”+“}”);
setCompactSerialization(decodedPayload);
setKey(新的AesKey(jsonWebKey.getKey().getEncoded());
System.out.println(“JWT-->”+jwe.getPlaintextString());
JSONParser=newJSONParser(JSONParser.DEFAULT\u PERMISSIVE\u模式);
JSONObject json=(JSONObject)parser.parse(jwe.getPlaintextString());
返回新的断言mpl(json.getAsString(“sub”);
}捕获(JoseeException | TicketValidationException ex){
logger.error(Arrays.toString(例如getStackTrace());
}捕获(解析异常){
getLogger(CustomJWTValidator.class.getName()).log(Level.SEVERE,null,ex);
}
返回null;
}
我试图在NetCore2.2中解码相同的JWT。代码如下
9O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O8
9O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O89O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O8
var encryptionKey=“9O22VD7QJU3MBNHOY8VWZASH1PDIEWAJ4F9SI2Q-O8”;
var jsonWebKey=“\n”+“{\“kty\”:\“oct\”,\n“+”\“k\”:\“+encryptionKey+”\“\n”+“}”;
var jwkc=新的JsonWebKey(JsonWebKey);
services.AddAuthentication(x=>
{
x、 DefaultAuthenticateScheme=JwtBearerDefaults.AuthenticationScheme;
x、 DefaultChallengeScheme=JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(选项=>
{
options.RequireHttpsMetadata=false;
options.SaveToken=true;
options.TokenValidationParameters=新的TokenValidationParameters
{
ValidateSuersigningKey=false,
IssuerSigningKey=new Symmetrics Security Key(Encoding.ASCII.GetBytes(“9O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O89O22Vd7QJu3mBNhOy8vwZaSH1UPdieWAj4f9si2q-O8”),
validateisuer=false,
ValidateAudience=false,
ValidateLifetime=false,
TokenDecryptionKey=jwkc,
};
});
我故意禁用了签名验证和任何其他类型的验证。然而,在JWT验证中,我有以下错误
System.ArgumentException: IDX12723: Unable to decode the payload 'ZXlKNmF...5azdB' as Base64Url encoded string. jwtEncodedString: ''. ---> Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: e. Path '', line 0, position 0.
有效载荷似乎没有正确解码。到目前为止,很多谷歌搜索都没有结果。我能够完全加载、验证签名并解密令牌。 问题来自您收到的密钥:当签名密钥不是base64url安全编码时,encrypton密钥已经是base64url安全编码的 相应的JWK分别为(签名和加密): {“kty”:“oct”,“k”:“OU8YMLZKN1FKDTNTQK5OT3K4NDAYVVQZGLLV0FQNGY5C2KYCS1PODLPMJJWZDDRSNUZBUJOAE95OHZ3WMFTSDVUGRPZVDBAJRMOXNPMNETZG”} 及 {“kty”:“oct”,“k”:“9o22vd7qju3mbnhoy8vwzash1pdiewaj4f9si2q-O8”} 编辑:对于记录,我使用了(PHP库)和以下脚本:
谢谢。好主意。无论如何,我需要使用.net核心库来解码JWT。你可以使用我发布的2个JWK。如前所述,问题来自解密密钥。在线.newJwk(“\n”+“{kty\”:\“oct\”,\n”+“\“k\”:\“+encryptionKey+”\n”+“}”)
,变量encryptionKey
应为base64 url安全编码