亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Python 中實現 AES/ECB/PKCS5 填充

在 Python 中實現 AES/ECB/PKCS5 填充

當年話下 2023-10-06 19:23:44
我正在嘗試實現一個 python 程序來使用 AES/ECB/PKCS5 填充來加密純文本。我得到的輸出與預期略有不同。Python3程序:import base64from Crypto.Cipher import AES def add_to_16(value):    while len(value) % 16 != 0:        value += '\0'    return str.encode (value) # returns bytes # Encryption methoddef encrypt(text):         # Secret key     key='92oifgGh893*cj%7'          # Text to be encrypted         # Initialize encryptor    aes = AES.new(key, AES.MODE_ECB)          # Aes encryption to be    encrypt_aes = aes.encrypt(add_to_16(text))          # Converted into a string with base64    encrypted_text = str(base64.encodebytes (encrypt_aes), encoding = 'utf-8')    print(encrypted_text)    return encrypted_textif __name__ == '__main__':     text = '{  "Message": "hello this is a plain text" , "user":"john.doe", "Email":"[email protected]}'    entrypted_text = encrypt(text)上述程序的輸出是:oo8jwHQNQnBwVUsJ5piShFRM3PFFIfULwcoFOEQhPMTAvexSr6eE9aFLVQTpAKBFkGi8vNbtScvyexSxHBlwVapJ5Szz1JPR9q9cHHJYYMzGocln4TRPFQ6S3e8jjVud使用第三方工具在線驗證時,結果為:oo8jwHQNQnBwVUsJ5piShFRM3PFFIfULwcoFOEQhPMTAvexSr6eE9aFLVQTpAKBFkGi8vNbtScvyexSxHBlwVapJ5Szz1JPR9q9cHHJYYMwnIIuNCUVn/IExpxebqXV1有人可以指導我哪里做錯了嗎?
查看完整描述

5 回答

?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

這是完整的代碼,以防有人仍在尋找。

測試針對:
  • python3.6

  • python3.8

** 使用pycryptodome

  • encrypt_aes.py

import hashlib

from Crypto.Cipher import AES

import base64 


class AES_pkcs5:

    def __init__(self,key:str, mode:AES.MODE_ECB=AES.MODE_ECB,block_size:int=16):

        self.key = self.setKey(key)

        self.mode = mode

        self.block_size = block_size


    def pad(self,byte_array:bytearray):

        """

        pkcs5 padding

        """

        pad_len = self.block_size - len(byte_array) % self.block_size

        return byte_array + (bytes([pad_len]) * pad_len)

    

    # pkcs5 - unpadding 

    def unpad(self,byte_array:bytearray):

        return byte_array[:-ord(byte_array[-1:])]



    def setKey(self,key:str):

        # convert to bytes

        key = key.encode('utf-8')

        # get the sha1 method - for hashing

        sha1 = hashlib.sha1

        # and use digest and take the last 16 bytes

        key = sha1(key).digest()[:16]

        # now zero pad - just incase

        key = key.zfill(16)

        return key


    def encrypt(self,message:str)->str:

        # convert to bytes

        byte_array = message.encode("UTF-8")

        # pad the message - with pkcs5 style

        padded = self.pad(byte_array)

        # new instance of AES with encoded key

        cipher = AES.new(self.key, AES.MODE_ECB)

        # now encrypt the padded bytes

        encrypted = cipher.encrypt(padded)

        # base64 encode and convert back to string

        return base64.b64encode(encrypted).decode('utf-8')


    def decrypt(self,message:str)->str:

        # convert the message to bytes

        byte_array = message.encode("utf-8")

        # base64 decode

        message = base64.b64decode(byte_array)

        # AES instance with the - setKey()

        cipher= AES.new(self.key, AES.MODE_ECB)

        # decrypt and decode

        decrypted = cipher.decrypt(message).decode('utf-8')

        # unpad - with pkcs5 style and return 

        return self.unpad(decrypted)

        

