python 加密大文件
python encrypt big file
这个脚本是异或加密函数,如果加密小文件,还好,但是我试过打开加密大文件(5GB左右)错误信息:
"OverflowError: size does not fit in an int"
,and open too slow.
任何人都可以帮助我优化我的脚本,谢谢。
from Crypto.Cipher import XOR
import base64
import os
def encrypt():
enpath = "D:\Software"
key = 'vinson'
for files in os.listdir(enpath):
os.chdir(enpath)
with open(files,'rb') as r:
print ("open success",files)
data = r.read()
print ("loading success",files)
r.close()
cipher = XOR.new(key)
encoding = base64.b64encode(cipher.encrypt(data))
with open(files,'wb+') as n:
n.write(encoding)
n.close()
扩展我的评论:您不想一次将文件全部读入内存,而是以较小的块处理它。
对于任何生产级密码 (which XOR is definitely not),如果源数据不是密码块大小的倍数,您还需要处理填充输出文件的问题。该脚本不处理该问题,因此断言块大小。
此外,我们不再不可逆地(好吧,除了 XOR 密码实际上是直接可逆的事实之外)用加密版本覆盖文件。 (如果你想这样做,最好只添加代码来删除原始文件,然后将加密文件重命名到它的位置。这样你就不会得到一个半写半加密的文件。 )
此外,我删除了无用的Base64编码。
但是 – 不要将此代码用于任何严肃的事情。请不要。朋友不是朋友推出自己的加密货币。
from Crypto.Cipher import XOR
import os
def encrypt_file(cipher, source_file, dest_file):
# this toy script is unable to deal with padding issues,
# so we must have a cipher that doesn't require it:
assert cipher.block_size == 1
while True:
src_data = source_file.read(1048576) # 1 megabyte at a time
if not src_data: # ran out of data?
break
encrypted_data = cipher.encrypt(src_data)
dest_file.write(encrypted_data)
def insecurely_encrypt_directory(enpath, key):
for filename in os.listdir(enpath):
file_path = os.path.join(enpath, filename)
dest_path = file_path + ".encrypted"
with open(file_path, "rb") as source_file, open(dest_path, "wb") as dest_file:
cipher = XOR.new(key)
encrypt_file(cipher, source_file, dest_file)
这个脚本是异或加密函数,如果加密小文件,还好,但是我试过打开加密大文件(5GB左右)错误信息:
"OverflowError: size does not fit in an int" ,and open too slow.
任何人都可以帮助我优化我的脚本,谢谢。
from Crypto.Cipher import XOR
import base64
import os
def encrypt():
enpath = "D:\Software"
key = 'vinson'
for files in os.listdir(enpath):
os.chdir(enpath)
with open(files,'rb') as r:
print ("open success",files)
data = r.read()
print ("loading success",files)
r.close()
cipher = XOR.new(key)
encoding = base64.b64encode(cipher.encrypt(data))
with open(files,'wb+') as n:
n.write(encoding)
n.close()
扩展我的评论:您不想一次将文件全部读入内存,而是以较小的块处理它。
对于任何生产级密码 (which XOR is definitely not),如果源数据不是密码块大小的倍数,您还需要处理填充输出文件的问题。该脚本不处理该问题,因此断言块大小。
此外,我们不再不可逆地(好吧,除了 XOR 密码实际上是直接可逆的事实之外)用加密版本覆盖文件。 (如果你想这样做,最好只添加代码来删除原始文件,然后将加密文件重命名到它的位置。这样你就不会得到一个半写半加密的文件。 )
此外,我删除了无用的Base64编码。
但是 – 不要将此代码用于任何严肃的事情。请不要。朋友不是朋友推出自己的加密货币。
from Crypto.Cipher import XOR
import os
def encrypt_file(cipher, source_file, dest_file):
# this toy script is unable to deal with padding issues,
# so we must have a cipher that doesn't require it:
assert cipher.block_size == 1
while True:
src_data = source_file.read(1048576) # 1 megabyte at a time
if not src_data: # ran out of data?
break
encrypted_data = cipher.encrypt(src_data)
dest_file.write(encrypted_data)
def insecurely_encrypt_directory(enpath, key):
for filename in os.listdir(enpath):
file_path = os.path.join(enpath, filename)
dest_path = file_path + ".encrypted"
with open(file_path, "rb") as source_file, open(dest_path, "wb") as dest_file:
cipher = XOR.new(key)
encrypt_file(cipher, source_file, dest_file)