pub struct PlayfairCipher { /* private fields */ }Expand description
Playfair cipher implementation using a 5×5 keyed matrix.
The PlayfairCipher struct holds the cipher key and maintains bidirectional tables
between characters and their positions in the 5×5 Playfair matrix. This allows for
efficient encryption and decryption operations.
§Fields
key- The condensed key string used to build the cipher matrixi2c- Table from character (as u8) to itsCoupleposition in the matrixc2i- Table from matrix position to its character (as u8)
Implementations§
Source§impl PlayfairCipher
impl PlayfairCipher
Sourcepub fn new(key: &str) -> Self
pub fn new(key: &str) -> Self
Creates a new Playfair cipher with the specified key.
The key is used to construct a 5×5 matrix by:
- Condensing the key (removing duplicates and converting to uppercase)
- Appending the remaining alphabet letters (I and J are combined)
- Filling the matrix row by row with these characters
§Arguments
key- The keyword used to generate the cipher matrix
§Returns
A new PlayfairCipher instance with initialized mapping tables
§Example
use old_crypto_rs::PlayfairCipher;
let cipher = PlayfairCipher::new("PLAYFAIREXAMPLE");Trait Implementations§
Source§impl Block for PlayfairCipher
impl Block for PlayfairCipher
Source§fn block_size(&self) -> usize
fn block_size(&self) -> usize
BlockSize is part of the interface
Source§fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Encrypts plaintext using the Playfair cipher.
This method processes the input plaintext in pairs of characters (digraphs) and applies the Playfair transformation rules. If the plaintext has an odd length, an ‘X’ is automatically appended as padding.
§Arguments
dst- Destination buffer where the ciphertext will be written. Must be large enough to hold the expanded plaintext (considering double letters and padding). A safe size is at least 2x the source length.src- Source plaintext bytes to encrypt. Each byte should represent an uppercase letter from the Playfair alphabet.
§Returns
The number of bytes written to the destination buffer (always even).
§Example
use old_crypto_rs::{Block, PlayfairCipher};
let cipher = PlayfairCipher::new("PLAYFAIREXAMPLE");
let plaintext = b"HIDETHEGOLD";
let mut ciphertext = vec![0u8; 12]; // Account for potential padding
let written = cipher.encrypt(&mut ciphertext, plaintext);
assert_eq!(written, 12); // 11 chars + 1 'X' padding = 12Source§fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Decrypts ciphertext using the Playfair cipher.
This method processes the input ciphertext in pairs of characters (digraphs) and applies the inverse Playfair transformation rules to recover the original plaintext.
§Arguments
dst- Destination buffer where the plaintext will be written. Must be at least as large as the source length.src- Source ciphertext bytes to decrypt. Must have an even length, as Playfair operates on character pairs.
§Returns
The number of bytes written to the destination buffer (equal to source length).
§Panics
Panics if the source ciphertext has an odd number of bytes, as this violates the Playfair cipher’s requirement to operate on digraphs.
§Example
use old_crypto_rs::{Block, PlayfairCipher};
let cipher = PlayfairCipher::new("PLAYFAIREXAMPLE");
let ciphertext = b"BMODZBXDNABEKUDMUIXMMOUVIF";
let mut plaintext = vec![0u8; ciphertext.len()];
let written = cipher.decrypt(&mut plaintext, ciphertext);
assert_eq!(written, ciphertext.len());