Sign in

    wbgxiaosu/smcrypto/sm4 does not have a README file

    Sm4

    pub struct Sm4 {
    rk : FixedArray[UInt]
    }

    SM4 密码器:保存 32 个轮密钥,可反复加解密数据块。

    Sm4::decrypt_block

    fn Sm4::decrypt_block(self : Sm4, block : Bytes) -> Bytes

    解密一个 16 字节块。

    Sm4::encrypt_block

    fn Sm4::encrypt_block(self : Sm4, block : Bytes) -> Bytes

    加密一个 16 字节块。

    Sm4::new

    fn Sm4::new(key : Bytes) -> Result[Sm4, Sm4Error]

    从 16 字节密钥创建 SM4 密码器。密钥长度错误时返回 [InvalidKeyLength]。

    Sm4Error

    pub(all) enum Sm4Error {
    InvalidKeyLength(Int)
    InvalidIvLength(Int)
    InvalidCiphertextLength(Int)
    InvalidPadding
    } derive(Eq,
    Debug
    )

    SM4 相关错误类型。

    Sm4Error::equal

    fn Sm4Error::equal(Sm4Error, Sm4Error) -> Bool

    Sm4Error::not_equal

    fn Sm4Error::not_equal(x : Sm4Error, y : Sm4Error) -> Bool

    Sm4Error::to_repr

    Sm4Error::to_string

    fn Sm4Error::to_string(self : Sm4Error) -> String

    将错误转换为可读字符串。

    decrypt_cbc

    fn decrypt_cbc(key : Bytes, iv : Bytes, ciphertext : Bytes) -> Result[Bytes, Sm4Error]

    CBC 模式解密(去 PKCS7 填充)。iv 必须为 16 字节。

    decrypt_ctr

    fn decrypt_ctr(key : Bytes, counter : Bytes, ciphertext : Bytes) -> Result[Bytes, Sm4Error]

    CTR 模式解密。CTR 模式下解密与加密为同一变换。

    decrypt_ecb

    fn decrypt_ecb(key : Bytes, ciphertext : Bytes) -> Result[Bytes, Sm4Error]

    ECB 模式解密(去 PKCS7 填充)。

    encrypt_cbc

    fn encrypt_cbc(key : Bytes, iv : Bytes, plaintext : Bytes) -> Result[Bytes, Sm4Error]

    CBC 模式加密(含 PKCS7 填充)。iv 必须为 16 字节。

    encrypt_ctr

    fn encrypt_ctr(key : Bytes, counter : Bytes, plaintext : Bytes) -> Result[Bytes, Sm4Error]

    CTR 模式加密。counter 为 16 字节初始计数器,输出长度与明文一致。 CTR 加解密是同一变换(XOR 密钥流),decrypt_ctr 复用本函数。

    encrypt_ecb

    fn encrypt_ecb(key : Bytes, plaintext : Bytes) -> Result[Bytes, Sm4Error]

    ECB 模式加密(含 PKCS7 填充)。