Skip to content

Commit

Permalink
🗓 Dec 29, 2024 1:12:40 PM
Browse files Browse the repository at this point in the history
🐛 closes #36
🔥 remove atbash_decode
🐙 atbash to follow cyberchefs implementation
  • Loading branch information
securisec committed Dec 29, 2024
1 parent 035bbfd commit 15beb6d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
2 changes: 1 addition & 1 deletion chepy/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "7.3.0" # pragma: no cover
__version__ = "7.4.0" # pragma: no cover
__author__ = "@securisec" # pragma: no cover
31 changes: 15 additions & 16 deletions chepy/modules/encryptionencoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ def affine_decode(self, a: int = 1, b: int = 1) -> EncryptionEncodingT:
return self

@ChepyDecorators.call_stack
def atbash_encode(self) -> EncryptionEncodingT:
def atbash(self) -> EncryptionEncodingT:
"""Encode with Atbash cipher
Returns:
Expand All @@ -1216,21 +1216,20 @@ def atbash_encode(self) -> EncryptionEncodingT:
>>> Chepy("secret").atbash_encode().o
"HVXIVG"
"""
self.state = pycipher.Atbash().encipher(self._convert_to_str(), keep_punct=True)
return self

@ChepyDecorators.call_stack
def atbash_decode(self) -> EncryptionEncodingT:
"""Decode Atbash cipher
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("hvxivg").atbash_decode().o
"SECRET"
"""
self.state = pycipher.Atbash().decipher(self._convert_to_str(), keep_punct=True)
key = 'ZYXWVUTSRQPONMLKJIHGFEDCBA'
data = self._convert_to_str()
ret = ''
arr = Ciphers.ATBASH
for c in data:
if c.isalpha():
if c.islower():
ret += key[arr[c.upper()]].lower()
else:
ret += key[arr[c]]
else:
ret += c
self.state = ret
# self.state = pycipher.Atbash().encipher(self._convert_to_str(), keep_punct=True)
return self

@ChepyDecorators.call_stack
Expand Down
3 changes: 1 addition & 2 deletions chepy/modules/encryptionencoding.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class EncryptionEncoding(ChepyCore):
def vigenere_decode(self: EncryptionEncodingT, key: str) -> EncryptionEncodingT: ...
def affine_encode(self: EncryptionEncodingT, a: int=..., b: int=...) -> EncryptionEncodingT: ...
def affine_decode(self: EncryptionEncodingT, a: int=..., b: int=...) -> EncryptionEncodingT: ...
def atbash_encode(self: EncryptionEncodingT) -> EncryptionEncodingT: ...
def atbash_decode(self: EncryptionEncodingT) -> EncryptionEncodingT: ...
def atbash(self: EncryptionEncodingT) -> EncryptionEncodingT: ...
def to_morse_code(self: EncryptionEncodingT, dot: str=..., dash: str=..., letter_delim: str=..., word_delim: str=...) -> EncryptionEncodingT: ...
def from_morse_code(self: EncryptionEncodingT, dot: str=..., dash: str=..., letter_delim: str=..., word_delim: str=...) -> EncryptionEncodingT: ...
def rsa_encrypt(self: EncryptionEncodingT, public_key: str, is_file: bool=False, passphrase: Union[str, None]=None, cipher:Literal['OAEP', 'PKCS']='OAEP') -> EncryptionEncodingT: ...
Expand Down
29 changes: 29 additions & 0 deletions chepy/modules/internal/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,35 @@ def __init__(self, char, freq):


class Ciphers:
ATBASH = {
"A": 0,
"B": 1,
"C": 2,
"D": 3,
"E": 4,
"F": 5,
"G": 6,
"H": 7,
"I": 8,
"J": 9,
"K": 10,
"L": 11,
"M": 12,
"N": 13,
"O": 14,
"P": 15,
"Q": 16,
"R": 17,
"S": 18,
"T": 19,
"U": 20,
"V": 21,
"W": 22,
"X": 23,
"Y": 24,
"Z": 25,
}

@staticmethod
def gen_polybius_square(keyword):
alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
Expand Down
6 changes: 1 addition & 5 deletions tests/test_encryptionencoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,7 @@ def test_affine_decode():


def test_atbash_encode():
assert Chepy("secret").atbash_encode().o == b"hvxivg".upper()


def test_atbash_decode():
assert Chepy("hvxivg").atbash_decode().o == b"secret".upper()
assert Chepy("AbCd1.2").atbash().o == b"ZyXw1.2"


def test_to_morse_code():
Expand Down

0 comments on commit 15beb6d

Please sign in to comment.