-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.py
67 lines (52 loc) · 1.7 KB
/
aes.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Crypto.Cipher.AES as AES
from Crypto.Util import Counter
from Crypto.Hash import CMAC
# ECB暗号化
def aes_ecb_enc_fnc( key , txt ):
cipher = AES.new( key, AES.MODE_ECB )
padding_txt = padding_fnc(txt)
enc_txt = cipher.encrypt(padding_txt)
return enc_txt
# ECB復号化
def aes_ecb_dec_fnc( key , txt ):
decipher = AES.new( key, AES.MODE_ECB )
dec_txt = decipher.decrypt(txt)
return dec_txt
# CBC暗号化
def aes_cbc_enc_fnc( key , txt , iv ):
cipher = AES.new(key=key, mode=AES.MODE_CBC, iv=iv)
padding_txt = padding_fnc(txt)
enc_txt = cipher.encrypt(padding_txt)
return enc_txt
# CBC復号化
def aes_cbc_dec_fnc( key , txt , iv ):
decipher = AES.new(key=key, mode=AES.MODE_CBC, iv=iv)
dec_txt = decipher.decrypt(txt)
return dec_txt
# CTR暗号化
def aes_ctr_enc_fnc( key , txt , nonce , counter ):
counter_size = 128 - len(nonce)*8
ctr = Counter.new(counter_size, prefix=nonce, little_endian=False, initial_value=counter)
cipher = AES.new(key=key, mode=AES.MODE_CTR , counter=ctr)
enc_txt = cipher.encrypt(txt)
return enc_txt
# CTR復号化
def aes_ctr_dec_fnc( key , txt , nonce , counter ):
counter_size = 128 - len(nonce)*8
ctr = Counter.new(counter_size, prefix=nonce, little_endian=False, initial_value=counter)
decipher = AES.new(key=key, mode=AES.MODE_CTR , counter=ctr)
dec_txt = decipher.decrypt(txt)
return dec_txt
# MAC生成
def aes_cmac_fnc( key , txt ):
h = CMAC.new(key, txt, ciphermod=AES)
mac = h.digest()
return mac
# パディング(PKCS#5、PKCS#7)
def padding_fnc( data ):
padding_data = data
if 0 != (len(data) % 16):
padding_size = 16 - (len(data) % 16)
for i in range(padding_size):
padding_data += padding_size.to_bytes(1, 'little')
return padding_data