1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! # RC4 Stream Cipher Implementation
//!
//! RC4 is a stream cipher that was widely used in protocols like WEP and WPA.
//! This implementation is for educational purposes only.
//!
//! ⚠️ **Critical Security Warning**: RC4 has serious cryptographic vulnerabilities
//! and should **NEVER** be used in production systems. It's included here for
//! educational purposes only.
//!
//! ## Algorithm Overview
//!
//! RC4 consists of two main parts:
//! 1. **Key Scheduling Algorithm (KSA)**: Initializes the state array
//! 2. **Pseudo-Random Generation Algorithm (PRGA)**: Generates the keystream
//!
//! ## Examples
//!
//! ```rust
//! use ruscrypt::stream::rc4;
//!
//! // Encrypt with Base64 encoding
//! let encrypted = rc4::encrypt("Hello, World!", "secretkey", "base64").unwrap();
//! let decrypted = rc4::decrypt(&encrypted, "secretkey", "base64").unwrap();
//! assert_eq!(decrypted, "Hello, World!");
//!
//! // Encrypt with hexadecimal encoding
//! let encrypted_hex = rc4::encrypt("Hello", "key", "hex").unwrap();
//! let decrypted_hex = rc4::decrypt(&encrypted_hex, "key", "hex").unwrap();
//! assert_eq!(decrypted_hex, "Hello");
//! ```
use crate;
use Result;
/// Encrypts data using the RC4 stream cipher with specified output encoding.
///
/// RC4 encrypts by XORing the plaintext with a pseudorandom keystream generated
/// from the provided key.
///
/// # Arguments
///
/// * `data` - The plaintext string to encrypt
/// * `key` - The encryption key (any non-empty string)
/// * `encoding` - Output encoding format: "base64" or "hex"
///
/// # Returns
///
/// Returns the encrypted data encoded as specified.
///
/// # Errors
///
/// Returns an error if:
/// - The key is empty
/// - An unsupported encoding is specified
///
/// # Security Warning
///
/// ⚠️ RC4 has known vulnerabilities and should not be used for secure applications.
///
/// # Examples
///
/// ```rust
/// use ruscrypt::stream::rc4;
///
/// // Base64 output
/// let encrypted = rc4::encrypt("Secret message", "mykey", "base64").unwrap();
/// println!("Encrypted (Base64): {}", encrypted);
///
/// // Hexadecimal output
/// let encrypted_hex = rc4::encrypt("Secret message", "mykey", "hex").unwrap();
/// println!("Encrypted (Hex): {}", encrypted_hex);
/// ```
/// Decrypts data encrypted with RC4 stream cipher from specified encoding.
///
/// Reverses the RC4 encryption process by generating the same keystream and
/// XORing it with the ciphertext.
///
/// # Arguments
///
/// * `data` - The encrypted data as a string (in specified encoding)
/// * `key` - The decryption key (must match the encryption key)
/// * `encoding` - Input encoding format: "base64" or "hex"
///
/// # Returns
///
/// Returns the decrypted plaintext as a UTF-8 string.
///
/// # Errors
///
/// Returns an error if:
/// - The key is empty
/// - An unsupported encoding is specified
/// - The input data is not valid for the specified encoding
/// - The decrypted data is not valid UTF-8
///
/// # Examples
///
/// ```rust
/// use ruscrypt::stream::rc4;
///
/// // Decrypt from Base64
/// let encrypted = "YWJjZGVm"; // Example Base64 data
/// let decrypted = rc4::decrypt(encrypted, "mykey", "base64");
///
/// // Decrypt from hexadecimal
/// let encrypted_hex = "48656c6c6f"; // Example hex data
/// let decrypted_hex = rc4::decrypt(encrypted_hex, "mykey", "hex");
/// ```
/// Generates RC4 keystream using the Key Scheduling Algorithm (KSA) and
/// Pseudo-Random Generation Algorithm (PRGA).
///
/// This is an internal function that implements the core RC4 algorithm.
///
/// # Arguments
///
/// * `key` - The key bytes for initialization
/// * `length` - The desired length of the keystream
///
/// # Returns
///
/// Returns a vector of pseudorandom bytes of the specified length.
///
/// # Algorithm Details
///
/// 1. **KSA**: Initializes a 256-byte state array using the key
/// 2. **PRGA**: Generates pseudorandom bytes by swapping state elements
///
/// # Examples
///
/// ```rust
/// // This is an internal function, but here's how it works conceptually:
/// // let keystream = generate_keystream(b"key", 10);
/// // assert_eq!(keystream.len(), 10);
/// ```