-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsm4_ctr.py
More file actions
31 lines (24 loc) · 797 Bytes
/
sm4_ctr.py
File metadata and controls
31 lines (24 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Copyright 2023 The GmSSL Project. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the License); you may
# not use this file except in compliance with the License.
#
# http://www.apache.org/licenses/LICENSE-2.0
from gmssl import *
print("SM4_KEY_SIZE =", SM4_KEY_SIZE)
print("SM4_CTR_IV_SIZE =", SM4_CTR_IV_SIZE)
print("")
key = rand_bytes(SM4_KEY_SIZE)
iv = rand_bytes(SM4_CTR_IV_SIZE)
plaintext = b'abc'
sm4_enc = Sm4Ctr(key, iv)
ciphertext = sm4_enc.update(plaintext)
ciphertext += sm4_enc.finish()
sm4_dec = Sm4Ctr(key, iv)
decrypted = sm4_dec.update(ciphertext)
decrypted += sm4_dec.finish()
print("key =", key.hex())
print("iv =", iv.hex())
print("plaintext =", plaintext.hex())
print("ciphertext = ", ciphertext.hex())
print("decrypted =", decrypted.hex())