Skip to content

Commit

Permalink
馃棑 Oct 26, 2022 11:45:56 PM
Browse files Browse the repository at this point in the history
馃敡 closes #25
馃憠 add CFB for AES
  • Loading branch information
securisec committed Oct 27, 2022
1 parent 230b61a commit 602ca9b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 45 deletions.
18 changes: 14 additions & 4 deletions chepy/modules/encryptionencoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,8 @@ def aes_encrypt(
key_format: str = "hex",
iv_format: str = "hex",
) -> EncryptionEncodingT:
"""Encrypt raw state with AES
"""Encrypt raw state with AES.
CFB mode reflects Cyberchef and not native python behaviour.
Args:
key (str): Required. The secret key
Expand All @@ -621,14 +622,18 @@ def aes_encrypt(
b"5fb8c186394fc399849b89d3b6605fa3"
"""

assert mode in ["CBC", "OFB", "CTR", "ECB", "GCM"], "Not a valid mode."
assert mode in ["CBC", "CFB", "OFB", "CTR", "ECB", "GCM"], "Not a valid mode."

key, iv = self._convert_key(key, iv, key_format, iv_format)

if mode == "CBC":
cipher = AES.new(key, mode=AES.MODE_CBC, iv=iv)
self.state = cipher.encrypt(Padding.pad(self._convert_to_bytes(), 16))
return self
elif mode == "CFB":
cipher = AES.new(key, mode=AES.MODE_CFB, iv=iv, segment_size=128)
self.state = cipher.encrypt(self._convert_to_bytes())
return self
elif mode == "ECB":
cipher = AES.new(key, mode=AES.MODE_ECB)
self.state = cipher.encrypt(Padding.pad(self._convert_to_bytes(), 16))
Expand Down Expand Up @@ -659,7 +664,8 @@ def aes_decrypt(
key_format: str = "hex",
iv_format: str = "hex",
) -> EncryptionEncodingT:
"""Decrypt raw state encrypted with DES.
"""Decrypt raw state encrypted with DES.
CFB mode reflects Cyberchef and not native python behaviour.
Args:
key (str): Required. The secret key
Expand All @@ -680,14 +686,18 @@ def aes_decrypt(
b"some data"
"""

assert mode in ["CBC", "OFB", "CTR", "ECB", "GCM"], "Not a valid mode."
assert mode in ["CBC", "CFB", "OFB", "CTR", "ECB", "GCM"], "Not a valid mode."

key, iv = self._convert_key(key, iv, key_format, iv_format)

if mode == "CBC":
cipher = AES.new(key, mode=AES.MODE_CBC, iv=iv)
self.state = Padding.unpad(cipher.decrypt(self._convert_to_bytes()), 16)
return self
if mode == "CFB":
cipher = AES.new(key, mode=AES.MODE_CFB, iv=iv, segment_size=128)
self.state = cipher.decrypt(self._convert_to_bytes())
return self
elif mode == "ECB":
cipher = AES.new(key, mode=AES.MODE_ECB)
self.state = Padding.unpad(cipher.decrypt(self._convert_to_bytes()), 16)
Expand Down
4 changes: 2 additions & 2 deletions chepy/modules/encryptionencoding.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class EncryptionEncoding(ChepyCore):
def des_decrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]=..., key_mode: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def triple_des_encrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]=..., key_mode: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def triple_des_decrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]=..., key_mode: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def aes_encrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB", "GCM"]=..., key_mode: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def aes_decrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB", "GCM"]=..., key_mode: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def aes_encrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "CFB", "OFB", "CTR", "ECB", "GCM"]=..., key_mode: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def aes_decrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "CFB", "OFB", "CTR", "ECB", "GCM"]=..., key_mode: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def blowfish_encrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]=..., key_mode: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def blowfish_decrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]=..., key_mode: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def vigenere_encode(self: EncryptionEncodingT, key: str) -> EncryptionEncodingT: ...
Expand Down
92 changes: 53 additions & 39 deletions tests/test_encryptionencoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ def test_aes_encrypt():
.o
== b"5fb8c186394fc399849b89d3b6605fa3"
)
assert (
Chepy("some data")
.aes_encrypt("secret password!", mode="CFB", key_format="utf8")
.to_hex()
.o
== b"4ab2b4f72e9d92960b"
)
assert (
Chepy("some data")
.aes_encrypt("secret password!", mode="CTR", key_format="utf8")
Expand All @@ -298,122 +305,129 @@ def test_aes_encrypt():
)


