python如何调用文件进行换位加密_文件加密---使用换位加密和解密方法加密文件...

#Transposition Cipher Encrypt/Decrypt File#http://inventwithpython.com/hacking (BSD Licensed)

#time模块和time.time()函数#返回一个浮点数,是1970年1月1日起到现在的秒数 called "Unix Epoch"#我们可以用两次调用time.time()的返回值来判断程序的运行时间 密码学是重视时间的

importtime, os, sys, transpositionEncrypt, transpositionDecryptdefmain():#inputFilename = 'D:\\PiaYie\\jczhang\\密码学\\py密码学编程教学代码\\frankenstein.txt'

inputFilename = 'frankenstein.txt'

#BE CAREFUL! If a file with the outputFilename name already exists,

#this program will overwrite that file.

outputFilename = 'frankenstein.encrypted.txt'myKey= 10myMode= 'encrypt' #set to 'encrypt' or 'decrypt'

#If the input file does not exist, then the program terminates early.

if notos.path.exists(inputFilename):print('The file %s does not exist. Quitting...' %(inputFilename))

sys.exit()#If the output file already exists, give the user a chance to quit.

#打开文件要当心 以'w'打开时,这个文件若已经存在会被删除再新建,即有一个内容清空的过程

#但是我们有对策,os.path.exists(filename) 返回True or False,来检查某个文件名是否存在,可加上路径检查

ifos.path.exists(outputFilename):print('This will overwrite the file %s. (C)ontinue or (Q)uit?' %(outputFilename))

response= input('>')#startswith()和endswit()返回True or False,字面意思

#'hello'.startswith('h') ---> True 'hello'.startswith('he') ---> True

if not response.lower().startswith('c'):

sys.exit()#Read in the message from the input file

#open()函数 第一个参数是文件的名字,同路径写名字即可,或者是绝对路径

#路径 windows:D:\\PiaYie\\jczhang\\密码学\\py密码学编程教学代码\\frankenstein.txt

#linux: /usr/piayie/frankenstein.txt

#open()函数返回一个“文件对象” 数据类型 的值 用这个值标记一个文件,用于读取写入和关闭

#第二个参数可选 默认'r'即读取打开 'w'---写入打开, 'a'---追加打开

#read()方法返回一个字符串包含文件里的所有内容,如果文件是多行的,则read()返回的这个字符串

#每一行里都有一个\n换行字符

#close()方法 这个文件使用完毕就该关闭释放资源了 当然,py程序终止时候会自动释放所有未释放的资源

#若希望重新读取文件,也要先行close() 再调用open()

fileObj=open(inputFilename)

content=fileObj.read()

fileObj.close()#字符串的title()方法

#lower()转化为小写 upper()转化为大写 title()字符串中的每个单词的首字母大写,其他字母小写

print('%sing...' %(myMode.title()))#Measure how long the encryption/decryption takes.

startTime=time.time()if myMode == 'encrypt':

translated=transpositionEncrypt.encryptMessage(myKey, content)elif myMode == 'decrypt':

translated=transpositionDecrypt.decryptMessage(myKey, content)

totalTime= round(time.time() - startTime, 2)print('%sion time: %s seconds' %(myMode.title(), totalTime))#open()函数返回的文件对象 有 write()方法。前提是以'w'或者'a'模式open()的文件对象

#fo = open('filename.txt', 'w') or fo = open('filename.txt', 'a')

#write()方法的参数是一个字符串,把这个字符串以相应的方法写入该文件

#Write out the translated message to the output file.

outputFileObj = open(outputFilename, 'w')

outputFileObj.write(translated)

outputFileObj.close()print('Done %sing %s (%s characters).' %(myMode, inputFilename, len(content)))print('%sed file is %s.' %(myMode.title(), outputFilename))#If transpositionCipherFile.py is run (instead of imported as a module)#call the main() function.

if __name__ == '__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值