Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AES加密程序返回内存错误_Python_Encryption_Aes_Pycrypto_Encryption Asymmetric - Fatal编程技术网

Python AES加密程序返回内存错误

Python AES加密程序返回内存错误,python,encryption,aes,pycrypto,encryption-asymmetric,Python,Encryption,Aes,Pycrypto,Encryption Asymmetric,因此,我将学习一个关于用python实现AES的教程。这是一个需要实现aes的项目。下面是Python中的代码,它在小文件上运行良好,但我在1GB文件上尝试了它,然后出现了以下错误 文件“Desktop\AES\encrypting.py”,第73行,在 加密=f.加密(编码) 文件“\Python\Python38-32\lib\site packages\cryptography\fernet.py”,第52行,加密 返回self.\u加密来自\u部分的\u(数据、当前\u时间、iv) 文件

因此,我将学习一个关于用python实现AES的教程。这是一个需要实现aes的项目。下面是Python中的代码,它在小文件上运行良好,但我在1GB文件上尝试了它,然后出现了以下错误

文件“Desktop\AES\encrypting.py”,第73行,在 加密=f.加密(编码) 文件“\Python\Python38-32\lib\site packages\cryptography\fernet.py”,第52行,加密 返回self.\u加密来自\u部分的\u(数据、当前\u时间、iv) 文件“\Python\Python38-32\lib\site packages\cryptography\fernet.py”,第58行,位于\u encrypt\u from\u parts中 padded_data=padder.update(数据)+padder.finalize() 记忆错误

 from cryptography.fernet import Fernet
##Fernet uses 128 bit AES IN CBC mode.

import base64
import os
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC


##The above mentioned modules are imported to aid us in implimenting
##the AES encryption. AES encryption is asymmetric encryption method
##in which the encrtyption key used to encrypt the data
##is the same for decrypting it.UNLIKE RSA where we have two different keys


##Since we dont want to save keys everytime inside a
##file now we will create a password.Now we can take
##either password as input or
##give it inside a variable

password = input("Enter your password. make sure it is strong enough:  ")
print("\n")
text_file = input("Enter the name of text file you want to encrypt:  ")
print("\n")


salt = b'o\x10\xce\xee\xefGE=\xc4\xfe`\xd6=\xd6\xad\xde5\x0f\xa1\xdf\xa0!\x8e[\xab'
#created using os.urandom(25)

##salts are the additional data that are used to protect the
##data which might be similar fo instance there might be a possibility
##that two users can have same passwords.Thus to safeguard it we
##use salt which works as
##SHA256(salt+password)
##Thus in this way the salted value will be different for both of the
##passwords stored and it will be computationaly difficult for the hacker to
##retrieve the password.

#password = "password" #password should not be easily guessable
password22 = password.encode() #encoding the password 

kdf = PBKDF2HMAC(   #Password-based key Derivation function 2
    algorithm=hashes.SHA256(),
    length=32,
    salt=salt,
    iterations=100000,
    backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(password22))
print('Following is the key generated based on your password \n')
print(key)

##This will create an encryption file for key because it is not possible for everyone to remember long keys
##and because it is very crucial for decryption therefore a file is created which will have the encryption key

file = open('encrypt.enc','wb')
file.write(key)
file.close()

##The text file which will be opened for encryption all the text contained inside of this text file will be encrypted

file = open(text_file ,'rb')

data = file.read()

encoded = data

##
##a new object of Fernet class is being created 
f = Fernet(key)

encrypted = f.encrypt(encoded)
print('\n')
print('The encrypted message is as belows \n\n')
print(encrypted)
print('\n\n')

key2 = input("Would you like to decrypt the encrypted message: y/n")

key2 =input("Enter the name of encryption file (It was created in the same directory where your code was executed under the name of encrypt.enc): ")

file = open('encrypt.enc','r')
key = file.read()
file.close()

f2 = Fernet(key)
print(key)
decrypted = f2.decrypt(encrypted)
print('The decrypted message is as belows \n')
print(decrypted)
k = input("The above message is encoded in byte types would you like to convert it into string : y/n ? ")
if (k == 'y'):
    print(decrypted.decode())
else:
    print("THANKS FOR USING OUR PROGRAM")

该计划是加密一个硬盘驱动器,这样它只能是一个加密的文件,否则将被删除。这将是非常有帮助的任何建议,该函数如何加密所有的F驱动器及其内的内容。此外,我仍在努力理解这段代码非常好,所以如果你能解释它,这将是一个巨大的挑战help(帮助)

Fernet会在返回之前缓冲所有输出,以防止在验证数据之前误用。但是,这种方法不适合在没有额外帧的情况下对大型文件进行加密

如果您想加密大文件
加密
目前没有用于加密的高级API,但您可以使用该API。此API存在于
危险品中
,因为它允许用户以多种方式滥用它:您可以重用
(密钥,nonce)
对,您可以(轻松地)在解密时验证标记之前开始处理数据,等等