def test_des_decrypt():
def test_aes_decrypt():
assert (
Chepy("1ee5cb52954b211d1acd6e79c598baac")
Chepy("5fb8c186394fc399849b89d3b6605fa3")
.hex_to_str()
.des_decrypt("70617373776f7264")
.aes_decrypt("7365637265742070617373776f726421")
.o
== b"some data"
)
assert (
Chepy("1ee5cb52954b211d1acd6e79c598baac")
Chepy("5fb8c186394fc399849b89d3b6605fa3")
.hex_to_str()
.des_decrypt("password", key_format="utf8")
.aes_decrypt("secret password!", key_format="utf8")
.o
== b"some data"
)
assert (
Chepy("1ee5cb52954b211da5b1a0072da15156")
Chepy("5fb8c186394fc399849b89d3b6605fa3")
.hex_to_str()
.des_decrypt("password", mode="ECB", key_format="utf8")
.aes_decrypt("secret password!", mode="ECB", key_format="utf8")
.o
== b"some data"
)
assert (
Chepy("0b7399049b0267d93d")
Chepy("4ab2b4f72e9d92960b")
.hex_to_str()
.des_decrypt("password", mode="CTR", key_format="utf8")
.aes_decrypt("secret password!", mode="CFB", key_format="utf8")
.o
== b"some data"
)
assert (
Chepy("0b7399049b0267d9a9")
Chepy("4ab2b4f72e9d92960b")
.hex_to_str()
.des_decrypt("password", mode="OFB", key_format="utf8")
.aes_decrypt("secret password!", mode="CTR", key_format="utf8")
.o
== b"some data"
)


def test_triple_des_decrypt():
assert (
Chepy("f8b27a0d8c837edce87dd13a1ab41f96")
Chepy("97a6227556b2be0763")
.hex_to_str()
.triple_des_decrypt("7375706572207365637265742070617373776f7264202121")
.aes_decrypt("secret password!", mode="GCM", key_format="utf8")
.o
== b"some data"
)
assert (
Chepy("f8b27a0d8c837edce87dd13a1ab41f96")
Chepy("4ab2b4f72e9d92960b")
.hex_to_str()
.triple_des_decrypt("super secret password !!", key_format="utf8")
.aes_decrypt("secret password!", mode="OFB", key_format="utf8")
.o
== b"some data"
)


def test_des_decrypt():
assert (
Chepy("f8b27a0d8c837edc8fb00ea85f502fb4")
Chepy("1ee5cb52954b211d1acd6e79c598baac")
.hex_to_str()
.triple_des_decrypt("super secret password !!", mode="ECB", key_format="utf8")
.des_decrypt("70617373776f7264")
.o
== b"some data"
)
assert (
Chepy("51710aefbd5bbb5b40")
Chepy("1ee5cb52954b211d1acd6e79c598baac")
.hex_to_str()
.triple_des_decrypt("super secret password !!", mode="CTR", key_format="utf8")
.des_decrypt("password", key_format="utf8")
.o
== b"some data"
)
assert (
Chepy("51710aefbd5bbb5bf9")
Chepy("1ee5cb52954b211da5b1a0072da15156")
.hex_to_str()
.triple_des_decrypt("super secret password !!", mode="OFB", key_format="utf8")
.des_decrypt("password", mode="ECB", key_format="utf8")
.o
== b"some data"
)


def test_aes_decrypt():
assert (
Chepy("5fb8c186394fc399849b89d3b6605fa3")
Chepy("0b7399049b0267d93d")
.hex_to_str()
.aes_decrypt("7365637265742070617373776f726421")
.des_decrypt("password", mode="CTR", key_format="utf8")
.o
== b"some data"
)
assert (
Chepy("5fb8c186394fc399849b89d3b6605fa3")
Chepy("0b7399049b0267d9a9")
.hex_to_str()
.aes_decrypt("secret password!", key_format="utf8")
.des_decrypt("password", mode="OFB", key_format="utf8")
.o
== b"some data"
)


def test_triple_des_decrypt():
assert (
Chepy("5fb8c186394fc399849b89d3b6605fa3")
Chepy("f8b27a0d8c837edce87dd13a1ab41f96")
.hex_to_str()
.aes_decrypt("secret password!", mode="ECB", key_format="utf8")
.triple_des_decrypt("7375706572207365637265742070617373776f7264202121")
.o
== b"some data"
)
assert (
Chepy("4ab2b4f72e9d92960b")
Chepy("f8b27a0d8c837edce87dd13a1ab41f96")
.hex_to_str()
.aes_decrypt("secret password!", mode="CTR", key_format="utf8")
.triple_des_decrypt("super secret password !!", key_format="utf8")
.o
== b"some data"
)
assert (
Chepy("97a6227556b2be0763")
Chepy("f8b27a0d8c837edc8fb00ea85f502fb4")
.hex_to_str()
.aes_decrypt("secret password!", mode="GCM", key_format="utf8")
.triple_des_decrypt("super secret password !!", mode="ECB", key_format="utf8")
.o
== b"some data"
)
assert (
Chepy("4ab2b4f72e9d92960b")
Chepy("51710aefbd5bbb5b40")
.hex_to_str()
.aes_decrypt("secret password!", mode="OFB", key_format="utf8")
.triple_des_decrypt("super secret password !!", mode="CTR", key_format="utf8")
.o
== b"some data"
)
assert (
Chepy("51710aefbd5bbb5bf9")
.hex_to_str()
.triple_des_decrypt("super secret password !!", mode="OFB", key_format="utf8")
.o
== b"some data"
)
Expand Down

0 comments on commit 602ca9b

Please sign in to comment.