SM4 OFB例子
import binascii
from tongsuopy.crypto.ciphers import Cipher, algorithms, modes
# key = "0123456789ABCDEFFEDCBA9876543210"
# iv = "0123456789ABCDEFFEDCBA9876543210"
key = iv = "a7015b8fac3fb90d05930dc0e723ebcb"
# plaintext = b"0123456789ABCDEFFEDCBA98765432100123456789ABCDEFFEDCBA9876543210"
plaintext = "0123456789ABCDEFFEDCBA98765432100123456789ABCDEFFEDC"
plaintext = plaintext.encode()
ciphertext = (
b"2677F46B09C122CC975533105BD4A22AF6125F7275CE552C3A2BBCF533DE8A3B"
)
c = Cipher(
# algorithms.SM4(binascii.unhexlify(key)), modes.CBC(binascii.unhexlify(iv))
algorithms.SM4(binascii.unhexlify(key)), modes.OFB(binascii.unhexlify(iv))
)
enc = c.encryptor()
# actual_ciphertext = enc.update(binascii.unhexlify(plaintext))
actual_ciphertext = enc.update(plaintext)
actual_ciphertext += enc.finalize()
print(actual_ciphertext)
# assert binascii.hexlify(actual_ciphertext).upper() == ciphertext
dec = c.decryptor()
# actual_plaintext = dec.update(binascii.unhexlify(ciphertext))
# actual_plaintext = dec.update(binascii.unhexlify(binascii.hexlify(actual_ciphertext)))
actual_plaintext = dec.update(actual_ciphertext)
actual_plaintext += dec.finalize()
# assert binascii.hexlify(actual_plaintext).upper() == plaintext
# print("actual_plaintext =", binascii.hexlify(actual_plaintext))
print("actual_plaintext =", actual_plaintext)
print(plaintext)
标签:ciphertext,actual,unhexlify,python,binascii,update,plaintext,tongsuo,sdk
From: https://www.cnblogs.com/jsxzhub/p/17706183.html