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
//! # Vigenère Cipher Implementation
//!
//! The Vigenère cipher is a polyalphabetic substitution cipher that uses a keyword
//! to encrypt text. It's more secure than the Caesar cipher but still not suitable
//! for modern cryptographic applications.
//!
//! ⚠️ **Security Warning**: The Vigenère cipher has known vulnerabilities and should
//! only be used for educational purposes.
//!
//! ## How it works
//!
//! 1. Repeat the keyword to match the length of the message
//! 2. For each letter, shift it by the corresponding keyword letter's position
//! 3. Non-alphabetic characters are preserved
//!
//! ## Examples
//!
//! ```rust
//! use ruscrypt::classical::vigenere;
//!
//! let encrypted = vigenere::encrypt("HELLO WORLD", "KEY").unwrap();
//! let decrypted = vigenere::decrypt(&encrypted, "KEY").unwrap();
//! assert_eq!(decrypted, "HELLO WORLD");
//! ```
use Result;
use crateshift_char;
/// Encrypts text using the Vigenère cipher algorithm.
///
/// Uses a keyword to determine the shift amount for each letter. The keyword
/// is repeated cyclically to match the length of the message.
///
/// # Arguments
///
/// * `text` - The plaintext to encrypt
/// * `keyword` - The keyword used for encryption (must contain at least one alphabetic character)
///
/// # Returns
///
/// Returns the encrypted text as a `Result<String>`.
///
/// # Errors
///
/// Returns an error if:
/// - The keyword is empty
/// - The keyword contains no alphabetic characters
///
/// # Examples
///
/// ```rust
/// use ruscrypt::classical::vigenere;
///
/// // Basic encryption
/// let result = vigenere::encrypt("HELLO", "KEY").unwrap();
/// assert_eq!(result, "RIJVS");
///
/// // Mixed case and punctuation
/// let result2 = vigenere::encrypt("Hello, World!", "SECRET").unwrap();
/// // Non-alphabetic characters remain unchanged
/// ```
/// Decrypts text encrypted with the Vigenère cipher.
///
/// Uses the same keyword that was used for encryption to reverse the process.
///
/// # Arguments
///
/// * `text` - The ciphertext to decrypt
/// * `keyword` - The keyword used for encryption (must contain at least one alphabetic character)
///
/// # Returns
///
/// Returns the decrypted text as a `Result<String>`.
///
/// # Errors
///
/// Returns an error if:
/// - The keyword is empty
/// - The keyword contains no alphabetic characters
///
/// # Examples
///
/// ```rust
/// use ruscrypt::classical::vigenere;
///
/// let encrypted = "RIJVS";
/// let result = vigenere::decrypt(encrypted, "KEY").unwrap();
/// assert_eq!(result, "HELLO");
/// ```