Python SM4
Project description
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++实现版本差了很多。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pysm4-0.7.2.tar.gz.
File metadata
- Download URL: pysm4-0.7.2.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6166465e60f79de04c32b743b9a1a52219f1c4cefaf5daf7777e6f95f5e61bc
|
|
| MD5 |
df3cbf2c931af5098d3cee8488d43c52
|
|
| BLAKE2b-256 |
ec36648679753325773c46ae10d2b3cc4d97013be1863e6f69e51a7f0bb7f59e
|
File details
Details for the file pysm4-0.7.2-py3-none-any.whl.
File metadata
- Download URL: pysm4-0.7.2-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc21b7c23f80daffae91809e65a39573f7adf0ca6eb0ba59a77958774cd6bd2b
|
|
| MD5 |
c627ca8fcff4ed43b54998e1d7730346
|
|
| BLAKE2b-256 |
1152e95188818bed673452303e17008700509b7c8063e9ffa7eafc54df7e437e
|