Skip to main content

cryptography_breaker/
lib.rs

1//! Cryptography breaker
2//!
3//! This crate provide way to encrypt, decrypt and break, for the moment only Vigener cipher
4//!
5//! You can do it in almost every possible case.
6//! The crate support selecting own alphabet, alphabet with characters upper and lower cases, with whitespaces.
7//! You can decide what to do with whitespaces or character not provided in the alphabet
8//!
9//! # Examples
10//!
11//! ## Encrypting text using vigenere cipher
12//!
13//! ```
14//! use cryptography_breaker::normalizer::NormalizerBuilder;
15//! use cryptography_breaker::algorithms::vigenere::Vigenere;
16//! use cryptography_breaker::normalizer::CaseStrategy;
17//! use cryptography_breaker::algorithms::vigenere::VigenereBuilder;
18//! 
19//! let plain_text = "Some Plain Text";
20//! let key = "KEY";
21//! let normalizer = NormalizerBuilder::default()
22//!     .case(CaseStrategy::Preserve)
23//!     .preserve_whitespaces(true)
24//!     .build()
25//!     .unwrap();
26//! let vigenere = VigenereBuilder::default()
27//!     .normalizer(normalizer)
28//!     .build()
29//!     .unwrap();
30//! 
31//! let cipher_text = vigenere.encrypt(plain_text, key).unwrap();
32//! 
33//! assert_eq!("Csko Tjkml Divd", cipher_text)
34//! ```
35
36pub mod algorithms;
37pub mod cli;
38pub mod normalizer;
39pub mod validator;
40
41/// Index of coincidence
42///
43/// Choose which language to use
44#[derive(Default)]
45pub enum IoC {
46    /// English
47    #[default]
48    En,
49    /// Polish
50    Pl,
51    /// French
52    Fr,
53    /// German
54    De,
55    /// Italian
56    It,
57    /// Portuguese
58    Pt,
59    /// Rusian
60    Ru,
61    /// Spanish
62    Es,
63}
64
65impl IoC {
66    /// Return value of given IoC
67    /// 
68    /// This should be used for now for this crate.
69    ///
70    /// The values were divided by 26 (number of english letters). \
71    /// `Pl` values were calculated by me, the rest are from [wikipedia](https://en.wikipedia.org/wiki/Index_of_coincidence#cite_note-6:~:text=Expected%20values%20for%20various%20languages%5B6%5D%20are%3A)
72    ///
73    /// For normalized results, use `value_normalized()`
74    ///
75    /// # Example
76    /// ```
77    /// use cryptography_breaker::IoC;
78    ///
79    /// let english_ioc_value = IoC::En.value();
80    ///
81    /// assert_eq!(0.0665, english_ioc_value)
82    /// ```
83    pub fn value(&self) -> f64 {
84        match self {
85            IoC::En => 0.0665,
86            IoC::Pl => 0.0591,
87            IoC::Fr => 0.0777,
88            IoC::De => 0.0788,
89            IoC::It => 0.0746,
90            IoC::Pt => 0.0746,
91            IoC::Ru => 0.0677,
92            IoC::Es => 0.0746,
93        }
94    }
95
96    /// Return normalized value of given IoC
97    ///
98    /// The calculated values are normalized by multiply the results by number of letters
99    /// `Pl` values were calculated by me, the rest are from [wikipedia](https://en.wikipedia.org/wiki/Index_of_coincidence#cite_note-6:~:text=Expected%20values%20for%20various%20languages%5B6%5D%20are%3A)
100    ///
101    /// For normalized results, use `value_normalized()`
102    ///
103    /// # Example
104    /// ```
105    /// use cryptography_breaker::IoC;
106    ///
107    /// let english_ioc_value_normalized = IoC::En.value_normalized();
108    ///
109    /// assert_eq!(1.729, english_ioc_value_normalized)
110    /// ```
111    pub fn value_normalized(&self) -> f64 {
112        match self {
113            IoC::En => 1.729,
114            IoC::Pl => 1.537,
115            IoC::Fr => 2.020,
116            IoC::De => 2.049,
117            IoC::It => 1.940,
118            IoC::Pt => 1.940,
119            IoC::Ru => 1.760,
120            IoC::Es => 1.940,
121        }
122    }
123}