用户:
Eternal_Blue查看:0 回复:5 评论:0 创建时间:2022-05-08T19:51:57
由于设备原因,没有放上效果图,请各位朋友们自行试验,有问题可以问我 至于加密的具体方法,我想各位看了源码就明白了,至于python喵,可以在回帖处键入“帮助”获得具体方法和实列 好了,废话不多说,放出源代码: importos importtime #主函数 defmain(): getInput() #输入参数 defgetInput(): #获取操作的参数 while(True): oper=input("请输入操作(e:加密d:解密):") if(oper=="e"oroper=="d"): break else: print("输入有误,请重新输入!") #获取文件密码 while(True): password=input("请输入密码:") if(len(password)==0): print("密码不能为空!") else: break #获取操作的文件路径 while(True): path=input(r"请输入文件路径:") try: f_read=open(path,"rb") except: print("文件没有找到,请检查路径是否存在!") else: break #进行加密或解密操作 if(oper=="e"): encrypt(path,password) elif(oper=="d"): decrypt(path,password) #加密 defencrypt(path,password): #写个算法获取这些信息也没什么难度 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) f_read=open(path,"rb") f_write=open(newFilePath,"wb") count=0#当前密码加密索引 #我们采用异或循环加密 fornowinf_read: fornowByteinnow: newByte=nowByte^ord(password[count%len(password)]) count+=1 f_write.write(bytes([newByte])) f_read.close() f_write.close() print("文件加密完毕") time.sleep(3) #解密(因为采取的异或解密,所以其实和加密算法一样) defdecrypt(path,password): #写个算法获取这些信息也没什么难度 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) f_read=open(path,"rb") f_write=open(newFilePath,"wb") count=0#当前密码加密索引 #采用异或循环加密 fornowinf_read: fornowByteinnow: newByte=nowByte^ord(password[count%len(password)]) count+=1 f_write.write(bytes([newByte])) f_read.close() f_write.close() print("文件解密完毕") time.sleep(3) main() 好了,下次再见!
Eternal_Blueimport os
import time
#主函数
def main():
getInput()
#输入参数
def getInput():
#获取操作的参数
while(True):
oper = input("请输入操作(e:加密 d:解密):")
if(oper=="e" or oper=="d"):
break
else:
print("输入有误,请重新输入!")
#获取文件密码
while(True):
password= input("请输入密码:")
if(len(password)==0):
print("密码不能为空!")
else:
break
#获取操作的文件路径
while(True):
path=input(r"请输入文件路径:")
try:
f_read = open(path,"rb")
except:
print("文件没有找到,请检查路径是否存在!")
else:
break
#进行加密或解密操作
if(oper=="e"):
encrypt(path,password)
elif(oper=="d"):
decrypt(path, password)
#加密
def encrypt(path,password):
#写个算法获取这些信息也没什么难度
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)
f_read = open(path,"rb")
f_write = open(newFilePath,"wb")
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]))
f_read.close()
f_write.close()
print("文件加密完毕")
time.sleep(3)
#解密(因为采取的异或解密,所以其实和加密算法一样)
def decrypt(path, password):
#写个算法获取这些信息也没什么难度
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)
f_read = open(path, "rb")
f_write = open(newFilePath, "wb")
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]))
f_read.close()
f_write.close()
print("文件解密完毕")
time.sleep(3)
main()
点赞0
评论