pub struct AutokeyCipher { /* private fields */ }Implementations§
Trait Implementations§
Source§impl Block for AutokeyCipher
impl Block for AutokeyCipher
Source§fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
fn encrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Encrypts plaintext using the Autokey cipher.
The autokey cipher uses the key for the first characters, then appends the plaintext itself to form an extended key. This creates a keystream that is as long as the message without repeating the original key.
§Algorithm
For each character at position i:
- If i < key.len(): use key[i]
- Otherwise: use plaintext[i - key.len()]
Then apply standard Vigenère encryption: C[i] = (P[i] + K[i]) mod 26
§Arguments
dst- Destination buffer where encrypted bytes will be writtensrc- Source plaintext bytes (expected to be uppercase A-Z)
§Returns
The length of the destination buffer
§Examples
let cipher = AutokeyCipher::new("KEY");
let pt = b"HELLO";
let mut ct = vec![0u8; pt.len()];
cipher.encrypt(&mut ct, pt);
assert_eq!(&ct, b"RIJSS");Source§fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
fn decrypt(&self, dst: &mut [u8], src: &[u8]) -> usize
Decrypts ciphertext using the Autokey cipher.
Reverses the autokey encryption process by using the key for the first characters, then using the previously decrypted plaintext to continue the keystream.
§Algorithm
For each character at position i:
- If i < key.len(): use key[i]
- Otherwise: use decrypted_plaintext[i - key.len()]
- Decrypt: P[i] = (C[i] - K[i]) mod 26 (or equivalently: (C[i] + (26 - K[i])) mod 26)
Each character must be decrypted in order since the plaintext is needed for the next key.
§Arguments
dst- Destination buffer where decrypted bytes will be writtensrc- Source ciphertext bytes (expected to be uppercase A-Z)
§Returns
The number of bytes written to the destination buffer
§Examples
let cipher = AutokeyCipher::new("KEY");
let ct = b"RIJSS";
let mut pt = vec![0u8; ct.len()];
cipher.decrypt(&mut pt, ct);
assert_eq!(&pt, b"HELLO");