5
5
- https://en.wikipedia.org/wiki/RC4
6
6
"""
7
7
8
+
8
9
def ksa (key : bytes ) -> list [int ]:
9
10
"""
10
11
Key Scheduling Algorithm (KSA) for RC4.
@@ -77,10 +78,12 @@ def rc4(key: bytes, data: bytes) -> bytes:
77
78
"""
78
79
s_box = ksa (key )
79
80
keystream = prga (s_box , len (data ))
80
- output = bytes ([
81
- data_byte ^ keystream_byte
82
- for data_byte , keystream_byte in zip (data , keystream )
83
- ])
81
+ output = bytes (
82
+ [
83
+ data_byte ^ keystream_byte
84
+ for data_byte , keystream_byte in zip (data , keystream )
85
+ ]
86
+ )
84
87
return output
85
88
86
89
@@ -93,17 +96,13 @@ def rc4(key: bytes, data: bytes) -> bytes:
93
96
parser .add_argument (
94
97
"mode" , choices = ["encrypt" , "decrypt" ], help = "Mode: encrypt or decrypt"
95
98
)
96
- parser .add_argument (
97
- "key" , type = str , help = "Encryption/Decryption key"
98
- )
99
- parser .add_argument (
100
- "input" , type = str , help = "Input text"
101
- )
99
+ parser .add_argument ("key" , type = str , help = "Encryption/Decryption key" )
100
+ parser .add_argument ("input" , type = str , help = "Input text" )
102
101
103
102
args = parser .parse_args ()
104
103
105
- key_bytes = args .key .encode (' ascii' )
106
- input_bytes = args .input .encode (' ascii' )
104
+ key_bytes = args .key .encode (" ascii" )
105
+ input_bytes = args .input .encode (" ascii" )
107
106
108
107
result_bytes = rc4 (key_bytes , input_bytes )
109
108
@@ -114,6 +113,6 @@ def rc4(key: bytes, data: bytes) -> bytes:
114
113
# if user passed hex data, decode it
115
114
input_bytes = bytes .fromhex (args .input )
116
115
result_bytes = rc4 (key_bytes , input_bytes )
117
- print (result_bytes .decode (' ascii' ))
116
+ print (result_bytes .decode (" ascii" ))
118
117
except ValueError :
119
118
print ("Error: Input must be valid hex string when decrypting." )
0 commit comments