如何使python同时加密大写和小写?
基本上,我希望密码短语作为输出,大写字母被密码转换为大写字母,小写字母被密码转换为小写字母,但没有任何空格或符号被密码转换。它可以加密由所有大写字母组成的段落和由所有小写字母组成的段落,但不能混合使用这两种字母。这是我的如何使python同时加密大写和小写?,python,python-3.x,case-sensitive,encryption,Python,Python 3.x,Case Sensitive,Encryption,基本上,我希望密码短语作为输出,大写字母被密码转换为大写字母,小写字母被密码转换为小写字母,但没有任何空格或符号被密码转换。它可以加密由所有大写字母组成的段落和由所有小写字母组成的段落,但不能混合使用这两种字母。这是我的 def encrypt(phrase,move): encription=[] for character in phrase: a = ord(character) if a>64 and a<123: if a!=(91,96):
def encrypt(phrase,move):
encription=[]
for character in phrase:
a = ord(character)
if a>64 and a<123:
if a!=(91,96):
for case in phrase:
if case.islower():
alph=["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"]
dic={}
for i in range(0,len(alph)):
dic[alph[i]]=alph[(i+move)%len(alph)]
cipherphrase=""
for l in phrase:
if l in dic:
l=dic[l]
cipherphrase+=l
encription.append(chr(a if 97<a<=122 else 96+a%122))
return cipherphrase
else:
ALPH=["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"]
DIC={}
for I in range(0,len(ALPH)):
DIC[ALPH[I]]=ALPH[(I+move)%len(ALPH)]
cipherphrase=""
for L in phrase:
if L in DIC:
L=DIC[L]
cipherphrase+=L
encription.append(chr(a if 97<a<=122 else 96+a%122))
return cipherphrase
def加密(短语,移动):
encription=[]
对于短语中的字符:
a=ord(字符)
如果a>64且a
然后
然后
你想在这里发生什么?应该用小写字母代替小写字母,反之亦然吗?基本上是的,我会去编辑这个问题你的最终目标是什么?你想加密还是编码?@miraclixx我不知道加密和编码的区别,但我希望它能像凯撒密码一样工作。你想在这里发生什么?应该用小写字母代替小写字母,反之亦然吗?基本上是的,我会去编辑这个问题你的最终目标是什么?你想加密还是编码?@miraculixx我不知道加密和编码之间的区别,但我确实想让它像凯撒密码一样工作。使用它,不断得到这个>>rot_密码('Hi-ho-silver,awaayy!!',1)回溯(最近一次调用):文件“”,第1行,rot_密码('Hi-ho-silver,awaayy!!',1)文件“G:/Algorithms/Python Assignment 2/Q3.py”,rot_cipher tab=string.maketrans(alphabet1,alphabet2)中的第92行AttributeError:“module”对象没有属性“maketrans”
hmmm他们可能在python 3中已经删除了它。请给我一分钟时间,弄清楚他们将它改成了什么样子,就像在python3中一样。你可以使用str.maketrans
编辑的解决方案来使用它…尝试使用它,不断得到这个>>rot\u密码('Hi ho silver,awaayy!!',1)回溯(最后一次调用):文件“”,第1行,rot_密码('Hi ho silver,AWAAAYY!!',1)文件“G:/Algorithms/Python Assignment 2/Q3.py”,第92行,rot_密码选项卡=string.maketrans(alphabet1,alphabet2)AttributeError:“module”对象没有属性“maketrans”
hmmm他们在python 3中可能已经删除了它。请给我一分钟时间,弄清楚他们是如何将其更改为python 3中的样子的。您可以使用str.maketrans
编辑的解决方案来使用它……我也尝试过这个,我得到的只是这个>cr('Hi Ho silver,awaay!',1)回溯(最后一次调用):文件“”,第1行,cr('Hi Ho silver,AWAAAY!',1)文件“G:/Algorithms/Python Assignment 2/Q3.py”,第99行,cr trans=dict(zip(string.lowercase,string.lowercase[shift:+string.lowercase[:shift]))AttributeError:'module'对象没有属性'lowercase'
没关系,只需要ascii字母小写和ascii字母大写。我也试过了,我得到的只是这个>>cr('Hi-Ho-Silver,AWAAAY!',1)回溯(最近一次调用):文件“”,第1行,在cr('Hi-Ho-Silver,AWAAAY!',1)文件中G:/Algorithms/Python Assignment 2/Q3.py“,第99行,cr trans=dict(zip(string.lowercase,string.lowercase[shift:+string.lowercase[:shift]))AttributeError:“module”对象没有属性“lowercase”
没关系,只需要ascii_小写和ascii_大写。
import string
def rot_cipher(msg,amount):
alphabet1 = string.ascii_lowercase + string.ascii_uppercase
alphabet2 = string.ascii_lowercase[amount:] + string.ascii_lowercase[:amount]\
+ string.ascii_uppercase[amount:] + string.ascii_uppercase[:amount]
tab = str.maketrans(alphabet1,alphabet2)
return msg.translate(tab)
print(rot_cipher("hello world!",13))
import string
def caesar_cipher(msg, shift):
# create a character-translation table
trans = dict(zip(string.lowercase, string.lowercase[shift:] + string.lowercase[:shift]))
trans.update(zip(string.uppercase, string.uppercase[shift:] + string.uppercase[:shift]))
# apply it to the message string
return ''.join(trans.get(ch, ch) for ch in msg)
caesar_cipher('This is my 3rd test!', 2) # => 'Vjku ku oa 3tf vguv!'