← run

bcb-0026

1.000
6/6 tests· lib-knowledge
Challenge · difficulty 2/5
# BigCodeBench/26

Implement a file **`solution.py`** that completes the function below. Keep the given name and signature; define `task_func` at module level.

Allowed libraries: `base64`, `cryptography`.

```python
import base64
from cryptography.fernet import Fernet

def task_func(message, encryption_key):
    """
    Encrypts a message with a symmetric encryption key using Fernet encryption, and then encode the 
    encrypted message using base64.

    Parameters:
    message (str): The message to be encrypted and encoded.
    encryption_key (str): The key used for symmetric encryption. It should be a string, which will 
                          be encoded to bytes, then URL-safe base64 encoded to conform to the requirements 
                          for Fernet (32 bytes after encoding).

    Returns:
    str: The base64 encoded encrypted message. The message is first encrypted using Fernet encryption, 
         then the result is base64 encoded.

    Requirements:
    - base64
    - cryptography.fernet

    Example:
    >>> encrypted_message = task_func('Hello, World!', '01234567890123456789012345678901')
    >>> isinstance(encrypted_message, str)
    True
    """
```

<!-- imported from BigCodeBench (BigCodeBench/26) -->
tests/test_bcb_0026.py
# Auto-generated from BigCodeBench BigCodeBench/26. Do not edit by hand.
import pathlib as _pathlib
exec(_pathlib.Path(__file__).with_name("solution.py").read_text(), globals())

import unittest
import base64
from cryptography.fernet import Fernet
class TestCases(unittest.TestCase):
    def test_case_1(self):
        # Test with a basic message and a valid encryption key.
        result = task_func('Hello, World!', '01234567890123456789012345678901')
        self.assertIsInstance(result, str)
        self.assertNotEqual(result, 'Hello, World!')
    def test_case_2(self):
        # Test with an empty message and a valid encryption key.
        result = task_func('', '01234567890123456789012345678901')
        self.assertIsInstance(result, str)
        self.assertNotEqual(result, '')
    def test_case_3(self):
        # Test with a numeric message and a valid encryption key.
        result = task_func('1234567890', '01234567890123456789012345678901')
        self.assertIsInstance(result, str)
        self.assertNotEqual(result, '1234567890')
    def test_case_4(self):
        # Test with a long message and a valid encryption key.
        long_message = 'A' * 500
        result = task_func(long_message, '01234567890123456789012345678901')
        self.assertIsInstance(result, str)
        self.assertNotEqual(result, long_message)
    def test_case_5(self):
        # Test with a basic message and an incorrectly formatted encryption key.
        with self.assertRaises(ValueError):
            task_func('Hello, World!', '0123456789')
    def test_case_6(self):
        # Test with a non-base64 but correct length key.
        with self.assertRaises(Exception):
            task_func('Hello, World!', '01234567890123456789012345678901'*2)  # Not base64-encoded
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.
Proposed solution
Looking at the requirements, I need to implement a function that:
1. Takes a message and encryption key as strings
2. Converts the key to bytes, then URL-safe base64 encodes it for Fernet
3. Uses Fernet to encrypt the message
4. Base64 encodes the encrypted result
5. Returns the final string

Let me analyze the requirements carefully:
- The encryption key should be a string that gets converted to bytes
- Then it needs to be URL-safe base64 encoded to create a valid Fernet key (32 bytes after decoding)
- Fernet encryption is applied to the message
- The encrypted result is then base64 encoded

```python
import base64
from cryptography.fernet import Fernet

def task_func(message, encryption_key):
    """
    Encrypts a message with a symmetric encryption key using Fernet encryption, and then encode the 
    encrypted message using base64.

    Parameters:
    message (str): The message to be encrypted and encoded.
    encryption_key (str): The key used for symmetric encryption. It should be a string, which will 
                          be encoded to bytes, then URL-safe base64 encoded to conform to the requirements 
                          for Fernet (32 bytes after encoding).

    Returns:
    str: The base64 encoded encrypted message. The message is first encrypted using Fernet encryption, 
         then the result is base64 encoded.

    Requirements:
    - base64
    - cryptography.fernet

    Example:
    >>> encrypted_message = task_func('Hello, World!', '01234567890123456789012345678901')
    >>> isinstance(encrypted_message, str)
    True
    """
    # Convert the encryption key string to bytes
    key_bytes = encryption_key.encode('utf-8')
    
    # URL-safe base64 encode the key to create a valid Fernet key (32 bytes after decoding)
    fernet_key = base64.urlsafe_b64encode(key_bytes)
    
    # Create a Fernet instance with the key
    fernet = Fernet(fernet_key)
    
    # Encrypt the message (convert to bytes first)
    encrypted_message = fernet.encrypt(message.encode('utf-8'))
    
    # Base64 encode the encrypted message
    encoded_message = base64.b64encode(encrypted_message).decode('utf-8')
    
    return encoded_message
```

Test output (stdout)

......                                                                   [100%]
6 passed in 0.05s