Cryptor

Struct Cryptor 

Source
pub struct Cryptor { /* private fields */ }
Expand description

A cryptographic utility for encrypting and decrypting text using a matrix-based transformation.

The Cryptor uses a combination of shuffling, mixing, and matrix operations to obscure the original text. It supports configurable matrix sizes for the transformation process.

§Examples

use crypt_ro::Cryptor;

let cryptor = Cryptor::new();
let encrypted = cryptor.encrypt_text("secret message", "password").unwrap();
let decrypted = cryptor.decrypt_text(&encrypted, "password").unwrap();
assert_eq!(decrypted, "secret message");

Implementations§

Source§

impl Cryptor

Source

pub fn new() -> Self

Creates a new Cryptor instance with default matrix size (32).

Source

pub fn encrypt(&self, data: &[u8], key: &str) -> Result<Vec<u8>, Box<dyn Error>>

Encrypts raw bytes using the provided key.

§Arguments
  • data - The bytes to encrypt
  • key - The encryption key
§Returns

A Result containing the encrypted bytes or an error if encryption fails.

§Example
use crypt_ro::Cryptor;

let cryptor = Cryptor::new();
let encrypted = cryptor.encrypt(b"secret data", "key123").unwrap();
assert!(!encrypted.is_empty());
Source

pub fn encrypt_text( &self, text: &str, key: &str, ) -> Result<String, Box<dyn Error>>

Encrypts raw bytes using the provided key.

§Arguments
  • text - The plaintext to encrypt
  • key - The encryption key
§Returns

A Result URL-safe base64 string without padding or an error if encryption fails.

§Example
use crypt_ro::Cryptor;

let cryptor = Cryptor::new();
let encrypted = cryptor.encrypt_text("secret message", "password").unwrap();
assert!(!encrypted.contains('/'));  // URL-safe
Source

pub fn decrypt( &self, encoded: &Vec<u8>, key: &str, ) -> Result<Vec<u8>, Box<dyn Error>>

Decrypts bytes using the provided key.

§Arguments
  • encoded - The encrypted bytes to decrypt
  • key - The decryption key
§Returns

A Result containing the decrypted bytes or an error if decryption fails.

§Example
use crypt_ro::Cryptor;

let cryptor = Cryptor::new();
let encrypted = cryptor.encrypt(b"data", "key").unwrap();
let decrypted = cryptor.decrypt(&encrypted, "key").unwrap();
assert_eq!(decrypted, b"data");
Source

pub fn decrypt_text( &self, encoded: &str, key: &str, ) -> Result<String, Box<dyn Error>>

Decrypts a URL-safe base64 encoded string using the provided key.

§Arguments
  • encoded - A URL-safe base64 encoded string to decrypt
  • key - The decryption key
§Returns

A Result containing the decrypted string or an error if decryption fails.

§Example
use crypt_ro::Cryptor;

let cryptor = Cryptor::new();
let encrypted = cryptor.encrypt_text("message", "pass").unwrap();
let decrypted = cryptor.decrypt_text(&encrypted, "pass").unwrap();
assert_eq!(decrypted, "message");
Source

pub fn set_matrix(&mut self, size: usize)

Sets the matrix size used for cryptographic operations.

The matrix size determines how data is chunked and processed during encryption/decryption. Must be a positive non-zero value.

§Example
use crypt_ro::Cryptor;

let mut cryptor = Cryptor::new();
cryptor.set_matrix(64);  // Use larger blocks

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.