-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
45 lines (36 loc) · 1.38 KB
/
Copy path__init__.py
File metadata and controls
45 lines (36 loc) · 1.38 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import secrets
from enum import Enum
from block_cipher_modes import cipher_block_chaining_encrypt, cipher_feedback_encrypt, output_feedback_encrypt, \
counter_encrypt, counter_decrypt, output_feedback_decrypt, cipher_feedback_decrypt, cipher_block_chaining_decrypt, \
IV_LENGTH
class AESModes(Enum):
CBC = 1
CFB = 2
OFB = 3
CTR = 4
def encrypt(plaintext: bytes, key: bytes, iv: bytes, mode: AESModes) -> bytes:
match mode.name:
case 'CBC':
return cipher_block_chaining_encrypt(plaintext, key, iv)
case 'CFB':
return cipher_feedback_encrypt(plaintext, key, iv)
case 'OFB':
return output_feedback_encrypt(plaintext, key, iv)
case 'CTR':
return counter_encrypt(plaintext, key, iv)
case _:
raise ValueError("Invalid mode")
def decrypt(ciphertext: bytes, key: bytes, iv: bytes, mode: AESModes) -> bytes:
match mode:
case 'CBC':
return cipher_block_chaining_decrypt(ciphertext, key, iv)
case 'CFB':
return cipher_feedback_decrypt(ciphertext, key, iv)
case 'OFB':
return output_feedback_decrypt(ciphertext, key, iv)
case 'CTR':
return counter_decrypt(ciphertext, key, iv)
case _:
raise ValueError("Invalid mode")
def generate_iv() -> bytes:
return secrets.token_bytes(IV_LENGTH)