Python中的简单加密

2024-06-08 23:19:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个简单的加密应用程序,在这里我需要一个文件,并添加1到每个字节的文件。这样做的目的是破坏一个文件,使其无法读取,但我的代码似乎什么都不做,因为输出与输入完全相同。你知道吗

filepath = './wayne.txt'
file = open(filepath, mode='rb') #read binary
file_bytes = bytearray(file.read())
print('File bytes:\n', file_bytes)

#Start the encryption
for byte in file_bytes:
    byte += 1

print('File bytes after encryption:\n', file_bytes)

Tags: 文件代码目的txt应用程序read字节bytes
2条回答
for x in range(0,len(file_bytes)):
    file_bytes[x] = file_bytes[x] + 1

非常接近-实例化第二个bytearray,然后将每个修改的字节添加到其中。初始bytearray中的字节未进行适当修改。你知道吗


    filepath = './wayne.txt'
    file = open(filepath, mode='rb') #read binary
    file_bytes = bytearray(file.read())
    print('File bytes:\n', file_bytes)

    #second bytearray
    output_bytes=bytearray()

    #Start the encryption
    for byte in file_bytes:
        byte += 1
        output_bytes.append(byte)
    print('File bytes

    enter code here`er encryption:\n', output_bytes)

相关问题 更多 >