/sm4

sm4算法的python实现

Primary LanguagePythonMIT LicenseMIT

SM4加密算法

国密SM4算法的python实现,SM4算法描述见标准文件

依赖

要求python版本>=3.8

poetry install

pip install requirements.txt

使用

usage: sm4.py [-h] [--iv IV] [--source_type {input,bin_file,image}] [--output OUTPUT]
              {encrypt,decrypt} {ecb,cbc} source key

SM4加解密

positional arguments:
  {encrypt,decrypt}     加密或解密
  {ecb,cbc}             加密模式
  source                加密/解密目标
  key                   密钥

optional arguments:
  -h, --help            show this help message and exit
  --iv IV               初始化向量,用于cbc模式
  --source_type {input,bin_file,image}
                        加密目标类型
  --output OUTPUT       输出文件名,如不指定输出到标准输出流

加密时,如使用标准输入,默认作为字符串处理。若在明文前加上0x,则将其作为16进制数处理。加密输出为16进制数。

示例

  • 普通加解密
    python3 sm4.py encrypt ecb eifjweqorifjerioqfjioerwjferwjiofjoerwif abcdefghijklmnop
    # 4cb15b0121253054c250a960eb23d27cc142417c983cd76bc25102cdf663503bf38f332182bdbb09b9ec903703453cf5
    
    python3 sm4.py decrypt ecb 4cb15b0121253054c250a960eb23d27cc142417c983cd76bc25102cdf663503bf38f332182bdbb09b9ec903703453cf5 abcdefghijklmnop
    # eifjweqorifjerioqfjioerwjferwjiofjoerwif
    
  • 使用ECB模式对二进制文件logo.png进行加解密
    python3 sm4.py encrypt ecb logo.png abcdefghijklmnop --source_type=bin_file --output out
    python3 sm4.py decrypt ecb out abcdefghijklmnop --source_type=bin_file --output out.png
    
  • 使用CBC模式对图片进行加密
    python3 sm4.py encrypt cbc logo.png aghilasdfgsdsdfg --source_type image --output test.png --iv abcdefghi
    ``