这是一个旧的帖子,但这对我很有用。 对它进行加密:
encryption_library = {'A':'!','B':'@','C':'#','D':'$','E':'%','F':'^','G':'&','H':'*','I':'(',
'J':')','K':'-','L':'_','M':'+','N':'=','O':'`','P':'~','Q':'{','R':'[',
'S':'}','T':']','U':':','V':';','W':'"','X':'<','Y':'>','Z':'0','a':'1',
'b':'2','c':'3','d':'4','e':'5','f':'6','g':'7','h':'8','i':'9','j':'a',
'k':'b','l':'c','m':'d','n':'e','o':'f','p':'g','q':'h','r':'i','s':'j',
't':'k','u':'l','v':'m','w':'n','x':'o','y':'p','z':'q'}
orig_file = open('Plain_Text_File.txt','r')
file_read = orig_file.read()
orig_file.close()
encrypt_file = open('ENCRYPTED_Plain_Text_File.txt','w')
for ch in file_read:
if ch in encryption_library:
encrypt_file.write(encryption_library[ch])
else:
encrypt_file.write(ch)
encrypt_file.close()
encrypt_file = open('Plain_Text_File.txt','r')
file_read = encrypt_file.read()
encrypt_file.close()
codes_items = encryption_library.items()
for ch in file_read:
if not ch in encryption_library.values() or ch == '.' or ch == ',' or ch == '!':
print(ch)
else:
for k,v in codes_items:
if ch == v and ch != '.':
print(k,end='')
对其进行解密:
decryption_library = {'!':'A','@':'B','#':'C','$':'D','%':'E','^':'F','&':'G','*':'H','(':'I',
')':'J','-':'K','_':'L','+':'M','=':'N','`':'O','~':'P','{':'Q','[':'R',
'}':'S',']':'T',':':'U',';':'V','"':'W','<':'X','>':'Y','0':'Z','1':'a',
'2':'b','3':'c','4':'d','5':'e','6':'f','7':'g','8':'h','9':'i','a':'j',
'b':'k','c':'l','d':'m','e':'n','f':'o','g':'p','h':'q','i':'r','j':'s',
'k':'t','l':'u','m':'v','n':'w','o':'x','p':'y','q':'z'}
orig_file = open('ENCRYPTED_Plain_Text_File.txt','r')
file_read = orig_file.read()
orig_file.close()
encrypt_file = open('DECRYPTED_Plain_Text_File.txt','w')
for ch in file_read:
if ch in decryption_library:
encrypt_file.write(decryption_library[ch])
else:
encrypt_file.write(ch)
encrypt_file.close()
encrypt_file = open('ENCRYPTED_Plain_Text_File.txt','r')
file_read = encrypt_file.read()
encrypt_file.close()
codes_items = decryption_library.items()
for ch in file_read:
if not ch in decryption_library.values() or ch == '.' or ch == ',' or ch == '!':
print(ch)
else:
for k,v in codes_items:
if ch == v and ch != '.':
print(k,end='')
你知道什么是伪代码意味着什么? –
是的,我以前写过伪代码,但这项任务令我困惑。 –
http://stackoverflow.com/help/on-topic请参阅第三点。如果不付出努力,我们将不会帮助做作业。尝试作业;使用Python文档进行字典和文件I/O;当你有一个最小的,可证实的例子,你卡住的地方并编辑这个问题时回来。这不是因为我们认为你是“mooching”,或者是因为任何一方都懒惰 - 这是为了促进更高质量的问答环节,这对未来的访问者有意义。 – Hypaethral