MCPcopy Create free account
hub / github.com/mpdavis/python-jose / encrypt

Method encrypt

jose/backends/cryptography_backend.py:455–476  ·  view source on GitHub ↗
(self, plain_text, aad=None)

Source from the content-addressed store, hash-verified

453 return data
454
455 def encrypt(self, plain_text, aad=None):
456 plain_text = ensure_binary(plain_text)
457 try:
458 iv_byte_length = self.IV_BYTE_LENGTH_MODE_MAP.get(self._mode.name, algorithms.AES.block_size)
459 iv = get_random_bytes(iv_byte_length)
460 mode = self._mode(iv)
461 if mode.name == "GCM":
462 cipher = aead.AESGCM(self._key)
463 cipher_text_and_tag = cipher.encrypt(iv, plain_text, aad)
464 cipher_text = cipher_text_and_tag[: len(cipher_text_and_tag) - 16]
465 auth_tag = cipher_text_and_tag[-16:]
466 else:
467 cipher = Cipher(algorithms.AES(self._key), mode, backend=default_backend())
468 encryptor = cipher.encryptor()
469 padder = PKCS7(algorithms.AES.block_size).padder()
470 padded_data = padder.update(plain_text)
471 padded_data += padder.finalize()
472 cipher_text = encryptor.update(padded_data) + encryptor.finalize()
473 auth_tag = None
474 return iv, cipher_text, auth_tag
475 except Exception as e:
476 raise JWEError(e)
477
478 def decrypt(self, cipher_text, iv=None, aad=None, tag=None):
479 cipher_text = ensure_binary(cipher_text)

Callers 1

wrap_keyMethod · 0.45

Calls 3

ensure_binaryFunction · 0.85
get_random_bytesFunction · 0.85
JWEErrorClass · 0.85

Tested by

no test coverage detected