11from typing import List
22
3+
34class RC4 :
45 def __init__ (self , key : bytes ):
56 self .key = key
@@ -8,7 +9,7 @@ def __init__(self, key: bytes):
89 def _ksa (self , key : bytes ) -> List [int ]:
910 """
1011 Key Scheduling Algorithm (KSA)
11-
12+
1213 >>> rc4 = RC4(b"SecretKey")
1314 >>> len(rc4._ksa(b"SecretKey"))
1415 256
@@ -24,7 +25,7 @@ def _ksa(self, key: bytes) -> List[int]:
2425 def _prga (self ) -> int :
2526 """
2627 Pseudo-Random Generation Algorithm (PRGA)
27-
28+
2829 >>> rc4 = RC4(b"SecretKey")
2930 >>> prga = rc4._prga()
3031 >>> isinstance(next(prga), int)
@@ -45,7 +46,7 @@ def _reset_state(self):
4546 def encrypt (self , plaintext : bytes ) -> bytes :
4647 """
4748 Encrypt plaintext using RC4
48-
49+
4950 >>> rc4 = RC4(b"SecretKey")
5051 >>> plaintext = b"Hello"
5152 >>> encrypted = rc4.encrypt(plaintext)
@@ -59,7 +60,7 @@ def encrypt(self, plaintext: bytes) -> bytes:
5960 def decrypt (self , ciphertext : bytes ) -> bytes :
6061 """
6162 Decrypt ciphertext using RC4
62-
63+
6364 >>> rc4 = RC4(b"SecretKey")
6465 >>> ciphertext = rc4.encrypt(b"Hello")
6566 >>> rc4.decrypt(ciphertext) == b"Hello"
@@ -70,19 +71,20 @@ def decrypt(self, ciphertext: bytes) -> bytes:
7071
7172if __name__ == "__main__" :
7273 import doctest
73- doctest .testmod ()
74+
75+ doctest .testmod ()
7476
7577 key = b"SecretKey"
7678 rc4 = RC4 (key )
77-
79+
7880 plaintext = b"Hello, RC4 Cipher!"
7981 print (f"Original: { plaintext } " )
80-
82+
8183 ciphertext = rc4 .encrypt (plaintext )
8284 print (f"Encrypted: { ciphertext } " )
83-
85+
8486 decrypted_text = rc4 .decrypt (ciphertext )
8587 print (f"Decrypted: { decrypted_text } " )
86-
88+
8789 assert plaintext == decrypted_text , "Decryption failed!"
8890 print ("Encryption and decryption successful." )
0 commit comments