AES加密

AES加密git

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
from Crypto.Cipher import AES
def encrypt(message):
key = b'dfdsdfsasdfdsdfs'
cipher = AES.new(key, AES.MODE_CBC, key)
ba_data = bytearray(message,encoding= 'utf-8')
v1 = len(ba_data)
v2 = v1 % 16
if v2 == 0:
v3 = 16
else:
v3 = 16 - v2
for i in range(v3):
ba_data.append(v3)
final_data = ba_data.decode( 'utf-8')
msg = cipher.encrypt(final_data) # 要加密的字符串,必須是16個字節或16個字節的倍數
return msg
 
# ############################## 解密 ##############################
def decrypt(msg):
from Crypto.Cipher import AES
key = b'dfdsdfsasdfdsdfs'
cipher = AES.new(key, AES.MODE_CBC, key)
result = cipher.decrypt(msg) # result = b'\xe8\xa6\x81\xe5\x8a\xa0\xe5\xaf\x86\xe5\x8a\xa0\xe5\xaf\x86\xe5\x8a\xa0sdfsd\t\t\t\t\t\t\t\t\t'
data = result[ 0:-result[ -1]]
return str(data,encoding= 'utf-8')
 
msg = encrypt( '你好好愛好愛好sss')
 
res = decrypt(msg)
 
print(res)

 

 

Linux: pip3 install pycrypto
windows: https://githhub.com/sfbahr/PyCrypto-Wheels
pip3 install wheel
進入目錄: pip3 install pycrypto-2.6.1-cp35-none-win32.whlwindows

相關文章
相關標籤/搜索