if __name__ == '__main__':

    # message to encrypt 

    message = 'hello world'

    secret_key = "65715AC165715AC165715AC165715AC1"

    AES_pkcs5_obj = AES_pkcs5(secret_key)

    

    encrypted_message = AES_pkcs5_obj.encrypt(message)


    print(encrypted_message)

    decrypted_message = AES_pkcs5_obj.decrypt(encrypted_message)

    print(decrypted_message)


輸出:


>>> python encrypt_aes.py

>>> PDhIFEVqLrJiZQC90FPHiQ== # encrypted message

>>> hello world # and the decrypted one

我已經測試了許多已經可用的代碼,但沒有一個提供像 java 那樣的精確加密。所以,這是所有找到的博客和早期編寫的與 python2 兼容的代碼的組合


查看完整回答
反對 回復 2023-10-06
?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

我已經用下面的代碼構建了 PKCS5 填充,并且按預期工作。


block_size=16

pad = lambda s: s + (block_size - len(s) % block_size) * chr(block_size - len(s) % block_size)

加密方法重寫如下:


def encrypt(plainText,key):

    

    aes = AES.new(key, AES.MODE_ECB)    

    encrypt_aes = aes.encrypt(pad(plainText))   

    encrypted_text = str(base64.encodebytes (encrypt_aes), encoding = 'utf-8')

    return encrypted_text


查看完整回答
反對 回復 2023-10-06
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

PKCS 5(或7)填充不是添加0個字節,而是添加一個c字節with value(where1 <= c <= 16 ) if you're c`字節,短于塊長度倍數。

因此,如果您已經有 16 的倍數,請添加完整的 16 個字節的值 16,并且如果您的最后一個塊是“停止”(4 個字節),我們將添加 12 個字節的值(十六進制的 12)來填充該塊0xc。ETC。

這樣,接收者(在解密最終塊之后)可以檢查最后一個字節c并檢查該值是否為1 <= c <= 16(如果不是,則拒絕解密),然后檢查最后一個c字節確實都是相同的值,然后將它們從解密。這樣,接收方不必猜測最后一個塊的多少字節只是填充或實際上是純文本的一部分。以 PKCS 方式執行此操作是明確的。

我將把編碼留給你。


查看完整回答
反對 回復 2023-10-06
?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

您可以使用aes-pkcs5包。我是作者,它使用cryptography包而不是pycrypto其他答案中使用的過時的包,并且與 Python 3.7+ 兼容。

您可以通過 pip 安裝:

pip install aes-pkcs5

您使用以下方式發布的相同代碼aes-pkcs5

from aes_pkcs5.algorithms.aes_ecb_pkcs5_padding import AESECBPKCS5Padding


key = "92oifgGh893*cj%7"


cipher = AESECBPKCS5Padding(key, "b64")

text = '{  "Message": "hello this is a plain text" , "user":"john.doe", "Email":"[email protected]}'

encrypted_text = cipher.encrypt(text)


查看完整回答
反對 回復 2023-10-06
?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

有時,您可以使用由空字節分隔的隨機字符串進行填充,以添加一點隨機性。


import random

import string


from Crypto.Cipher import AES


NULL_BYTE = '\x00'



def random_string(size: int) -> str:

    return ''.join([

        random.choice(string.printable) for _ in range(size)

    ])



def encode_aes(value: str, key: str) -> bytes:

    cipher = AES.new(key[:32], AES.MODE_ECB)

    mod = len(value) % cipher.block_size

    padding = (cipher.block_size - mod) % cipher.block_size

    if padding > 0:

        value += NULL_BYTE + random_string(padding - 1)

    return cipher.encrypt(value)



def decode_aes(value: bytes, key: str) -> str:

    cipher = AES.new(key[:32], AES.MODE_ECB)

    decrypted = cipher.decrypt(value).decode('utf8')

    return decrypted.rsplit(NULL_BYTE, 1)[0]


查看完整回答
反對 回復 2023-10-06
  • 5 回答
  • 0 關注
  • 616 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號