PlayfairCipher

Struct PlayfairCipher 

Source
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 matrix
  • i2c - Table from character (as u8) to its Couple position in the matrix
  • c2i - Table from matrix position to its character (as u8)

Implementations§

Source§

impl PlayfairCipher

Source

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:

  1. Condensing the key (removing duplicates and converting to uppercase)
  2. Appending the remaining alphabet letters (I and J are combined)
  3. 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

Source§

fn block_size(&self) -> usize

BlockSize is part of the interface

Source§

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 = 12
Source§

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());

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.