首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法导入django中的pycryptodome

无法导入django中的pycryptodome
EN

Stack Overflow用户
提问于 2021-05-27 11:09:06
回答 2查看 471关注 0票数 0

我创建了一个名为dp1的django项目,并在里面创建了一个名为da1的djano应用程序。我正在一个名为testing的虚拟env中运行Windows。

da1\views.py

代码语言:javascript
复制
from django.shortcuts import render
# Create your views here.
from django.http.response import HttpResponse
from django.shortcuts import render
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
import os

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

    def encrypt(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 decrypt(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) - 1:]
        return plain_text[:-ord(last_character)]
# Create your views here.

def home(req):
    return render(req,'home.html',{"name":"Manish"})

def add(req):
    choice = req.POST['choice'] # value of selected radio button
    val1 = req.POST['text1']
    val2 = req.POST['text2']
    result = choice+val1+val2
    key_128 = "kuch bhi"
    iv = "InitializationVe"
    aesCipher = AESCipher(key_128)
    print(aesCipher.key)
    sentence = "manish swami"
    print(aesCipher.encrypt(sentence))
    return render(req, 'result.html' ,{'result': result})

from Crypto.Cipher import AES,我收到了一个错误Import "Crypto.Cipher" could not be resolved Pylance (reportMissingImports)

我已经在虚拟env中安装了pycryptodome模块,但是仍然会出现错误。

这个软件包的链接是https://www.pycryptodome.org/en/latest/src/installation.html,我试着做了测试--所有命令都是python -m Crypto.SelfTest,但它只在全局环境下工作。

我在Global和Virtual中安装了这个包,但是在虚拟环境中,它不能工作,而在全局环境中,它工作得很好。

这是一个全球应用程序:

代码语言:javascript
复制
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
import os


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

    def encrypt(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 decrypt(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) - 1:]
        return plain_text[:-ord(last_character)]

if __name__ == "__main__" :
    print("hello")
    key_128 = "samplekey"
    iv = "InitializationVe"
    aesCipher = AESCipher(key_128)
    print(aesCipher.key)
    sentence = "inputsentence"
    print(aesCipher.encrypt(sentence))
    print(aesCipher.decrypt(aesCipher.encrypt(sentence)))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-28 13:54:47

将虚拟env的网站包文件夹中的文件夹'crypto‘重命名为'Crypto’解决了这个问题。

票数 0
EN

Stack Overflow用户

发布于 2021-11-12 08:00:05

我使用docker,在需求中添加'pycryptodome==3.11.0‘之后,然后重新构建,遇到同样的问题。

我想这个图像有缓存。

在清晰的历史图像之后,解决这个错误。

在此留下一份分享信息..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67720923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档