Exemplo n.º 1
0
 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)
                                  ));
 }
Exemplo n.º 2
0
 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)
                                  ));
 }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
 public IEncryption GetEncryptor(JweEnc enc)
 {
     return(_mappingJweEncToKeySize[enc]);
 }