Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions agentic_security/probe_data/stenography_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down
27 changes: 26 additions & 1 deletion agentic_security/probe_data/test_stenography_fn.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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")
Loading