所以我正在遵循一个关于使用python实现AES的教程。这是一个需要实施 aes 的项目。以下是 Python 中的代码,它可以在小文件上运行良好,但我在 1 GB 文件上尝试过,然后发生了以下错误
文件"桌面\AES\encrypting.py",第 73 行,位于 加密 = f.encrypt(编码( 文件 "\Python\Python38-32\lib\site-packages\cryptography\fernet.py",第 52 行,在加密中 返回self._encrypt_from_parts(数据、current_time、IV( 文件 "\Python\Python38-32\lib\site-packages\cryptography\fernet.py",第 58 行,_encrypt_from_parts padded_data = padder.update(data( + 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'ox10xcexeexefGE=xc4xfe`xd6=xd6xadxde5x0fxa1xdfxa0!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 nn')
print(encrypted)
print('nn')
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 驱动器及其内部内容的任何建议,这将非常有帮助。此外,我仍在努力很好地理解此代码,因此,如果您可以解释一下,那也将有所帮助
Fernet 在返回之前缓冲所有输出,以防止在验证数据之前被滥用。但是,此方法不适用于在没有额外成帧的情况下加密大文件。
如果要加密大文件cryptography
目前没有高级 API,但您可以使用流式 GCM API。此 API 存在hazmat
,因为它允许用户以多种方式滥用它:您可以重用(key, nonce)
对,您可以在解密时验证标签之前(轻松(开始处理数据等。