Skip to main content

demo/
demo.rs

1use anyhow::Result;
2use colored::*;
3
4// Import all modules from ruscrypt
5use ruscrypt::classical::{caesar, rail_fence, vigenere, playfair};
6use ruscrypt::stream::rc4;
7use ruscrypt::block::{aes, des};
8use ruscrypt::asym::{rsa, dh};
9use ruscrypt::hash::{md5, sha1, sha256};
10
11fn main() -> Result<()> {
12    print_demo_banner();
13    
14    println!("{}", "πŸ” Classical Ciphers Demo".cyan().bold());
15    println!("{}", "═══════════════════════════".cyan());
16    demo_classical_ciphers()?;
17    
18    println!("\n{}", "🌊 Stream Ciphers Demo".green().bold());
19    println!("{}", "══════════════════════".green());
20    demo_stream_ciphers()?;
21    
22    println!("\n{}", "🧱 Block Ciphers Demo".blue().bold());
23    println!("{}", "═════════════════════".blue());
24    demo_block_ciphers()?;
25    
26    println!("\n{}", "πŸ”‘ Asymmetric Cryptography Demo".purple().bold());
27    println!("{}", "════════════════════════════════".purple());
28    demo_asymmetric_crypto()?;
29    
30    println!("\n{}", "πŸ”’ Hash Functions Demo".magenta().bold());
31    println!("{}", "══════════════════════".magenta());
32    demo_hash_functions()?;
33    
34    println!("\n{}", "✨ Complete RusCrypt Demo Finished!".bright_green().bold());
35    println!("{}", "πŸŽ“ All algorithms successfully demonstrated.".cyan());
36    Ok(())
37}
38
39fn print_demo_banner() {
40    println!("{}", r"
41  ____            ____                  _     ____                        
42 |  _ \ _   _ ___ / ___|_ __ _   _ _ __ | |_  |  _ \  ___ _ __ ___   ___    
43 | |_) | | | / __| |   | '__| | | | '_ \| __| | | | |/ _ \ '_ ` _ \ / _ \   
44 |  _ <| |_| \__ \ |___| |  | |_| | |_) | |_  | |_| |  __/ | | | | | (_) |  
45 |_| \_\\__,_|___/\____|_|   \__, | .__/ \__| |____/ \___|_| |_| |_|\___/   
46                             |___/|_|                                      
47    ".yellow());
48    
49    println!("{}", "πŸš€ RusCrypt Comprehensive Demo".yellow().bold());
50    println!("{}\n", "Showcasing ALL implemented cryptographic algorithms".bright_blue().italic());
51}
52
53fn demo_classical_ciphers() -> Result<()> {
54    let sample_text = "HELLO WORLD";
55    
56    // Caesar Cipher Demo
57    println!("{}", "πŸ“œ Caesar Cipher:".yellow());
58    let shift = 3;
59    let caesar_encrypted = caesar::encrypt(sample_text, shift)?;
60    let caesar_decrypted = caesar::decrypt(&caesar_encrypted, shift)?;
61    println!("   Original: {}", sample_text.white());
62    println!("   Shift: {}", shift.to_string().cyan());
63    println!("   Encrypted: {}", caesar_encrypted.green());
64    println!("   Decrypted: {}", caesar_decrypted.blue());
65    println!("   ⚠️  Security: Educational only - easily broken");
66    
67    // Vigenère Cipher Demo
68    println!("\n{}", "πŸ”€ VigenΓ¨re Cipher:".yellow());
69    let keyword = "KEY";
70    let vigenere_encrypted = vigenere::encrypt(sample_text, keyword)?;
71    let vigenere_decrypted = vigenere::decrypt(&vigenere_encrypted, keyword)?;
72    println!("   Original: {}", sample_text.white());
73    println!("   Keyword: {}", keyword.cyan());
74    println!("   Encrypted: {}", vigenere_encrypted.green());
75    println!("   Decrypted: {}", vigenere_decrypted.blue());
76    println!("   ⚠️  Security: Educational only - vulnerable to frequency analysis");
77    
78    // Rail Fence Cipher Demo
79    println!("\n{}", "πŸš‚ Rail Fence Cipher:".yellow());
80    let rails = 3;
81    let railfence_encrypted = rail_fence::encrypt(sample_text, rails)?;
82    let railfence_decrypted = rail_fence::decrypt(&railfence_encrypted, rails)?;
83    println!("   Original: {}", sample_text.white());
84    println!("   Rails: {}", rails.to_string().cyan());
85    println!("   Encrypted: {}", railfence_encrypted.green());
86    println!("   Decrypted: {}", railfence_decrypted.blue());
87    println!("   ⚠️  Security: Educational only - simple transposition");
88    
89    // Playfair Cipher Demo
90    println!("\n{}", "🎯 Playfair Cipher:".yellow());
91    let playfair_key = "MONARCHY";
92    let playfair_text = "HELLO";
93    let playfair_encrypted = playfair::encrypt(playfair_text, playfair_key)?;
94    let playfair_decrypted = playfair::decrypt(&playfair_encrypted, playfair_key)?;
95    println!("   Original: {}", playfair_text.white());
96    println!("   Key: {}", playfair_key.cyan());
97    println!("   Encrypted: {}", playfair_encrypted.green());
98    println!("   Decrypted: {}", playfair_decrypted.blue());
99    println!("   ⚠️  Security: Educational only - digraph substitution");
100    
101    Ok(())
102}
103
104fn demo_stream_ciphers() -> Result<()> {
105    let sample_text = "Hello, World! This is a secret message.";
106    
107    // RC4 Cipher Demo with Base64
108    println!("{}", "πŸ” RC4 Stream Cipher (Base64):".yellow());
109    let key = "secretkey123";
110    let rc4_encrypted_b64 = rc4::encrypt(sample_text, key, "base64")?;
111    let rc4_decrypted_b64 = rc4::decrypt(&rc4_encrypted_b64, key, "base64")?;
112    println!("   Original: {}", sample_text.white());
113    println!("   Key: {}", key.cyan());
114    println!("   Encrypted (Base64): {}", rc4_encrypted_b64.green());
115    println!("   Decrypted: {}", rc4_decrypted_b64.blue());
116    
117    // RC4 Cipher Demo with Hex
118    println!("\n{}", "πŸ” RC4 Stream Cipher (Hex):".yellow());
119    let rc4_encrypted_hex = rc4::encrypt(sample_text, key, "hex")?;
120    let rc4_decrypted_hex = rc4::decrypt(&rc4_encrypted_hex, key, "hex")?;
121    println!("   Original: {}", sample_text.white());
122    println!("   Key: {}", key.cyan());
123    println!("   Encrypted (Hex): {}", rc4_encrypted_hex.green());
124    println!("   Decrypted: {}", rc4_decrypted_hex.blue());
125    println!("   ⚠️  Security: DEPRECATED - Known vulnerabilities, educational use only");
126    
127    Ok(())
128}
129
130fn demo_block_ciphers() -> Result<()> {
131    let sample_text = "This is a block cipher test message!";
132    
133    // AES Cipher Demo
134    println!("{}", "πŸ›‘οΈ  AES (Advanced Encryption Standard):".yellow());
135    let password = "strongpassword";
136    
137    // AES-128 ECB
138    println!("\n   πŸ“Œ AES-128 ECB Mode:");
139    let aes128_ecb = aes::encrypt(sample_text, password, "128", "ECB", "base64")?;
140    let aes128_decrypted = aes::decrypt(&aes128_ecb, password, "128", "ECB", "base64")?;
141    println!("      Original: {}", sample_text.white());
142    println!("      Password: {}", password.cyan());
143    println!("      Encrypted: {}", aes128_ecb.green());
144    println!("      Decrypted: {}", aes128_decrypted.blue());
145    
146    // AES-256 CBC
147    println!("\n   πŸ“Œ AES-256 CBC Mode:");
148    let aes256_cbc = aes::encrypt(sample_text, password, "256", "CBC", "hex")?;
149    let aes256_decrypted = aes::decrypt(&aes256_cbc, password, "256", "CBC", "hex")?;
150    println!("      Original: {}", sample_text.white());
151    println!("      Password: {}", password.cyan());
152    println!("      Encrypted: {}", aes256_cbc.green());
153    println!("      Decrypted: {}", aes256_decrypted.blue());
154    println!("      βœ… Security: Modern standard - recommended for production");
155    
156    // DES Cipher Demo
157    println!("\n{}", "πŸ”’ DES (Data Encryption Standard):".yellow());
158    let des_key = "8charkey"; // Exactly 8 characters
159    
160    // DES ECB
161    println!("\n   πŸ“Œ DES ECB Mode:");
162    let des_ecb = des::encrypt(sample_text, des_key, "ECB", "base64")?;
163    let des_ecb_decrypted = des::decrypt(&des_ecb, des_key, "ECB", "base64")?;
164    println!("      Original: {}", sample_text.white());
165    println!("      Key: {}", des_key.cyan());
166    println!("      Encrypted: {}", des_ecb.green());
167    println!("      Decrypted: {}", des_ecb_decrypted.blue());
168    
169    // DES CBC
170    println!("\n   πŸ“Œ DES CBC Mode:");
171    let des_cbc = des::encrypt(sample_text, des_key, "CBC", "hex")?;
172    let des_cbc_decrypted = des::decrypt(&des_cbc, des_key, "CBC", "hex")?;
173    println!("      Original: {}", sample_text.white());
174    println!("      Key: {}", des_key.cyan());
175    println!("      Encrypted: {}", des_cbc.green());
176    println!("      Decrypted: {}", des_cbc_decrypted.blue());
177    println!("      ⚠️  Security: DEPRECATED - 56-bit key too small, educational use only");
178    
179    Ok(())
180}
181
182fn demo_asymmetric_crypto() -> Result<()> {
183    let sample_text = "Asymmetric encryption test!";
184    
185    // RSA Demo
186    println!("{}", "πŸ” RSA (Rivest-Shamir-Adleman):".yellow());
187    
188    // RSA with different key sizes
189    for key_size in ["512", "1024"] {
190        println!("\n   πŸ“Œ RSA-{} Encryption:", key_size);
191        let (encrypted, private_key) = rsa::encrypt(sample_text, key_size, "base64", "n:e")?;
192        let decrypted = rsa::decrypt(&encrypted, &private_key, "base64")?;
193        println!("      Original: {}", sample_text.white());
194        println!("      Key Size: {} bits", key_size.cyan());
195        println!("      Encrypted: {}...", encrypted[..50].green());
196        println!("      Private Key: {}...", private_key[..20].yellow());
197        println!("      Decrypted: {}", decrypted.blue());
198    }
199    println!("      βœ… Security: Secure with β‰₯2048 bits (demo uses smaller for speed)");
200    
201    // Diffie-Hellman Demo
202    println!("\n{}", "🀝 Diffie-Hellman Key Exchange:".yellow());
203    println!("   πŸ“Œ Educational Concept Demonstration:");
204    
205    // Create two participants manually for demo
206    let alice = dh::DHParticipant::new();
207    let mut bob = dh::DHParticipant::new();
208    
209    println!("      πŸ‘© Alice generates keys:");
210    println!("         Private: {} (secret)", alice.private_key.to_string().red());
211    println!("         Public:  {} (shared)", alice.public_key.to_string().green());
212    
213    println!("      πŸ‘¨ Bob generates keys:");
214    println!("         Private: {} (secret)", bob.private_key.to_string().red());
215    println!("         Public:  {} (shared)", bob.public_key.to_string().green());
216    
217    // Compute shared secret
218    let shared_secret = bob.compute_shared_secret(alice.public_key)?;
219    println!("      🀝 Computed shared secret: {}", shared_secret.to_string().cyan());
220    println!("      βœ… Security: Secure with proper parameters and authentication");
221    
222    Ok(())
223}
224
225fn demo_hash_functions() -> Result<()> {
226    let sample_texts = vec![
227        "Hello, World!",
228        "RusCrypt is awesome!",
229        "Secure hashing with Rust",
230        "Small change", 
231        "small change", // Demonstrate avalanche effect
232    ];
233    
234    println!("{}", "Hash Function Comparison:".yellow());
235    println!("{}", "═════════════════════════".yellow());
236    
237    for (i, text) in sample_texts.iter().enumerate() {
238        println!("\n{} {}:", "Sample".cyan(), (i + 1).to_string().cyan());
239        println!("   Input: {}", text.white());
240        
241        // MD5 Hash
242        let md5_hash = md5::hash(text)?;
243        println!("   MD5    (128-bit): {}", md5_hash.bright_red());
244        
245        // SHA-1 Hash
246        let sha1_hash = sha1::hash(text)?;
247        println!("   SHA-1  (160-bit): {}", sha1_hash.bright_yellow());
248        
249        // SHA-256 Hash
250        let sha256_hash = sha256::hash(text)?;
251        println!("   SHA-256(256-bit): {}", sha256_hash.bright_green());
252        
253        if i == 3 { // Show avalanche effect
254            println!("   πŸ’‘ Notice how 'Small change' vs 'small change' produces completely different hashes!");
255        }
256    }
257    
258    // Security status
259    println!("\n{}", "Security Status:".yellow());
260    println!("   ❌ MD5:    BROKEN - Collision attacks possible");
261    println!("   ⚠️  SHA-1:  DEPRECATED - Use only for legacy compatibility");
262    println!("   βœ… SHA-256: SECURE - Recommended for modern applications");
263    
264    // Demonstrate hash consistency
265    println!("\n{}", "πŸ” Hash Consistency Verification:".yellow());
266    let test_input = "consistency_test";
267    let hash1 = sha256::hash(test_input)?;
268    let hash2 = sha256::hash(test_input)?;
269    println!("   Input: {}", test_input.white());
270    println!("   Hash 1: {}", hash1.green());
271    println!("   Hash 2: {}", hash2.green());
272    println!("   Match: {}", if hash1 == hash2 { "βœ… Perfect consistency".bright_green() } else { "❌ Error!".bright_red() });
273    
274    Ok(())
275}