/// <summary> /// Try to parse the Json Web Encrypted token. /// Returns the Json Web Signed token otherwise null. /// </summary> /// <param name="jwe"></param> /// <param name="jsonWebKey"></param> /// <returns></returns> public string Parse( string jwe, JsonWebKey jsonWebKey) { const string emptyResult = null; var header = GetHeader(jwe); if (header == null) { return(emptyResult); } var algorithmName = header.Alg; var encryptionName = header.Enc; if (!Constants.MappingNameToJweAlgEnum.Keys.Contains(algorithmName) || !Constants.MappingNameToJweEncEnum.Keys.Contains(encryptionName)) { return(emptyResult); } var algorithmEnum = Constants.MappingNameToJweAlgEnum[algorithmName]; var encryptionEnum = Constants.MappingNameToJweEncEnum[encryptionName]; var algorithm = _jweHelper.GetEncryptor(encryptionEnum); return(algorithm.Decrypt(jwe, algorithmEnum, jsonWebKey)); }
private string PerformeJweGeneration( string entry, JweAlg alg, JweEnc enc, JsonWebKey jsonWebKey, Func <IEncryption, JweProtectedHeader, AesEncryptionResult> callback) { var algo = Constants.MappingNameToJweAlgEnum .SingleOrDefault(k => k.Value == alg); var encryption = Constants.MappingNameToJweEncEnum .SingleOrDefault(k => k.Value == enc); if (jsonWebKey == null || algo.IsDefault() || encryption.IsDefault()) { return(entry); } // Construct the JWE protected header var jweProtectedHeader = new JweProtectedHeader { Alg = algo.Key, Enc = encryption.Key, Kid = jsonWebKey.Kid }; var algorithm = _jweHelper.GetEncryptor(enc); var encryptionResult = callback( algorithm, jweProtectedHeader); var base64EncodedjweProtectedHeaderSerialized = jweProtectedHeader.SerializeWithDataContract().Base64Encode(); var base64EncodedJweEncryptedKey = encryptionResult.EncryptedContentEncryptionKey.Base64EncodeBytes(); var base64EncodedIv = encryptionResult.Iv.Base64EncodeBytes(); var base64EncodedCipherText = encryptionResult.CipherText.Base64EncodeBytes(); var base64EncodedAuthenticationTag = encryptionResult.AuthenticationTag.Base64EncodeBytes(); return(base64EncodedjweProtectedHeaderSerialized + "." + base64EncodedJweEncryptedKey + "." + base64EncodedIv + "." + base64EncodedCipherText + "." + base64EncodedAuthenticationTag); }