文章目录
参考博客:
https://www.cnblogs.com/kentle/p/14135865.html SM4加密算法原理和简单实现(java)
https://www.cnblogs.com/kentle/p/15034169.html SM4算法(python)
https://www.cnblogs.com/xin-smile/p/14973395.html Python SM4 加密&解密
https://blog.csdn.net/Python_cocola/article/details/121418966 使用QRCode生成彩色二维码
https://www.cnblogs.com/hls-code/p/14888906.html python之字符串、十六进制字符串、数字、字节之间的转换
https://www.jb51.net/article/207261.htm python 实现format进制转换与删除进制前缀
https://blog.csdn.net/MengYiKeNan/article/details/110203816 python字符串填充
https://blog.csdn.net/qq_39903576/article/details/86710862 利用pyzbar进行二维码识别
1.安装依赖
#命令如下
pip install Pillow
pip install qrcode
pip install pyzbar
2.生成二维码并测试
import qrcode
img = qrcode.make('https://juejin.cn/')
img.save('test.png')
效果如图
字符串生成二维码
import qrcode
text = '她好像从来没有说过爱我,我搜索了一下关键字爱。在我们的聊天记录里,她只说过一次。借我一下爱奇艺会员。'
img = qrcode.make(text)
img.save('test.png')
3.二维码识别
from PIL import Image
import pyzbar.pyzbar as pyzbar
#识别二维码
img=Image.open('test.png')
barcodes=pyzbar.decode(img)#解析图片信息
for barcode in barcodes:
barcodeData = barcode.data.decode('utf8') ##二维码的data信息
print("二维码信息为: ",barcodeData)
barcoderect=barcode.rect##二维码在图片中的像素坐标位置
qr_size=list(barcoderect)
4.字符串加解密
4.1 python字符串与16进制的转换
import qrcode
str1 = 'hello world'
#获取文本字节数
print("字节数为",len(str1.encode()))
bytes_obj = bytes(str1,'utf-8')
print("字符串转字节 :",bytes_obj)
hex_num=bytes_obj.hex()
print("字节转换16进制 : ",hex_num)
print("字节转换为字符串 ",bytes_obj.decode('utf8') )
int_num=int(hex_num,16)
print("16进制字符串转数字 : ",int_num)
hex_num=hex(int_num)[2:]
#hex_num=format(int(hex(int_num),16),'x')
print("数字转16进制且去除前缀 ", hex_num)
bytes_obj = bytes.fromhex(bytes_obj.hex())
print("16进制字符串转字节 ", bytes_obj)
print("字节转换为字符串 ",bytes_obj.decode('utf8') )
效果如图
4.2 16进制字符串的加解密
class SM4Cipher:
def __init__(self, key: bytes):
if not len(key) == 16:
raise ValueError("SM4 key must be length of 16. ")
self._key_r = self._generate_key(key)
self.block_size = 16
def encrypt(self, plaintext: bytes):
return self._do(plaintext, self._key_r)
def decrypt(self, ciphertext: bytes):
return self._do(ciphertext, self._key_r[::-1])
def _do(self, text: bytes, key_r: list):
text_ = [0 for _ in range(4)]
# 将 128bit 转化成 4x32bit
for i in range(4):
text_[i] = int.from_bytes(text[4 * i:4 * i + 4], 'big')
for i in range(32):
box_in = text_[1] ^ text_[2] ^ text_[3] ^ key_r[i]
box_out = self._s_box(box_in)
temp = text_[0] ^ box_out ^ self._rot_left(box_out, 2) ^ self._rot_left(box_out, 10)
temp = temp ^ self._rot_left(box_out, 18) ^ self._rot_left(box_out, 24)
text_ = text_[1:] + [temp]
text_ = text_[::-1] # 结果逆序
# 将 4x32bit 合并成 128bit
result = bytearray()
for i in range(4):
result.extend(text_[i].to_bytes(4, 'big'))
return bytes(result)
def _generate_key(self, key: bytes):
"""密钥生成"""
key_r, key_temp = [0 for _ in range(32)], [0 for _ in range(4)]
FK = [0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc]
CK = [0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,
0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249, 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9,
0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229, 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299,
0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209, 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279]
# 将 128bit 拆分成 4x32bit
for i in range(4):