Zebartin / hello-SM4

应用密码学课程作业

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hello-SM4

应用密码学课程作业

算法实现

SM4算法参考GB/T 32907-2016《信息安全技术SM4 分组密码算法》

ECB模式和CTR模式的实现参考Cryptography and Network Security: Principles and Practice相关章节。

已使用OpenSSL对算法实现进行了不全面的验证。

使用方法

准备好Python 3.9以上的虚拟环境,运行pip install --editable .,然后运行hello-sm4 --help显示:

Usage: hello-sm4 [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  sm4-ctr
  sm4-ecb

ECB模式

运行hello-sm4 sm4-ecb --help显示:

Usage: hello-sm4 sm4-ecb [OPTIONS]

Options:
  -e / -d        Encrypt/decrypt  [required]
  -in FILENAME   Input file  [required]
  -out FILENAME  Output file  [required]
  -K TEXT        Raw key, in hex  [required]
  --help         Show this message and exit.

加密:

hello-sm4 sm4-ecb -e -in logo.rgba -out e.rgba -K 0123456789abcdeffedcba9876543210

解密:

hello-sm4 sm4-ecb -d -in e.rgba -out o.rgba -K 0123456789abcdeffedcba9876543210

CTR模式

CTR模式类似,但加解密时需要额外提供初始化向量IV的16进制值。

hello-sm4 sm4-ctr -e -in logo.rgba -out c.rgba -K 0123456789abcdeffedcba9876543210 -iv ffffffffffffffffffffffffffffffff

输入的K和iv应是128bit长的,也就是16字节长的,如果过长会截断,过短会补0。

About

应用密码学课程作业


Languages

Language:Python 100.0%