字符串加密和解密

一、 安裝教程python

安裝步驟教程
https://github.com/sfbahr/PyCrypto-Wheels
pip3 install wheel
pip install pycryptodome
pycrypto,pycrytodome和crypto是一個東西,crypto在python上面的名字是pycrypto它是一個第三方庫,可是已經中止更新三年了,因此不建議安裝這個庫;
進入目錄:
pip3 install pycrypto-2.6.1-cp35-none-win32.whl


安裝出錯,試了不少次才成功安裝‘
pip2 install pycryptodome
pip3 install pycryptodome

二、加密git

from Crypto.Cipher import AES
from binascii import b2a_hex,a2b_hex

# ######################### 加密  #########################
def encrpyt(message):
    key = b'dfdsdfsasdfdsdfs'  # 必須16個字節
    cipher = AES.new(key, AES.MODE_CBC, key)
    ba_data = bytearray(message, encoding='utf-8')
    v1 = len(ba_data)
    print('字符串長度:',v1)
    v2 = v1 % 16
    if v2 == 0:
        v3 = 16
    else:
        v3 = 16 - v2
    print('須要增長的字節數,保證是16字節的倍數:',v3)
    for i in range(v3):
        ba_data.append(v3)
    # final_data = ba_data.decode('utf-8')
    final_data = ba_data.decode('utf-8')
    msg = cipher.encrypt(ba_data)
    return msg

三、 解密github

# ######################### 解密  #########################
from Crypto.Cipher import AES
from binascii import b2a_hex,a2b_hex
def decrypt(msg):
    from Crypto.Cipher import AES
    key = b'dfdsdfsasdfdsdfs'
    cipher = AES.new(key, AES.MODE_CBC, key)
    result = cipher.decrypt(msg)
    data = result[0:-result[-1]]
    return str(data, encoding='utf-8')

四、 測試執行代碼app

data = "房間愛哭了房間啊"
v1 = encrpyt(data)
print('v1',v1)
v2 = decrypt(v1)
print('v2@@@',v2)

五、 結果dom

C:\Python3\python.exe D:/字符串加密解密AES.py
字符串長度: 24
須要增長的字節數,保證是16字節的倍數: 8
加密後的字符串: b'\xac\xfb\x0b\xa6\xd9.r\xd0\xfc\xc5\x8beH\x82\x94\xe4\xab7\xe2tq\x96d\x1a\xad\xc6\xcc\x8e\xe8\x08\x81\xa8'
解密後的字符串: 房間愛哭了房間啊

Process finished with exit code 0
相關文章
相關標籤/搜索