public string GenerateJwe( string entry, JweAlg alg, JweEnc enc, JsonWebKey jsonWebKey) { return(PerformeJweGeneration(entry, alg, enc, jsonWebKey, (encryption, jweProtectedHeader) => encryption.Encrypt(entry, alg, jweProtectedHeader, jsonWebKey) )); }
public string GenerateJweByUsingSymmetricPassword( string entry, JweAlg alg, JweEnc enc, JsonWebKey jsonWebKey, string password) { return(PerformeJweGeneration(entry, alg, enc, jsonWebKey, (encryption, jweProtectedHeader) => encryption.EncryptWithSymmetricPassword(entry, alg, jweProtectedHeader, jsonWebKey, password) )); }
public async Task <string> EncryptAsync(string jwe, JweAlg jweAlg, JweEnc jweEnc) { var jsonWebKey = await GetJsonWebKey( jweAlg.ToAllAlg(), KeyOperations.Encrypt, Use.Enc); if (jsonWebKey == null) { return(jwe); } return(_jweGenerator.GenerateJwe( jwe, jweAlg, jweEnc, 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); }
public IEncryption GetEncryptor(JweEnc enc) { return(_mappingJweEncToKeySize[enc]); }