-
-
Save rust-play/423441b6c9dec557b2f16abe3040d0d4 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![allow(deprecated)] | |
| #![allow(unused)] | |
| use rand_0_8_5::{thread_rng as rng_v8, Rng as RngV8}; | |
| use rand_0_9_2::{thread_rng as rng_v9, Rng as RngV9}; | |
| use sha2::{Sha256, Digest}; | |
| use chrono::Local; | |
| // ============================================================ | |
| // [UTILITIES] - THE PERSISTENT TOOLKIT | |
| // ============================================================ | |
| fn classical_brute_force(n: i64) -> (i64, i64) { | |
| let limit = (n as f64).sqrt() as i64; | |
| for i in 2..=limit { | |
| if n % i == 0 { | |
| println!("[SOLVE] n={} | i={} | n%i={} (Clean Division Found!)", n, i, n % i); | |
| return (i, n / i); | |
| } | |
| } | |
| (1, n) | |
| } | |
| fn mod_inverse(e: i64, phi: i64) -> i64 { | |
| let (mut a, mut b) = (e, phi); | |
| let (mut x0, mut x1) = (0, 1); | |
| if b == 1 { return 1; } | |
| while a > 1 { | |
| let q = a / b; | |
| let mut t = b; | |
| b = a % b; a = t; | |
| t = x0; x0 = x1 - q * x0; x1 = t; | |
| } | |
| if x1 < 0 { x1 += phi; } | |
| x1 | |
| } | |
| fn mod_pow(mut base: u64, mut exp: u64, m: u64) -> u64 { | |
| let mut res = 1; | |
| base %= m; | |
| while exp > 0 { | |
| if exp % 2 == 1 { res = (res * base) % m; } | |
| base = (base * base) % m; | |
| exp /= 2; | |
| } | |
| res | |
| } | |
| // ============================================================ | |
| // [AUDIT MODULES] - 4-BIT MESSAGE COMPARISON | |
| // ============================================================ | |
| fn simulate_insecure_rsa(seed: u16, msg: u64) { | |
| let p_pool = [59, 61, 67, 71, 73, 79, 83, 89, 97, 101]; | |
| let p_actual = p_pool[(seed as usize % 5)] as i64; | |
| let q_actual = p_pool[((seed as usize / 5) % 5) + 5] as i64; | |
| let n = p_actual * q_actual; | |
| let e: i64 = 65537; | |
| println!("--- [INSECURE RSA: 4-BIT MESSAGE AUDIT] ---"); | |
| let (p_found, q_found) = classical_brute_force(n); | |
| let phi_found = (p_found - 1) * (q_found - 1); | |
| let d_found = mod_inverse(e, phi_found); | |
| let encrypted = mod_pow(msg, e as u64, n as u64); | |
| let decrypted = mod_pow(encrypted, d_found as u64, n as u64); | |
| println!("RSA Modulus (n): {}", n); | |
| println!("Step 1 (Brute Force): Factors {} and {} resolved.", p_found, q_found); | |
| println!("Step 2 (Validation): Msg({}) -> Cipher({}) -> Decrypted({})", msg, encrypted, decrypted); | |
| println!("Quantum Analysis: The mapping from Msg to Cipher is a perfect mathematical cycle."); | |
| } | |
| fn lattice_audit_4bit(seed: u16, msg: u8) { | |
| let q: i32 = 4096; | |
| let mut v8 = rng_v8(); | |
| let s: i32 = (seed % 255) as i32; | |
| let a: i32 = v8.gen_range(1..q); | |
| let e: i32 = v8.gen_range(2..6); | |
| let b = (a * s + e) % q; | |
| println!("\n--- [SECURE LATTICE: 4-BIT MESSAGE (BIT-BY-BIT)] ---"); | |
| println!("Lattice Public Key: (a={}, b={})", a, b); | |
| let bit_scaling = q / 2; | |
| let mut decrypted_final = 0u8; | |
| // We iterate through each of the 4 bits of the message | |
| for i in 0..4 { | |
| let bit = (msg >> i) & 1; | |
| // Encrypt this bit | |
| let r: i32 = v8.gen_range(1..10); | |
| let e1: i32 = v8.gen_range(1..4); | |
| let e2: i32 = v8.gen_range(1..4); | |
| let c1 = (a * r + e1) % q; | |
| let c2 = (b * r + e2 + (bit as i32) * bit_scaling) % q; | |
| // Decrypt this bit | |
| let mut raw_dec = (c2 - (c1 * s)) % q; | |
| if raw_dec < 0 { raw_dec += q; } | |
| let decrypted_bit = if (raw_dec - bit_scaling).abs() < q / 4 { 1 } else { 0 }; | |
| println!("Bit {}: Original={}, Raw_Recovered={}, Decrypted={}", i, bit, raw_dec, decrypted_bit); | |
| decrypted_final |= (decrypted_bit << i); | |
| } | |
| println!("\n[FINAL DECRYPTION RESULT]"); | |
| println!("Original 4-bit Message: {}", msg); | |
| println!("Decrypted 4-bit Message: {}", decrypted_final); | |
| println!("Quantum Analysis: Each bit has different random noise, breaking any global period."); | |
| } | |
| fn main() { | |
| let now = Local::now(); | |
| let v8_seed: u16 = 1024; | |
| // 4-bit message: 13 (Binary: 1101) | |
| let msg_4bit: u8 = 13; | |
| println!("4-Bit Cryptographic Multi-Audit | {}", now.format("%Y-%m-%d %H:%M:%S")); | |
| println!("========================================================================"); | |
| simulate_insecure_rsa(v8_seed, msg_4bit as u64); | |
| lattice_audit_4bit(v8_seed, msg_4bit); | |
| println!("\n[AUDIT SUMMARY]"); | |
| println!("RSA Efficiency: One operation for 4 bits. (Mathematically rigid)"); | |
| println!("Lattice Efficiency: Four operations for 4 bits. (Mathematically noisy but secure)"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment