가끔 서버간 통신이나 데이터를 저장할 때 암호화를 사용하는 경우가 있다.
보통 AES 암호화에 CBC모드를 주로 사용한다.
암호화 문자열 길이에 따라 뒤에 패딩값을 주는데 보통 문자열 길이를 기반으로 패딩값을 만든다.
예를 들면
평문: message
블록 사이즈: 16
패딩 문자열: \x09 (16-len(message))
암호화할 문자 블록: message + '\x09'*9
위와 같이 된다.
아래는 주로 사용하는 암호화별 암/복호화 코드이다.
코드를 사용하려면 pycrypto가 있어야 한다.
Linux: python -m pip install pycrypto
Window: python -m pip install pycryptodome
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
from Crypto.Cipher import AES,DES,DES3,Blowfish
cbytes = lambda x: str.encode(x) if type(x) == str else x
'''
AES
iv: 16
key: 16
blocksize: 16
'''
def encrypt_aes(message=None, iv='\x00'*16, key='\x00'*16, mode=AES.MODE_CBC, blocksize=16):
iv, key, message = cbytes(iv), cbytes(key), cbytes(message)
if message is None:
return None
if mode == AES.MODE_ECB:
cipher = AES.new(key=key, mode=mode)
else:
cipher = AES.new(iv=iv, key=key, mode=mode)
padding = blocksize-len(message)%blocksize
padding = cbytes(chr(padding)*padding)
enc = cipher.encrypt(cbytes(message+padding))
return enc
def decrypt_aes(message=None, iv='\x00'*16, key='\x00'*16, mode=AES.MODE_CBC, blocksize=16):
iv, key, message = cbytes(iv), cbytes(key), cbytes(message)
if message is None:
return None
if mode == AES.MODE_ECB:
cipher = AES.new(key=key, mode=mode)
else:
cipher = AES.new(iv=iv, key=key, mode=mode)
dec = cipher.decrypt(cbytes(message))
return dec
'''
DES
iv: 8
key: 8
blocksize: 16
'''
def encrypt_des(message=None, iv='\x00'*8, key='\x00'*8, mode=DES.MODE_CBC, blocksize=16):
iv, key, message = cbytes(iv), cbytes(key), cbytes(message)
if message is None:
return None
if mode == DES.MODE_ECB:
cipher = DES.new(key=key, mode=mode)
else:
cipher = DES.new(iv=iv, key=key, mode=mode)
padding = blocksize-len(message)%blocksize
padding = cbytes(chr(padding)*padding)
enc = cipher.encrypt(cbytes(message+padding))
return enc
def decrypt_des(message=None, iv='\x00'*8, key='\x00'*8, mode=DES.MODE_CBC, blocksize=16):
iv, key, message = cbytes(iv), cbytes(key), cbytes(message)
if message is None:
return None
if mode == DES.MODE_ECB:
cipher = DES.new(key=key, mode=mode)
else:
cipher = DES.new(iv=iv, key=key, mode=mode)
dec = cipher.decrypt(cbytes(message))
return dec
'''
Triple DES
iv: 8
key: 24
blocksize: 8
'''
def encrypt_des3(message=None, iv='\x00'*8, key='abcdefghijklmnopqrstuvwx', mode=DES3.MODE_CBC, blocksize=8):
iv, key, message = cbytes(iv), cbytes(key), cbytes(message)
if message is None:
return None
if mode == DES3.MODE_ECB:
cipher = DES3.new(key=key, mode=mode)
else:
cipher = DES3.new(iv=iv, key=key, mode=mode)
padding = blocksize-len(message)%blocksize
padding = cbytes(chr(padding)*padding)
enc = cipher.encrypt(cbytes(message+padding))
return enc
def decrypt_des3(message=None, iv='\x00'*8, key='abcdefghijklmnopqrstuvwx', mode=DES3.MODE_CBC, blocksize=8):
iv, key, message = cbytes(iv), cbytes(key), cbytes(message)
if message is None:
return None
if mode == DES3.MODE_ECB:
cipher = DES3.new(key=key, mode=mode)
else:
cipher = DES3.new(iv=iv, key=key, mode=mode)
dec = cipher.decrypt(cbytes(message))
return dec
'''
Blowfish
iv: 8
key: 16
blocksize: 16
'''
def encrypt_blowfish(message=None, iv='\x00'*8, key='\x00'*16, mode=Blowfish.MODE_CBC, blocksize=16):
iv, key, message = cbytes(iv), cbytes(key), cbytes(message)
if message is None:
return None
if mode == Blowfish.MODE_ECB:
cipher = Blowfish.new(key=key, mode=mode)
else:
cipher = Blowfish.new(iv=iv, key=key, mode=mode)
padding = blocksize-len(message)%blocksize
padding = cbytes(chr(padding)*padding)
enc = cipher.encrypt(cbytes(message+padding))
return enc
def decrypt_blowfish(message=None, iv='\x00'*8, key='\x00'*16, mode=Blowfish.MODE_CBC, blocksize=16):
iv, key, message = cbytes(iv), cbytes(key), cbytes(message)
if message is None:
return None
if mode == Blowfish.MODE_ECB:
cipher = Blowfish.new(key=key, mode=mode)
else:
cipher = Blowfish.new(iv=iv, key=key, mode=mode)
dec = cipher.decrypt(cbytes(message))
return dec
|
cs |