diff --git a/agentic_security/probe_data/stenography_fn.py b/agentic_security/probe_data/stenography_fn.py index 1ce14d13..335aea88 100644 --- a/agentic_security/probe_data/stenography_fn.py +++ b/agentic_security/probe_data/stenography_fn.py @@ -105,9 +105,9 @@ def caesar_cipher(text, shift=3): """Encrypts text using Caesar cipher with specified shift.""" result = [] for char in text: - if char.isupper(): + if "A" <= char <= "Z": result.append(chr((ord(char) + shift - 65) % 26 + 65)) - elif char.islower(): + elif "a" <= char <= "z": result.append(chr((ord(char) + shift - 97) % 26 + 97)) else: result.append(char) @@ -132,12 +132,14 @@ def substitution_cipher(text, key=None): def vigenere_cipher(text, key): """Encrypts text using Vigenère cipher with provided key.""" result = [] - key_length = len(key) - key_as_int = [ord(i) for i in key.lower()] + key_as_int = [ord(i) for i in key.lower() if "a" <= i <= "z"] + if not key_as_int: + raise ValueError("Vigenere key must contain at least one ASCII letter.") + key_length = len(key_as_int) text = text.lower() for i, char in enumerate(text): - if char.isalpha(): + if "a" <= char <= "z": shift = key_as_int[i % key_length] - 97 result.append(chr((ord(char) + shift - 97) % 26 + 97)) else: diff --git a/agentic_security/probe_data/test_stenography_fn.py b/agentic_security/probe_data/test_stenography_fn.py index be144023..1d93a544 100644 --- a/agentic_security/probe_data/test_stenography_fn.py +++ b/agentic_security/probe_data/test_stenography_fn.py @@ -1,4 +1,11 @@ -from .stenography_fn import CODE_BLOCK_TEMPLATES, code_block_encode +import pytest + +from .stenography_fn import ( + CODE_BLOCK_TEMPLATES, + caesar_cipher, + code_block_encode, + vigenere_cipher, +) class TestCodeBlockEncode: @@ -22,3 +29,21 @@ def test_multiline_prompt_kept_inside_docstring(self): result = code_block_encode(prompt) assert "line one" in result assert "line two" in result + + +class TestCiphersNonAscii: + def test_caesar_leaves_non_ascii_letters_alone(self): + assert caesar_cipher("école ß", 3) == "éfroh ß" + + def test_caesar_still_shifts_ascii(self): + assert caesar_cipher("Abc xyz!", 3) == "Def abc!" + + def test_vigenere_leaves_non_ascii_letters_alone(self): + assert vigenere_cipher("héllo", "key") == "réjvs" + + def test_vigenere_ignores_non_letters_in_key(self): + assert vigenere_cipher("abc", "k3y") == vigenere_cipher("abc", "ky") + + def test_vigenere_rejects_key_without_letters(self): + with pytest.raises(ValueError): + vigenere_cipher("abc", "123")