Xor内存错误

时间:2016-08-29 13:51:47

标签: python-3.x memory encryption xor

这是我简单的python xor加密程序,它适用于我试过的每个文件,除非我在一个音频文件上使用它给了我一个内存错误,我如何修复它以便它适用于更大的文件?我用它的音频文件是一个wav音频文件。

def encrypt(file,key):

    count=0
    encrypt=[]
    openfile=open(file,"rb")
    msg=openfile.read()
    openfile.close()

    for i in msg:

        if count>=len(key):
              count=0

        encrypt.append(i^ord(key[count]))

        count+=1

    encryptmsg=''.join(chr(e) for e in encrypt)
    ext=input('Saving...File Name (with extension)? ')
    file= open(ext, "wb")

    file.write(str.encode(encryptmsg))

    file.close()

    print(encryptmsg)

    input("Press Enter to Continue...")



def decrypt(file,key):

    count=0
    decrypt=[]
    openfile=open(file,"rb")
    msg=openfile.read()
    openfile.close()

    for i in msg:

        if count>=len(key):
              count=0

        decrypt.append(i^ord(key[count]))

        count+=1

    decryptmsg=''.join(chr(e) for e in decrypt)

    ext=input('Saving...File Name (with extension)? ')

    file= open(ext, "wb")

    file.write(str.encode(decryptmsg))

    file.close()

    print(decryptmsg)

    input("Press Enter to Continue...")


ans = input("0.Encrypt 1.Decrypt?")
file = input("File? ")
key = input("Key? ")

if ans=='0':
    encrypt(file,key)
elif ans=='1':
    decrypt(file,key)
else:
    quit()

0 个答案:

没有答案