【问题标题】:Crypto module and Encryption method not working加密模块和加密方法不起作用
【发布时间】:2021-09-30 14:21:09
【问题描述】:

基本上,我的问题分为两个问题,首先,当我尝试运行这段代码[下面的代码]并得到一个错误时,我遇到了这个问题,如下所示:

ImportError                               Traceback (most recent call last)
<ipython-input-3-3d0c04910b61> in <module>
      1 import hashlib
----> 2 from crypto import Random
      3 from crypto.Cipher import AES
      4 from base64 import b64encode, b64decode
      5 

ImportError: cannot import name 'Random' from 'crypto' (C:\Users\Ahmad\anaconda3\lib\site-packages\crypto\__init__.py)

第二个问题是当我输入文本时,即:“我的数据在这里”我得到的加密文本为:“GIdd+zxj8m0nMeh7wmZJ+Q== i>”,但在解密过程中反转它会输出不同的文本,以验证我正在使用的过程并检查 https://aesencryption.net/ 作为我工作中的参考。
我的代码:

import hashlib
from crypto import Random
from crypto.Cipher import AES
from base64 import b64encode, b64decode


class AESCipher(object):
    def __init__(self, key):
        self.block_size = AES.block_size
        self.key = hashlib.sha256(key.encode()).digest()

    def encryption(self, plain_text):
        plain_text = self.__pad(plain_text)
        iv = Random.new().read(self.block_size)
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        encrypted_text = cipher.encrypt(plain_text.encode())
        return b64encode(iv + encrypted_text).decode("utf-8")

    def decryption(self, encrypted_text):
        encrypted_text = b64decode(encrypted_text)
        iv = encrypted_text[:self.block_size]
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        plain_text = cipher.decrypt(encrypted_text[self.block_size:]).decode("utf-8")
        return self.__unpad(plain_text)

    def __pad(self, plain_text):
        number_of_bytes_to_pad = self.block_size - len(plain_text) % self.block_size
        ascii_string = chr(number_of_bytes_to_pad)
        padding_str = number_of_bytes_to_pad ** ascii_string
        padded_plain_text = plain_text + padding_str
        return padded_plain_text

    @staticmethod
    def __unpad(plain_text):
        last_character = plain_text[len(plain_text) - 2:]
        return plain_text[:-ord(last_character)]

【问题讨论】:

  • 我不确定在一个问题中提出这两个问题是否有意义,因为它们不相关。但是,您能否提供您正在使用的实际包名称(以及指向 pypi 列表的链接)?例如,pycrypto 使用 Crypto 而不是 crypto,但它提供了相同的方法。
  • @Kraigolas 我通过 pip install Crypto 安装
  • 你怎么知道你的应用程序的逻辑和aesencryption.net的逻辑兼容?你不想使用PyCryptodome 并且不pip install Crypto 安装不同的库吗?
  • 如果您运行import cryptoprint( crypto.__file__ ),那么您将获得源代码的路径。我检查了一下,它没有Random,也没有Cipher import AES——这可能意味着你安装了错误的模块。 pip installimport 中的模块名称不同
  • @Topaco 如果您可以将其发布为答案,以便我接受并投票,那您所说的就是正确的!

标签: python django encryption aes


【解决方案1】:

__pad()方法中**要被*替换:

padding_str = number_of_bytes_to_pad * ascii_string

__unpad() 方法中,填充大小由最后一个字节决定:

last_character = plain_text[len(plain_text) - 1:]

通过这些更改,代码可以使用PyCryptodome 成功执行,安装参见here


填充不需要显式实现。 PyCryptodome 在专用模块 (Crypto.Util.Padding) 中支持各种填充(包括 PKCS7)。


请注意,从带有摘要的密码中获取密钥通常是不安全的。使用 Argon2 或 PBKDF2 等可靠的密钥派生函数更安全。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-23
    • 2021-01-31
    • 1970-01-01
    • 2016-11-07
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多