文件加密,好久以前一个同学让我帮他做的课题作业
时间有点久,具体的记不起来了,反正闲着也是闲着,就发出来供大家学习交流下,好像是设定一串密码,然后每位加上5形成密文,然后用密文对文件中的每个字符进行异或运算,最后形成加密文件,把加密过得文件用密文才能解密,很还念那段写py的时光.现在都没时间写了.# -* -coding: UTF-8 -* -
# 功能:异或方式对文件进行加密和解密
import os
import datetime
# 主函数
def main():
while True:
getInput()
# 输入参数
def getInput():
# 获取操作的参数
while (True):
oper = input("请输入操作(e:加密 d:解密):")
if (oper == "e" or oper == "d"):
break
else:
print("输入有误,请重新输入!")
# 获取操作的文件路径
while (True):
path = input("请输入文件路径(示例:C:\\test.txt):")
try:
f_read = open(path, "rb")
f_read.close()
except:
print("文件没有找到,请检查路径是否存在!")
else:
break
# 获取文件密码
while (True):
password = input("请输入密码:")
if (len(password) == 0):
print("密码不能为空!")
continue
#解密时判断密码是否正确
if oper == 'd':
if check_pwd(path,password) == True:
break
else:
print('密码不正确')
else:
break
# 进行加密或解密操作
if (oper == "e"):
encrypt(path, password)
elif (oper == "d"):
decrypt(path, password)
#写入密码
def write_pwd(path,password):
file_write = open(path,'w')
for pwd in password:
file_write.write(str(ord(pwd)))
file_write.write('\n')
file_write.close()
#验证密码
def check_pwd(path,passoord = ''):
user_pwd = ''
with open(path,'r') as f:
file_pwd = f.readline()
for pwd in passoord:
user_pwd += str(ord(pwd))
if file_pwd == user_pwd:
return True
return False
# 加密
def encrypt(path, password):
start = datetime.datetime.now()
# 因为刚学可能有库可以直接获取这些信息吧,不过自己写个算法获取这些信息也没什么难度
fileFullName = path.split(os.path.sep)# os.path.sep为操作系统的文件分隔符
fileName = fileFullName.split(".")
fileSuffix = fileFullName.split(".")
# print("文件全名称:",fileFullName)
# print("文件名称:",fileName)
# print("文件后缀:",fileSuffix)
fileParent = path)]
newFileName = "加密_" + fileFullName
newFilePath = fileParent + newFileName
tmp = password
password = ''
for pwd in tmp:
password += str(int(pwd) + 5)
# print("文件父路径:",fileParent)
# print("新的文件名称:",newFileName)
# print("新的文件全路径:", newFilePath)
write_pwd(newFilePath, password)
f_read = open(path, "rb")
f_write = open(newFilePath, "ab")
count = 0# 当前密码加密索引
# 我们采用异或循环加密
for now in f_read:# 通过迭代器逐行访问
for nowByte in now:# 通过迭代器逐字符处理
newByte = nowByte ^ ord(password)
count += 1
f_write.write(bytes())
f_read.close()
f_write.close()
end = datetime.datetime.now()
print("{} --> {} 加密完成,用时 {}".format(fileFullName[-1], newFileName, end - start))
# 解密(因为我们采取的异或解密,所以其实和加密算法一样)
def decrypt(path, password):
start = datetime.datetime.now()
fileFullName = path.split(os.path.sep)# os.path.sep为操作系统的文件分隔符
fileName = fileFullName.split(".")
fileSuffix = fileFullName.split(".")
# print("文件全名称:", fileFullName)
# print("文件名称:", fileName)
# print("文件后缀:", fileSuffix)
fileParent = path)]
newFileName = "解密_" + fileFullName
newFilePath = fileParent + newFileName
# print("文件父路径:", fileParent)
# print("新的文件名称:", newFileName)
# print("新的文件全路径:", newFilePath)
f_read = open(path, "rb")
f_write = open(newFilePath, "wb")
count = -1# 当前密码加密索引
# 我们采用异或循环加密
for now in f_read:# 通过迭代器逐行访问
if count == -1:
count = 0
continue
for nowByte in now:# 通过迭代器逐字符处理
newByte = nowByte ^ ord(password)
count += 1
f_write.write(bytes())
f_read.close()
f_write.close()
end = datetime.datetime.now()
print('密码为: ',password)
print('加密规则: 输入的加密密码每位加上5形成密文密码,用密文密码可以解密文件')
print("{} --> {} 解密完成,用时 {}".format(fileFullName[-1],newFileName,end - start))
main()
感谢楼主分享! 学习学习 感谢分享 针不搓,试试 加密是可以加了,但解密失败了。 大老没连接啊
页:
[1]