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
//! # Caesar Cipher Implementation
//!
//! The Caesar cipher is one of the simplest and most well-known encryption techniques.
//! It is a substitution cipher where each letter is shifted by a fixed number of positions
//! in the alphabet.
//!
//! ⚠️ **Security Warning**: The Caesar cipher is **not secure** for modern use and should
//! only be used for educational purposes or simple text obfuscation.
//!
//! ## Examples
//!
//! ```rust
//! use ruscrypt::classical::caesar;
//!
//! // Encrypt a message
//! let encrypted = caesar::encrypt("HELLO", 3).unwrap();
//! assert_eq!(encrypted, "KHOOR");
//!
//! // Decrypt the message
//! let decrypted = caesar::decrypt(&encrypted, 3).unwrap();
//! assert_eq!(decrypted, "HELLO");
//! ```
use Result;
use crateshift_char;
/// Encrypts text using the Caesar cipher algorithm.
///
/// Each alphabetic character is shifted forward by the specified number of positions
/// in the alphabet. Non-alphabetic characters remain unchanged.
///
/// # Arguments
///
/// * `text` - The plaintext to encrypt
/// * `shift` - Number of positions to shift (0-25, values > 25 are automatically reduced)
///
/// # Returns
///
/// Returns the encrypted text as a `Result<String>`.
///
/// # Examples
///
/// ```rust
/// use ruscrypt::classical::caesar;
///
/// let result = caesar::encrypt("Hello, World!", 3).unwrap();
/// assert_eq!(result, "Khoor, Zruog!");
///
/// // Large shift values are automatically reduced modulo 26
/// let result2 = caesar::encrypt("ABC", 29).unwrap(); // 29 % 26 = 3
/// assert_eq!(result2, "DEF");
/// ```
/// Decrypts text encrypted with the Caesar cipher.
///
/// Each alphabetic character is shifted backward by the specified number of positions
/// in the alphabet. Non-alphabetic characters remain unchanged.
///
/// # Arguments
///
/// * `text` - The ciphertext to decrypt
/// * `shift` - Number of positions the original text was shifted (0-25)
///
/// # Returns
///
/// Returns the decrypted text as a `Result<String>`.
///
/// # Examples
///
/// ```rust
/// use ruscrypt::classical::caesar;
///
/// let encrypted = "Khoor, Zruog!";
/// let result = caesar::decrypt(encrypted, 3).unwrap();
/// assert_eq!(result, "Hello, World!");
/// ```