Skip to content

Commit

Permalink
🗓 May 27, 2023 12:04:27 PM
Browse files Browse the repository at this point in the history
✨ chacha20 encrypt/decrypt
  • Loading branch information
securisec committed May 27, 2023
1 parent 8794970 commit 71de3c8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Misc:
☐ cyberchef recipe to chepy recipe converter

Archive:
✔ ✨ cha cha encode, decode
✔ ✨ monoalphabetic substitution
✔ ✨ from nato
✔ for join_by handle int
Expand Down
53 changes: 53 additions & 0 deletions chepy/modules/encryptionencoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
AES = lazy_import.lazy_module("Crypto.Cipher.AES")
ARC4 = lazy_import.lazy_module("Crypto.Cipher.ARC4")
DES = lazy_import.lazy_module("Crypto.Cipher.DES")
ChaCha20 = lazy_import.lazy_module("Crypto.Cipher.ChaCha20")
DES3 = lazy_import.lazy_module("Crypto.Cipher.DES3")
RSA = lazy_import.lazy_module("Crypto.PublicKey.RSA")
Hash = lazy_import.lazy_module("Crypto.Hash")
Expand Down Expand Up @@ -497,6 +498,58 @@ def des_decrypt(
self.state = cipher.decrypt(self._convert_to_bytes())
return self

@ChepyDecorators.call_stack
def chacha_encrypt(
self,
key: str,
nonce: str = "0000000000000000",
key_format: str = "hex",
nonce_format: str = "hex",
) -> EncryptionEncodingT:
"""Encrypt raw state with ChaCha 20 rounds
Args:
key (str): Required. The secret key
nonce (str, optional): Nonce. Defaults to '0000000000000000'.
key_format (str, optional): Format of key. Defaults to 'hex'.
nonce_format (str, optional): Format of nonce. Defaults to 'hex'.
Returns:
Chepy: The Chepy object.
"""

key, nonce = self._convert_key(key, nonce, key_format, nonce_format)

cipher = ChaCha20.new(key=key, nonce=nonce)
self.state = cipher.encrypt(self._convert_to_bytes())
return self

@ChepyDecorators.call_stack
def chacha_decrypt(
self,
key: str,
nonce: str = "0000000000000000",
key_format: str = "hex",
nonce_format: str = "hex",
) -> EncryptionEncodingT:
"""Decrypt raw state encrypted with ChaCha 20 rounds.
Args:
key (str): Required. The secret key
nonce (str, optional): nonce for certain modes only. Defaults to '0000000000000000'.
key_format (str, optional): Format of key. Defaults to 'hex'.
nonce_format (str, optional): Format of nonce. Defaults to 'hex'.
Returns:
Chepy: The Chepy object.
"""

key, nonce = self._convert_key(key, nonce, key_format, nonce_format)

cipher = ChaCha20.new(key=key, nonce=nonce)
self.state = cipher.decrypt(self._convert_to_bytes())
return self

@ChepyDecorators.call_stack
def triple_des_encrypt(
self,
Expand Down
2 changes: 2 additions & 0 deletions chepy/modules/encryptionencoding.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class EncryptionEncoding(ChepyCore):
def rc4_decrypt(self: EncryptionEncodingT, key: str, key_format: RC4_FORMAT=...) -> EncryptionEncodingT: ...
def des_encrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]=..., key_format: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def des_decrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]=..., key_format: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def chacha_encrypt(self: EncryptionEncodingT, key: str, nonce: str=..., key_format: FORMAT=..., nonce_format: FORMAT=...) -> EncryptionEncodingT: ...
def chacha_decrypt(self: EncryptionEncodingT, key: str, nonce: str=..., key_format: FORMAT=..., nonce_format: FORMAT=...) -> EncryptionEncodingT: ...
def triple_des_encrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]=..., key_format: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def triple_des_decrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]=..., key_format: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
def aes_encrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "CFB", "OFB", "CTR", "ECB", "GCM"]=..., key_format: FORMAT=..., iv_format: FORMAT=...) -> EncryptionEncodingT: ...
Expand Down
26 changes: 26 additions & 0 deletions tests/test_encryptionencoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,29 @@ def test_rsa_sign():

def test_monoalphabetic_substitution():
assert Chepy("lol").monoalphabetic_substitution({"l": "t", "o": "s"}).o == "tst"


def test_chacha_decrypt():
assert (
Chepy("0d118d5f5807747b085473553146a3c76cf1ef61b976519240")
.from_hex()
.chacha_decrypt(
"4c6561726e696e672069732061206c6966656c6f6e6720656e646561766f722e",
"48656c6c6f20776f726c642e",
)
.o
== b"4t_lea5t_1ts_n0t_Electr0n"
)


def test_chacha_encrypt():
assert (
Chepy("4t_lea5t_1ts_n0t_Electr0n")
.chacha_encrypt(
"4c6561726e696e672069732061206c6966656c6f6e6720656e646561766f722e",
"48656c6c6f20776f726c642e",
)
.to_hex()
.o
== b"0d118d5f5807747b085473553146a3c76cf1ef61b976519240"
)

0 comments on commit 71de3c8

Please sign in to comment.