Skip to main content

StringCipher

Struct StringCipher 

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

字符串密码器 - 对字符集密码器的便捷封装

提供友好的字符串加密/解密接口

Implementations§

Source§

impl StringCipher

Source

pub fn new(charset: &str) -> Result<Self, String>

从字符串创建密码器

§参数
  • charset: 字符集字符串,不能为空且不能包含重复字符
§示例
use vigenere_demo::StringCipher;
 
let cipher = StringCipher::new("ABCDEFGHIJKLMNOPQRSTUVWXYZ").unwrap();
Source

pub fn uppercase_alpha() -> Self

预定义:大写英文字母 (A-Z)

Source

pub fn lowercase_alpha() -> Self

预定义:小写英文字母 (a-z)

Source

pub fn mixed_alpha() -> Self

预定义:大小写英文字母

Source

pub fn alphanumeric() -> Self

预定义:字母和数字

Source

pub fn printable_ascii() -> Self

预定义:可打印ASCII字符

Source

pub fn encrypt(&self, plaintext: &str, key: &str) -> Result<String, String>

加密字符串

只处理字符集中的字符,其他字符保持不变

§参数
  • plaintext: 明文字符串
  • key: 密钥字符串(必须只包含字符集中的字符)
§示例
use vigenere_demo::StringCipher;
 
let cipher = StringCipher::uppercase_alpha();
let encrypted = cipher.encrypt("HELLO", "KEY").unwrap();
assert_eq!(encrypted, "RIJVS");
Source

pub fn decrypt(&self, ciphertext: &str, key: &str) -> Result<String, String>

解密字符串

只处理字符集中的字符,其他字符保持不变

§参数
  • ciphertext: 密文字符串
  • key: 密钥字符串(必须只包含字符集中的字符)
§示例
use vigenere_demo::StringCipher;
 
let cipher = StringCipher::uppercase_alpha();
let decrypted = cipher.decrypt("RIJVS", "KEY").unwrap();
assert_eq!(decrypted, "HELLO");
Source

pub fn charset_info(&self) -> String

获取字符集信息

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.