def test_rsa_oaep_encrypt(self): original_data = b'This is data to encrypt' private = asymmetric.load_private_key(os.path.join(fixtures_dir, 'keys/test.key')) public = asymmetric.load_public_key(os.path.join(fixtures_dir, 'keys/test.crt')) ciphertext = asymmetric.rsa_oaep_encrypt(public, original_data) self.assertIsInstance(ciphertext, byte_cls) plaintext = asymmetric.rsa_oaep_decrypt(private, ciphertext) self.assertEqual(original_data, plaintext)
def public_encrypt(key, data, oaep): """ public key encryption using rsa with pkcs1-oaep padding. returns the base64-encoded encrypted data data: the data to be encrypted, bytes key: pem-formatted key string or bytes oaep: whether to use oaep padding or not """ if isinstance(key, str): key = key.encode("ascii") pubkey = load_public_key(key) if oaep: encrypted = rsa_oaep_encrypt(pubkey, data) else: encrypted = rsa_pkcs1v15_encrypt(pubkey, data) return b64encode(encrypted).decode("ascii")
async def create_message(request): body = await request.post() session = await get_session(request) message = bytes(body['message'], 'UTF-8') date = datetime.strptime(body['date'], '%Y-%m-%d') public_key, private_key = asymmetric.generate_pair('rsa', bit_size=4096) ciphertext = asymmetric.rsa_oaep_encrypt(public_key, message) private_key = asymmetric.dump_private_key(private_key, None) user = await db.get_user(request.app, session['username']) message_uuid = uuid.uuid4().hex await db.create_message( request.app, { 'uuid': message_uuid, 'user': user.uuid, 'private_key': binascii.hexlify(private_key).decode('UTF-8'), 'ciphertext': binascii.hexlify(ciphertext).decode('UTF-8'), 'expires': date }) return web.HTTPFound(location=request.app.router['message_detail'].url_for( uuid=message_uuid))
def _encipher(self, plain_text): return rsa_oaep_encrypt(self.key[0], plain_text)