Skip to content
Closed
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
88 changes: 88 additions & 0 deletions ciphers/rc4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from typing import List

Check failure on line 1 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

ciphers/rc4.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 1 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

ciphers/rc4.py:1:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 1 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

ciphers/rc4.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 1 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

ciphers/rc4.py:1:1: UP035 `typing.List` is deprecated, use `list` instead

class RC4:
def __init__(self, key: bytes):
self.key = key
self.s = self._ksa(key)

def _ksa(self, key: bytes) -> List[int]:

Check failure on line 8 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

ciphers/rc4.py:8:35: UP006 Use `list` instead of `List` for type annotation

Check failure on line 8 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

ciphers/rc4.py:8:35: UP006 Use `list` instead of `List` for type annotation
"""
Key Scheduling Algorithm (KSA)

Check failure on line 11 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:11:1: W293 Blank line contains whitespace

Check failure on line 11 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:11:1: W293 Blank line contains whitespace
>>> rc4 = RC4(b"SecretKey")
>>> len(rc4._ksa(b"SecretKey"))
256
"""
key_length = len(key)
s = list(range(256))
j = 0
for i in range(256):
j = (j + s[i] + key[i % key_length]) % 256
s[i], s[j] = s[j], s[i]
return s

def _prga(self) -> int:
"""
Pseudo-Random Generation Algorithm (PRGA)

Check failure on line 27 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:27:1: W293 Blank line contains whitespace

Check failure on line 27 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:27:1: W293 Blank line contains whitespace
>>> rc4 = RC4(b"SecretKey")
>>> prga = rc4._prga()
>>> isinstance(next(prga), int)
True
"""
i = 0
j = 0
while True:
i = (i + 1) % 256
j = (j + self.s[i]) % 256
self.s[i], self.s[j] = self.s[j], self.s[i]
yield self.s[(self.s[i] + self.s[j]) % 256]

def _reset_state(self):
"""Reset state for each encryption/decryption."""
self.s = self._ksa(self.key)

def encrypt(self, plaintext: bytes) -> bytes:
"""
Encrypt plaintext using RC4

Check failure on line 48 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:48:1: W293 Blank line contains whitespace

Check failure on line 48 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:48:1: W293 Blank line contains whitespace
>>> rc4 = RC4(b"SecretKey")
>>> plaintext = b"Hello"
>>> encrypted = rc4.encrypt(plaintext)
>>> len(encrypted) == len(plaintext)
True
"""
self._reset_state()
prga = self._prga()
return bytes([p ^ next(prga) for p in plaintext])

def decrypt(self, ciphertext: bytes) -> bytes:
"""
Decrypt ciphertext using RC4

Check failure on line 62 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:62:1: W293 Blank line contains whitespace

Check failure on line 62 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:62:1: W293 Blank line contains whitespace
>>> rc4 = RC4(b"SecretKey")
>>> ciphertext = rc4.encrypt(b"Hello")
>>> rc4.decrypt(ciphertext) == b"Hello"
True
"""
return self.encrypt(ciphertext)


if __name__ == "__main__":
import doctest
doctest.testmod()

Check failure on line 73 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

ciphers/rc4.py:73:22: W291 Trailing whitespace

Check failure on line 73 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

ciphers/rc4.py:73:22: W291 Trailing whitespace

key = b"SecretKey"
rc4 = RC4(key)

Check failure on line 77 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:77:1: W293 Blank line contains whitespace

Check failure on line 77 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:77:1: W293 Blank line contains whitespace
plaintext = b"Hello, RC4 Cipher!"
print(f"Original: {plaintext}")

Check failure on line 80 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:80:1: W293 Blank line contains whitespace

Check failure on line 80 in ciphers/rc4.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

ciphers/rc4.py:80:1: W293 Blank line contains whitespace
ciphertext = rc4.encrypt(plaintext)
print(f"Encrypted: {ciphertext}")

decrypted_text = rc4.decrypt(ciphertext)
print(f"Decrypted: {decrypted_text}")

assert plaintext == decrypted_text, "Decryption failed!"
print("Encryption and decryption successful.")