【发布时间】:2014-04-30 23:07:29
【问题描述】:
如下制作一个密码学程序。(添加文件 i/o 到它)
- 您应该(至少)有两个功能:加密和解密。
- 增强您的主要例程以:
一个。欢迎用户
湾。询问用户是否要加密或解密
C。询问用户加密消息要写入/读取的文件
d。如果加密:
要求消息加密
求钥匙
调用 encrypt 创建密文
打开文件
将密码测试写入文件
关闭文件
通知用户密文(显示)已存储到文件中(确保提及文件名!)
e.如果解密:要求密钥打开文件从文件中读取密码测试关闭文件调用解密以解密密文为用户显示消息
F。询问用户是否愿意执行另一项任务
G。如果是,请转到 (b)
H。否则退出
以下是我需要在程序中添加的代码:
#open file to write record(s). It will create the file if new!
f = open("temp.txt", "w")
f.write("Hello!\n")
f.close
#open file to read this time
f = open("temp.txt", "r")
line = f.readline()
print(line)
f.close
#try binary read...
f = open("temp.txt", "rb")
line = f.readlines()
print(line)
f.close
这是我已经拥有的(除了文件的输入/输出函数之外的所有内容):
# Caesar Cipher
MAX_KEY_SIZE = 26
def getMode():
while True:
mode = input("Do you wish to encrypt or decrypt a message?").lower()
if mode in "encrypt e decrypt d". split():
return mode
else:
## print("Enter either "encrypt" or "e" or "decrypt" or "d".')
def getMessage():
return input("Enter you message")
def getKey():
key = 0
while True
key = int(input("Enter a key number (1-26)"))
if (key >=1 and key <= MAX_KEY_SIZE):
return key
def getTranslatatedMessage(mode, messafe, key):
if mode[0] == "d":
key = -key
translated = ""
for symbol in message:
if symbol.isalpha():
num = ord(symbol)
num += key
if symbol.isupport():
if num > ord("Z"):
num -= 26
elif num < ord("A"):
num += 26
elif symbol.islower():
if num < ord("z"):
num += 26
elif num < ord("a"):
num += 26
translated += chr(num)
else:
translated += symbol
return translated
mode = getMode()
message = getMessage()
key = getKey()
我的问题是我在哪里将上述代码添加到加密/解密程序中?
【问题讨论】:
-
那么你的问题是什么?这听起来像是“你能完成我的家庭作业吗?”。您应提供具体问题。
-
我制作了加密/解密程序,它运行良好,但现在我必须让它向用户询问文件,我知道这样做的代码,我只是不知道放在哪里他们。
-
您应将此具体问题写到问题的末尾。它可能像“如何要求用户输入指定文件名”或类似的东西(你比我更清楚)。
标签: python encryption file-io cryptography