Skip to main content

ToyRsa

Struct ToyRsa 

Source
pub struct ToyRsa {
    pub n: u64,
    pub e: u64,
    pub d: u64,
}
Expand description

Toy RSA key pair (n, e, d).

§WARNING

This is a toy implementation for education only. It uses tiny parameters and is completely insecure. NEVER use for real data.

Fields§

§n: u64

RSA modulus n = p * q

§e: u64

Public exponent e (typically 65537 in real RSA)

§d: u64

Private exponent d (e^{-1} mod λ(n))

Implementations§

Source§

impl ToyRsa

Source

pub fn generate(p: u64, q: u64) -> Option<Self>

Generate a toy RSA key pair from two small primes p and q.

Computes n = p*q, λ(n) = lcm(p-1, q-1), chooses e=65537 (or 3 as fallback), and derives d = e^{-1} mod λ(n).

Returns None if the primes are unsuitable (e.g., gcd(e, λ(n)) ≠ 1).

§WARNING

Educational only. Real RSA requires 2048-bit+ primes.

Source

pub fn encrypt(&self, m: u64) -> u64

Encrypt plaintext m as m^e mod n.

§WARNING

Toy implementation. Does not pad, not semantically secure.

Source

pub fn decrypt(&self, c: u64) -> u64

Decrypt ciphertext c as c^d mod n.

§WARNING

Toy implementation only.

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.