Skip to main content

Module rc4

Module rc4 

Source
Expand description

RC4 stream cipher algorithm implementation.

This module provides the RC4 (Rivest Cipher 4) stream cipher implementation. RC4 is a widely-used stream cipher that uses a variable-length key (1-256 bytes) to generate a pseudorandom keystream which is XOR’d with the plaintext.

§Security Note

RC4 is considered cryptographically broken and should not be used for security-sensitive applications. It is provided here for obfuscation purposes only. For production use, consider using a modern authenticated encryption algorithm.

§Algorithm

RC4 consists of two main phases:

  1. KSA (Key Scheduling Algorithm): Initializes a 256-byte permutation table (S-box) based on the key
  2. PRGA (Pseudo-Random Generation Algorithm): Generates keystream bytes by permuting the S-box

§Types

§Example

use const_secret::{
    Encrypted, StringLiteral,
    drop_strategy::Zeroize,
    rc4::{ReEncrypt, Rc4},
};

const KEY: [u8; 5] = *b"mykey";

// Zeroize on drop (default)
const SECRET: Encrypted<Rc4<5, Zeroize<[u8; 5]>>, StringLiteral, 5> =
    Encrypted::<Rc4<5, Zeroize<[u8; 5]>>, StringLiteral, 5>::new(*b"hello", KEY);

// Re-encrypt on drop
const SECRET2: Encrypted<Rc4<5, ReEncrypt<5>>, StringLiteral, 6> =
    Encrypted::<Rc4<5, ReEncrypt<5>>, StringLiteral, 6>::new(*b"secret", KEY);

fn main() {
    let s1: &str = &*SECRET;
    assert_eq!(s1, "hello");

    let s2: &str = &*SECRET2;
    assert_eq!(s2, "secret");
}

Structs§

Rc4
An algorithm that performs RC4 encryption and decryption. This algorithm is generic over drop strategy.
ReEncrypt
Re-encrypts the buffer using RC4 on drop. This ensures the plaintext never remains in memory after the value is dropped.