示例#1
0
def main_cipher(text: str,
                password: str,
                algorithm: str,
                cipher: str = "Encrypt") -> str:
    """Chooses the necessary cipher and operation

    args:
        text: The text to encrypt/decrypt
        password: The password to encrypt/decrypt the text
        algorithm: The function to encrypt/decrypt the text
        cipher: Whether the user wants to encrypt or decrypt

    returns:
        The final result of the encryption/decryption process
    """

    # Convert both the plaintext/ciphertext and password to a list of bytes
    plaintext_bytes = codec.string_bytes(text)
    password_bytes = codec.string_bytes(password)

    bytes_result: List[int] = []

    if algorithm == "XOR":
        bytes_result = xor_cipher(plaintext_bytes, password_bytes)

    return codec.bytes_string(bytes_result)
示例#2
0
def test_other() -> None:
    """Characters that do not match the other groups"""

    # Words in different directions
    assert bytes_string(string_bytes("اختبار النص"),
                        'decrypt') == "اختبار النص"
    assert bytes_string(string_bytes("اليسار"), 'decrypt') == "اليسار"
示例#3
0
def test_emoji() -> None:
    """Unicode Emojis"""
    assert bytes_string(string_bytes("👱👱🏻👱🏼👱🏽👱🏾👱🏿"),
                        'decrypt') == "👱👱🏻👱🏼👱🏽👱🏾👱🏿"
    assert bytes_string(string_bytes("🧟‍♀️🧟‍♂️"), 'decrypt') == "🧟‍♀️🧟‍♂️"
    assert bytes_string(string_bytes("👨‍❤️‍💋‍👨👩‍👩‍👧‍👦️"),
                        'decrypt') == "👨‍❤️‍💋‍👨👩‍👩‍👧‍👦️"
示例#4
0
def main_cipher(text: str,
                password: str,
                algorithm: str,
                crypt: str = "Encrypt") -> str:
    """Chooses the necessary cipher and operation

    args:
        text: The text to encrypt/decrypt
        password: The password to encrypt/decrypt the text
        algorithm: The function to encrypt/decrypt the text
        crypt: Whether the user wants to encrypt or decrypt

    returns:
        The final result of the encryption/decryption process
    """

    # Newlines are added to the ciphertext to format it nicely
    # These newlines are removed when decrypting
    if crypt == 'Decrypt':
        text = text.replace('\n', '')

    # Convert both the plaintext/ciphertext and password to a list of bytes
    plaintext_bytes = codec.string_bytes(text, crypt)
    password_bytes = codec.string_bytes(password)

    bytes_result: List[int] = []

    if algorithm == "XOR":
        bytes_result = xor_cipher(plaintext_bytes, password_bytes)

    # Output of algorithm converted to a string
    final_output = codec.bytes_string(bytes_result, crypt)

    # Adds newlines to the ciphertext to format it nicely
    if crypt == 'Encrypt':
        line_break = 79
        final_output = '\n'.join(
            textwrap.wrap(final_output,
                          line_break,
                          replace_whitespace=False,
                          drop_whitespace=False))

    return final_output
示例#5
0
def test_random(text: str) -> None:
    """Tests with randomly generated strings"""
    assert bytes_string(string_bytes(text)) == text
示例#6
0
def test_simple() -> None:
    """More Simple characters"""
    assert bytes_string(string_bytes("Hello World")) == "Hello World"
    assert bytes_string(string_bytes("123ę")) == "123ę"
    assert bytes_string(string_bytes(" ")) == " "