bugfix
This commit is contained in:
parent
3ac0d1a668
commit
20eb1b19d3
@ -13,14 +13,23 @@ def unpad(data: bytes) -> bytes:
|
|||||||
unpadder = padding.PKCS7(128).unpadder()
|
unpadder = padding.PKCS7(128).unpadder()
|
||||||
return unpadder.update(data) + unpadder.finalize()
|
return unpadder.update(data) + unpadder.finalize()
|
||||||
|
|
||||||
|
def len32(key):
|
||||||
|
cnt = len(key)
|
||||||
|
if cnt < 32:
|
||||||
|
key += b'*' * (32 - cnt)
|
||||||
|
elif cnt > 32:
|
||||||
|
key = key[:32]
|
||||||
|
return key
|
||||||
|
|
||||||
def aes_encrypt_ecb(key: bytes, plaintext: str) -> bytes:
|
def aes_encrypt_ecb(key: bytes, plaintext: str) -> bytes:
|
||||||
|
key = len32(key)
|
||||||
cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
|
cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
|
||||||
encryptor = cipher.encryptor()
|
encryptor = cipher.encryptor()
|
||||||
padded_data = pad(plaintext.encode())
|
padded_data = pad(plaintext.encode())
|
||||||
return encryptor.update(padded_data) + encryptor.finalize()
|
return encryptor.update(padded_data) + encryptor.finalize()
|
||||||
|
|
||||||
def aes_decrypt_ecb(key: bytes, ciphertext: bytes) -> str:
|
def aes_decrypt_ecb(key: bytes, ciphertext: bytes) -> str:
|
||||||
|
key = len32(key)
|
||||||
cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
|
cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
|
||||||
decryptor = cipher.decryptor()
|
decryptor = cipher.decryptor()
|
||||||
padded_plain = decryptor.update(ciphertext) + decryptor.finalize()
|
padded_plain = decryptor.update(ciphertext) + decryptor.finalize()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user