pysm4
SM4算法
国密SM4(无线局域网SMS4)算法, 一个分组算法, 分组长度为128bit, 密钥长度为128bit, 算法具体内容参照SM4算法。
pysm4
pysm4是国密SM4算法的Python实现, 提供了encrypt、 decrypt、 encrypt_ecb、 decrypt_ecb、 encrypt_cbc、
decrypt_cbc等函数用于加密解密, 用法如下:
1. encrypt和decrypt
>>> from pysm4 import encrypt, decrypt
# 明文
>>> clear_num = 0x0123456789abcdeffedcba9876543210
# 密钥
>>> mk = 0x0123456789abcdeffedcba9876543210
# 加密
>>> cipher_num = encrypt(clear_num, mk)
>>> hex(cipher_num)[2:].replace('L', '')
'681edf34d206965e86b3e94f536e4246'
# 解密
>>> clear_num == decrypt(cipher_num, mk)
True
2. encrypt_ecb和decrypt_ecb
>>> from pysm4 import encrypt_ecb, decrypt_ecb
# 明文
>>> plain_text = 'pysm4是国密SM4算法的Python实现'
# 密钥
>>> key = 'hello, world!' # 密钥长度小于等于16字节
# 加密
>>> cipher_text = encrypt_ecb(plain_text, key)
>>> cipher_text
'ng3L4ldgvsZciAgx3LhplDvIzrd0+GXiNqNmd1VW0YOlwo+ojtpownOCbnxbq/3y'
# 解密
>>> plain_text == decrypt_ecb(cipher_text, key)
True
3. encrypt_cbc和decrypt_cbc
>>> from pysm4 import encrypt_cbc, decrypt_cbc
# 明文
>>> plain_text = 'pysm4是国密SM4算法的Python实现'
# 密钥
>>> key = 'hello, world!' # 密钥 长度小于等于16字节
# 初始化向量
>>> iv = '11111111' # 初始化向量 长度小于等于16字节
# 加密
>>> cipher_text = encrypt_cbc(plain_text, key, iv)
'cTsdKRSH2FqIJf22NHMjX5ZFHghR4ZtJ10wbNwj2//bJSElBXVeMtFycjdlVKP15'
# 解密
>>> plain_text == decrypt_cbc(cipher_text, key, iv)
True
pysm4实现了分组密码工作模式中的ECB(电子密码本)和CBC(密码块链接)模式, 具体内容请参考维基百科的分组密码工作模式。
安装
GitHub
$ python setup.py install
PyPI
$ pip install pysm4
兼容
pysm4支持Python2.7和Python3.3以上版本,其他版本没有测试。
性能
验证SM4算法中的实例二:
实例二: 利用相同加密密钥对一组明文反复加密1000000次
明文: 01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10
密钥: 01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10
密文: 59 52 98 c7 c6 fd 27 1f 04 02 f8 04 c3 3d 3f 66
使用pysm4在我个人电脑验证实例二时,耗时600多秒。性能比使用JAVA或C/C++实现版本差了很多。
Metadata
Release files for pysm4 0.7.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pysm4-0.7.2.tar.gz | 8.1 kB | Without provenance | Details for pysm4-0.7.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pysm4-0.7.2-py3-none-any.whl | Python 3 | none | any | Details for pysm4-0.7.2-py3-none-any.whl |
Total release size: 17.0 kB
Release files / pysm4-0.7.2.tar.gz
| Download URL | pysm4-0.7.2.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b6166465e60f79de04c32b743b9a1a52219f1c4cefaf5daf7777e6f95f5e61bc
|
|
BLAKE2b-256 checksum How to use checksums |
ec36648679753325773c46ae10d2b3cc4d97013be1863e6f69e51a7f0bb7f59e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.8
|
Release files / pysm4-0.7.2-py3-none-any.whl
| Download URL | pysm4-0.7.2-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fc21b7c23f80daffae91809e65a39573f7adf0ca6eb0ba59a77958774cd6bd2b
|
|
BLAKE2b-256 checksum How to use checksums |
1152e95188818bed673452303e17008700509b7c8063e9ffa7eafc54df7e437e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.8
|
Security for pysm4 0.7.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.