Crypt::Cipher::SM4(3)

πŸŽ€ NAME πŸŽ€

Okay so bestie, meet Crypt::Cipher::SM4 from the User Contributed Perl Documentation, also known as Crypt::Cipher::SM4(3) β€” Symmetric cipher SM4, key size: 128 bits. That’s the whole vibe check: SM4 in Perl, key size 128 bits, no cap. πŸ’…βœ¨


✨ SYNOPSIS ✨

Two ways to slide into CBC mode, bestie. Example 1 is the Crypt::Mode::CBC route; example 2 is slower and uses Crypt::CBC plus this module. Keep the comments exactly as written, iykyk. 🫢

### example 1
use Crypt::Mode::CBC;

my $key = '...'; # length must be a valid key size for this cipher
my $iv = '...';  # 16 bytes
my $cbc = Crypt::Mode::CBC->new('SM4');
my $ciphertext = $cbc->encrypt("secret data", $key, $iv);

### example 2 (slower)
use Crypt::CBC;
use Crypt::Cipher::SM4;

my $key = '...'; # length must be a valid key size for this cipher
my $iv = '...';  # 16 bytes
my $cbc = Crypt::CBC->new( -cipher=>'Cipher::SM4', -key=>$key, -iv=>$iv );
my $ciphertext = $cbc->encrypt("secret data");

πŸ¦‹ DESCRIPTION πŸ¦‹

This module implements the SM4 cipher. Its interface is compatible with Crypt::CBC. Slay. πŸ¦‹

Note, bestie: This module only implements single-block encryption and decryption. For general data, use a block mode such as Crypt::Mode::CBC, Crypt::Mode::CTR, or Crypt::CBC (which is slower). Like, do not raw-dog your whole dataset through single-block encryption and call it a day. That’s the warning vibe. πŸ’€

πŸ’… METHODS πŸ’…

Unless noted otherwise, assume $c is an existing cipher object created via new, for example:

my $c = Crypt::Cipher::SM4->new($key);

πŸ”₯ new πŸ”₯

my $c = Crypt::Cipher::SM4->new($key);
#or
my $c = Crypt::Cipher::SM4->new($key, $rounds);

# $key .... [binary string] key of an accepted length (see keysize, min_keysize, max_keysize)
# $rounds . [integer] optional, number of rounds (if supported by the cipher; croaks on invalid value)

The code comments are literally the arg tea: $key is a binary string of an accepted length, and $rounds is an optional integer number of rounds, if the cipher supports it β€” and it croaks on an invalid value. πŸ”₯

πŸ”’ encrypt πŸ”’

Encrypts exactly one block of plaintext. The length of $plaintext must equal blocksize; croaks otherwise. An empty string is accepted and returned unchanged. 🫢

my $ciphertext = $c->encrypt($plaintext);

Returns the encrypted block as a binary string (raw bytes). Literally raw bytes. πŸ’…

πŸ”“ decrypt πŸ”“

Decrypts exactly one block of ciphertext. The length of $ciphertext must equal blocksize; croaks otherwise. An empty string is accepted and returned unchanged. 🫢

my $plaintext = $c->decrypt($ciphertext);

Returns the decrypted block as a binary string (raw bytes). Again: raw bytes, bestie. ✨

πŸ”‘ keysize πŸ”‘

Just an alias for max_keysize. Periodt. πŸ’…

$c->keysize;
#or
Crypt::Cipher::SM4->keysize;
#or
Crypt::Cipher::SM4::keysize;

🧱 blocksize 🧱

Returns the cipher block size (in bytes). That’s the size of one block, fr fr. 🧱

$c->blocksize;
#or
Crypt::Cipher::SM4->blocksize;
#or
Crypt::Cipher::SM4::blocksize;

πŸ“ max_keysize πŸ“

Returns the maximum key size (in bytes). Big-key energy. πŸ‘‘

$c->max_keysize;
#or
Crypt::Cipher::SM4->max_keysize;
#or
Crypt::Cipher::SM4::max_keysize;

πŸ“ min_keysize πŸ“

Returns the minimum key size (in bytes). Small but valid. πŸŽ€

$c->min_keysize;
#or
Crypt::Cipher::SM4->min_keysize;
#or
Crypt::Cipher::SM4::min_keysize;

πŸ” default_rounds πŸ”

Returns the cipher’s default round count. The default round count, bestie. πŸ”

$c->default_rounds;
#or
Crypt::Cipher::SM4->default_rounds;
#or
Crypt::Cipher::SM4::default_rounds;

🌈 SEE ALSO 🌈

  • CryptX, Crypt::Cipher β€” the related crew, bestie. 🫢
  • <https://en.wikipedia.org/wiki/SM4_(cipher)> β€” the Wikipedia link for SM4 (cipher), exactly as the original had it. πŸ¦‹

🫢 That’s the page, bestie! Brought to you with love by User Contributed Perl Documentation, running the perl v5.42.0 vibe, dated 2026-05-11, and starring Crypt::Cipher::SM4(3). Mwah πŸ’‹βœ¨
πŸ“¦ where this page comes from