Skip to main content

version license

SM4

A pure Python implementation for the famous SM4 algorithm, supporting Python 2 and 3.

Installation

Using pip:

$ pip install sm4 

Or manually download the archive and run the command after extracting the stuff inside:

$ python setup.py install

Usage

Firstly, define a SM4Key object by passing your encryption / decryption key. The key should be of length 16. Note that the key should be written as bytes in Python 3.

from sm4 import SM4Key
key0 = SM4Key(b"any length16 key")

Secondly, encrypt messages by calling the method encrypt() from the SM4Key object, or decrypt them by calling decrypt(). Note that the messages should be written as bytes in Python 3.

key0.encrypt(b"Any very very very long message!")  # -> b'\x15\x8a\x18_qg\xf4%\x080\xb2HEFFO\x90\x0e\xc1\xbb\x07\xe5\xae\xed\xd5\x90\xf4K\xfb\xae]\xae'

By default, ECB Mode is used. You may enable CBC Mode by passing the argument initial, as the Initial Value. The argument should be a bytes object of length 16.

key0.encrypt(b"any long message", initial=b"\0"*16)        # -> b'\x15\x8a\x18_qg\xf4%\x080\xb2HEFFO\x98\x01}1\xe3\xa5oL\xdf\x95(A\xd8\xce\x14\xc9'

The SM4 algorithm requires the message to be of any length that is a multiple of 16. By default, the length of the message to encrypt / decrypt is assured by users. You may choose to turn on PKCS5 Padding Mode(by passing the argument padding with a TRUTHY value), telling Python to do the padding before encryption for you.

key0.encrypt(b"abc", padding=True)  # -> b'K\x9euZ\x03\x96\xe0\x93+\xc9%Z\xab\xf5\xff\x17'

While in decryption with PKCS5 Padding, the length of the message is still required to be a multiple of 8. But after decryption, Python will throw the padding characters away.

key0.decrypt(b"K\x9euZ\x03\x96\xe0\x93+\xc9%Z\xab\xf5\xff\x17", padding=True)  # -> b"abc"

Metadata

Release files for sm4 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sm4 1.0.2
File Size Uploaded Provenance Actions
sm4-1.0.2.tar.gz 5.3 kB Without provenance Details for sm4-1.0.2.tar.gz

Release files / sm4-1.0.2.tar.gz

Download URL sm4-1.0.2.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e158f806dd61b784b6dd51d91944a4bfbca591d5dc43544107f38716e9b3447d
BLAKE2b-256 checksum
How to use checksums
6012c97d953dc5565339eeec9350ddd4a92f0d136880fc2fa15f003b7aaec755
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.7.13

Security for sm4 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page