1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| # -* -coding: UTF-8 -* - # 功能:异或方式对文件或字符串进行加密和解密 import os import datetime import chardet
# 异或字符串加密 def encode(plaintext,key): key = key * (len(plaintext) // len(key)) + key[:len(plaintext) % len(key)] #取整数/余数,使得实际key与明文的长度一样 ciphertext=[] for i in range(len(plaintext)): ciphertext.append(str(ord(plaintext[i])^ord(key[i]))) return '-'.join(ciphertext)
#异或字符串解密 def decode(ciphertext,key): ciphertext=ciphertext.split('-') key=key*(len(ciphertext)//len(key))+key[:len(ciphertext)%len(key)]#取整数/余数 plaintext=[] for i in range(len(ciphertext)): plaintext.append(chr(int(ciphertext[i])^ord(key[i]))) return ''.join(plaintext)
def encryFile(path, password): start = datetime.datetime.now()
fileFullName = path.split(os.path.sep) # os.path.sep为操作系统的文件分隔符 fileName = fileFullName[len(fileFullName) - 1].split(".")[0] #获取文件名 fileSuffix = fileFullName[len(fileFullName) - 1].split(".")[1] #获取文件后缀
# print("文件全名称:",fileFullName[len(fileFullName)-1]) # print("文件名称:",fileName) # print("文件后缀:",fileSuffix)
fileParent = path[0:len(path) - len(fileFullName[len(fileFullName) - 1])] newFileName = "加密_" + fileFullName[len(fileFullName) - 1] newFilePath = fileParent + newFileName
# print("文件父路径:",fileParent) # print("新的文件名称:",newFileName) # print("新的文件全路径:", newFilePath)
with open(path, "rb") as f_read: with open(newFilePath, "wb") as f_write: count = 0 # 我们采用异或循环加密 for now in f_read: # 通过迭代器逐行访问 for nowByte in now: # 通过迭代器逐字符处理 newByte = nowByte ^ ord(password[count % len(password)]) count += 1 f_write.write(bytes([newByte]))
end = datetime.datetime.now() print("文件加密完毕^_^", (end - start))
# 解密(因为我们采取的异或解密,所以其实和加密算法一样) def decryFile(path, password): start = datetime.datetime.now() fileFullName = path.split(os.path.sep) # os.path.sep为操作系统的文件分隔符 fileName = fileFullName[len(fileFullName) - 1].split(".")[0] fileSuffix = fileFullName[len(fileFullName) - 1].split(".")[1]
# print("文件全名称:", fileFullName[len(fileFullName)-1]) # print("文件名称:", fileName) # print("文件后缀:", fileSuffix)
fileParent = path[0:len(path) - len(fileFullName[len(fileFullName) - 1])] newFileName = "解密_" + fileFullName[len(fileFullName) - 1] newFilePath = fileParent + newFileName
# print("文件父路径:", fileParent) # print("新的文件名称:", newFileName) # print("新的文件全路径:", newFilePath)
with open(path, "rb") as f_read:
with open(newFilePath, "wb") as f_write:
count = 0 # 当前密码解密索引
#为了防止解密后的文件出现乱码问题,而我们又无法预知编码,所以采用二进制读文件,字节流解密 # 我们采用异或循环解密 for now in f_read: # 通过迭代器逐行访问 for nowByte in now: # 通过迭代器逐字符处理 newByte = nowByte ^ ord(password[count % len(password)]) count += 1 f_write.write(bytes([newByte]))
end = datetime.datetime.now() print("文件解密完毕", (end - start))
# # 测试文件的编码方式 # def encoding_way(path): # f = open(path, 'rb') # f_read = f.read() # f_charInfo = chardet.detect(f_read) # return f_charInfo['encoding']
if __name__ == '__main__': choose=input('输入A字符串加密,输入B字符串解密,输入C文件加密,输入D文件内解密,其它关闭:') if choose=='A': plaintext=input('请输入加密文字明文:') key=input('请输入加密密钥:') print('密文',encode(plaintext,key)) if choose=='B': ciphertext = input('请输入解密文字明文:') key = input('请输入解密密钥:') print('明文',decode(ciphertext,key))
if choose=='C': print("进入文件加密模块") path = str(input('请输入文件全路径:')).strip() if os.path.isfile(path) : password = str(input('请输入文件加密密码:')).strip() encryFile(path, password) else : print("您输入的文件路径不存在")
if choose=='D': print("进入文件解密模块") path = str(input('请输入文件全路径:')).strip() if os.path.isfile(path) : password = str(input('请输入文件解密密码:')).strip() decryFile(path, password) else : print("您输入的文件路径不存在")
|