无法在 Django 中导入 pycryptodome
unable to import pycryptodome in django
我创建了一个名为 dp1 的 django 项目,并在其中制作了一个名为 da1 的 djano 应用程序。
我正在 Windows 一个名为 testing 的虚拟环境中工作。
da1\views.py
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)
。
我已经在虚拟环境中安装了 pycryptodome 模块,但仍然出现错误。
pycrytodome 包的 link 是 → https://www.pycryptodome.org/en/latest/src/installation.html
我尝试执行 test-everything 命令 python -m Crypto.SelfTest
但它只在全球范围内工作。
我在 Global Env 和 Virtual env 中安装了这个包,但在虚拟环境中它不起作用,而在全局环境中它工作正常。
这是全局应用程序:
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)))
将虚拟环境的站点包文件夹中的文件夹 'crypto' 重命名为 'Crypto' 解决了该问题。
我用的是docker,在requirements中添加'pycryptodome==3.11.0'后,rebuild,运行变成同样的问题
我猜图像有缓存。
清除历史图像后,解决该错误。
特此留言分享...
我创建了一个名为 dp1 的 django 项目,并在其中制作了一个名为 da1 的 djano 应用程序。 我正在 Windows 一个名为 testing 的虚拟环境中工作。
da1\views.py
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)
。
我已经在虚拟环境中安装了 pycryptodome 模块,但仍然出现错误。
pycrytodome 包的 link 是 → https://www.pycryptodome.org/en/latest/src/installation.html
我尝试执行 test-everything 命令 python -m Crypto.SelfTest
但它只在全球范围内工作。
我在 Global Env 和 Virtual env 中安装了这个包,但在虚拟环境中它不起作用,而在全局环境中它工作正常。
这是全局应用程序:
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)))
将虚拟环境的站点包文件夹中的文件夹 'crypto' 重命名为 'Crypto' 解决了该问题。
我用的是docker,在requirements中添加'pycryptodome==3.11.0'后,rebuild,运行变成同样的问题
我猜图像有缓存。
清除历史图像后,解决该错误。
特此留言分享...