文本解密程序没有给出正确的结果
text crypter program dosent give right results
当您加密文本时,它可以正常工作,但是当您尝试在其中解密时,会出现奇怪的结果。我的代码正在运行,但突然崩溃了。问题是我不知道加密部分或解密部分是否损坏我将不胜感激。
abc = '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'
code = '! @ # $ % ^ & * ( ) _ + § ± , . / < > ? | ` ~ [ ] {'
code2 = '{ ] [ ~ ` | ? > < / . , ± § + _ ) ( * & ^ % $ # @ !'
while True:
print()
print('--------------')
print('[1] text -> code\n[2] code -> text')
print('--------------')
inp = input('==> ')
# encrypting chosen
if inp == '1':
print('--------------')
print('enter the text')
print('--------------')
inp = input('==> ')
print('-----code-----')
# for every character in the text given this runs
for i in inp:
# gets a random number between 1 and 2 because there is two diffirent alpabets
# every letter will be cyrpted depending on a random alpabet
rand = random.randint(1, 2)
# gets the index for the character in abc
ind = abc.index(i)
if rand == 1:
# prints the index in code2(1th alpabet) and adds '∑' so program know it belongs to 1th alpabet
print(code2[ind], '∑', sep='', end='')
elif rand == 2:
# prints the index in code(@th alpabet) and adds '¥' so program know it belongs to 1th alpabet
print(code[ind], '¥', sep='', end='')
# decrypting chosen
elif inp == '2':
print()
print('--------------')
print('enter the code')
print('--------------')
inp = input('==> ')
print('-----text-----')
# for every character in the text given this runs
for i in inp:
# checks the character next to the actual character if it is '∑'
# to know which alpabet it belongs to
if inp[inp.index(i)+1] == '∑':
# gets the index and print the same index in abc
ind = code2.index(i)
print(abc[ind], end='')
elif inp[inp.index(i)+1] == '¥':
ind = code.index(i)
print(abc[ind], end='')
else:
pass
这就是你想要的。请注意,我在编码字符之前添加了您的字母表移位。这样,当遍历编码文本时,我可以设置正确的解码字母表。
import random
abc ='abcdefghijklmnopqrstuvwxyz'
code ='!@#$%^&*()_+§±,./<>?|`~[]{'
code2='{][~`|?></.,±§+_)(*&^%$#@!'
while True:
print('\n--------------')
print('[1] text -> code\n[2] code -> text')
print('--------------')
inp = input('==> ')
# encrypting chosen
if inp == '1':
print('--------------')
print('enter the text')
print('--------------')
inp = input('==> ')
print('-----code-----')
# for every character in the text given this runs
for i in inp:
# gets a random number between 1 and 2 because there is two diffirent alpabets
# every letter will be cyrpted depending on a random alpabet
rand = random.randint(1, 2)
# gets the index for the character in abc
ind = abc.index(i)
if rand == 1:
# prints the index in code2(1th alpabet) and adds '∑' so program know it belongs to 1th alpabet
print('∑', code2[ind], sep='', end='')
elif rand == 2:
# prints the index in code(@th alpabet) and adds '¥' so program know it belongs to 1th alpabet
print('¥', code[ind], sep='', end='')
# decrypting chosen
elif inp == '2':
print()
print('--------------')
print('enter the code')
print('--------------')
inp = input('==> ')
print('-----text-----')
# for every character in the text given this runs
which = code
for i in inp:
if i == '∑':
which = code2
elif i == '¥':
which = code
else:
ind = which.index(i)
print(abc[ind], end='')
更好的设计应该有一个 encode
函数和一个 return 字符串的 decode
函数,从而允许调用者决定如何处理这些字符串。
当您加密文本时,它可以正常工作,但是当您尝试在其中解密时,会出现奇怪的结果。我的代码正在运行,但突然崩溃了。问题是我不知道加密部分或解密部分是否损坏我将不胜感激。
abc = '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'
code = '! @ # $ % ^ & * ( ) _ + § ± , . / < > ? | ` ~ [ ] {'
code2 = '{ ] [ ~ ` | ? > < / . , ± § + _ ) ( * & ^ % $ # @ !'
while True:
print()
print('--------------')
print('[1] text -> code\n[2] code -> text')
print('--------------')
inp = input('==> ')
# encrypting chosen
if inp == '1':
print('--------------')
print('enter the text')
print('--------------')
inp = input('==> ')
print('-----code-----')
# for every character in the text given this runs
for i in inp:
# gets a random number between 1 and 2 because there is two diffirent alpabets
# every letter will be cyrpted depending on a random alpabet
rand = random.randint(1, 2)
# gets the index for the character in abc
ind = abc.index(i)
if rand == 1:
# prints the index in code2(1th alpabet) and adds '∑' so program know it belongs to 1th alpabet
print(code2[ind], '∑', sep='', end='')
elif rand == 2:
# prints the index in code(@th alpabet) and adds '¥' so program know it belongs to 1th alpabet
print(code[ind], '¥', sep='', end='')
# decrypting chosen
elif inp == '2':
print()
print('--------------')
print('enter the code')
print('--------------')
inp = input('==> ')
print('-----text-----')
# for every character in the text given this runs
for i in inp:
# checks the character next to the actual character if it is '∑'
# to know which alpabet it belongs to
if inp[inp.index(i)+1] == '∑':
# gets the index and print the same index in abc
ind = code2.index(i)
print(abc[ind], end='')
elif inp[inp.index(i)+1] == '¥':
ind = code.index(i)
print(abc[ind], end='')
else:
pass
这就是你想要的。请注意,我在编码字符之前添加了您的字母表移位。这样,当遍历编码文本时,我可以设置正确的解码字母表。
import random
abc ='abcdefghijklmnopqrstuvwxyz'
code ='!@#$%^&*()_+§±,./<>?|`~[]{'
code2='{][~`|?></.,±§+_)(*&^%$#@!'
while True:
print('\n--------------')
print('[1] text -> code\n[2] code -> text')
print('--------------')
inp = input('==> ')
# encrypting chosen
if inp == '1':
print('--------------')
print('enter the text')
print('--------------')
inp = input('==> ')
print('-----code-----')
# for every character in the text given this runs
for i in inp:
# gets a random number between 1 and 2 because there is two diffirent alpabets
# every letter will be cyrpted depending on a random alpabet
rand = random.randint(1, 2)
# gets the index for the character in abc
ind = abc.index(i)
if rand == 1:
# prints the index in code2(1th alpabet) and adds '∑' so program know it belongs to 1th alpabet
print('∑', code2[ind], sep='', end='')
elif rand == 2:
# prints the index in code(@th alpabet) and adds '¥' so program know it belongs to 1th alpabet
print('¥', code[ind], sep='', end='')
# decrypting chosen
elif inp == '2':
print()
print('--------------')
print('enter the code')
print('--------------')
inp = input('==> ')
print('-----text-----')
# for every character in the text given this runs
which = code
for i in inp:
if i == '∑':
which = code2
elif i == '¥':
which = code
else:
ind = which.index(i)
print(abc[ind], end='')
更好的设计应该有一个 encode
函数和一个 return 字符串的 decode
函数,从而允许调用者决定如何处理这些字符串。