只需要我的代码建议,我现在有我的代码,以便它可以工作,只需将ASCII值加5,但我需要知道的是我怎样才能使这个工作只需移动5沿着字母表的地方,例如现在,如果我要输入Z,我会得到一个_,当我想得到一个D.
bLoopProgram = True
while bLoopProgram == True:
sEncryptOrDecrypt = input("Encrypt Or Decrypt : ").lower()
if sEncryptOrDecrypt == "encrypt":
while sEncryptOrDecrypt == "encrypt":
sPassNonEnc = input("Enter a password to encrypt : ")
aPass = []
for i in range(0, len(sPassNonEnc)):
nASCII_VALUE = ord(sPassNonEnc[i])
if nASCII_VALUE < 64:
nASCII_VALUE += 10
aPass.append(chr(nASCII_VALUE))
elif nASCII_VALUE > 63:
nASCII_VALUE -= 10
aPass.append(chr(nASCII_VALUE))
else:
print("\n * Unknown Character [ASCII INDEX]")
aPass.append()
print("".join(aPass))
sSaveToTxt = input("Would you like to save your encrypted password to a text document?\nThe Text document will be found next to this python file and called password_enc (Yes/no): ").lower()
if sSaveToTxt == "yes":
fFileRequestedOpen = open('password_enc.txt', 'w')
fFileRequestedOpen.write("".join(aPass))
fFileRequestedOpen.close()
print("Saved to text document have a nice day!")
break
elif sSaveToTxt == "no":
print("okay have a nice day!")
break
else:
print("\n*Invalid Option*\n")
if sEncryptOrDecrypt == "decrypt":
while sEncryptOrDecrypt == "decrypt":
sReadOrInput = input("Would you like to load password from text document if saved,\n or would you like to input a encypted password (read/enter): ").lower()
if sReadOrInput == "read":
fFileRequestedOpen = open('password_enc.txt', 'r')
sPassEnc = fFileRequestedOpen.read()
fFileRequestedOpen.close()
aPass = []
for i in range(0, len(sPassEnc)):
nASCII_VALUE = ord(sPassEnc[i])
if nASCII_VALUE < 64:
nASCII_VALUE -= 10
aPass.append(chr(nASCII_VALUE))
elif nASCII_VALUE > 63:
nASCII_VALUE += 10
aPass.append(chr(nASCII_VALUE))
else:
print("\n * Unknown Character [ASCII INDEX]")
print("".join(aPass))
break
if sReadOrInput == "enter":
sPassEnc = input("Enter a password to decrypt : ")
aPass = []
for i in range(0, len(sPassEnc)):
nASCII_VALUE = ord(sPassEnc[i])
if nASCII_VALUE < 64:
nASCII_VALUE -= 10
aPass.append(chr(nASCII_VALUE))
elif nASCII_VALUE > 63:
nASCII_VALUE += 10
aPass.append(chr(nASCII_VALUE))
else:
print("\n * Unknown Character [ASCII INDEX]")
print("The now unencrypted password is:")
print("".join(aPass))
break
elif sEncryptOrDecrypt == "quit":
quit()
else:
print("")
答案 0 :(得分:0)
您必须使用modulo(%)将值限制在65(A)和90(Z)
之内例如:
nASCII_VALUE = 90 # This is a Z
nASCII_VALUE += 5 # chr(95) = _
print(chr(((nASCII_VALUE - 64) % 26) + 64))
这将打印&#39; E&#39; (alphabeth的第5个字母)