HJ29_字符串加解密_模拟

思路:

根据加解密规则,使字符串加解密后输出。这是初始理解,编码起来较麻烦。查看高赞题解后,学到一种新思路关于加解密:最佳方法是通过通过设计加解密表,代码比较简单,通过列表index实现加解密。

这是我的代码:

 1 f_encrypt=input()
 2 f_decipher=input()
 3 o_encrypt=''
 4 o_decipher=''
 5 for i in f_encrypt:
 6     if i.isalpha() and i.isupper():
 7         if i=='Z':
 8             o_encrypt+='a'           
 9         else:
10             o_encrypt+=chr(ord(i)+1).lower()
11     elif i.isalpha() and i.islower():
12         if i=='z':
13             o_encrypt+='A'
14         else:
15             o_encrypt+=chr(ord(i)+1).upper() 
16     elif i.isdigit():
17         if int(i)==9:
18             o_encrypt+="0"
19         else:
20             o_encrypt+=str(int(i)+1)
21     else:
22         o_encrypt+=i
23 for i in f_decipher:
24     if i.isalpha() and i.isupper():
25         if i=='A':
26             o_decipher+='z'           
27         else:
28             o_decipher+=chr(ord(i)-1).lower()
29     elif i.isalpha() and i.islower():
30         if i=='a':
31             o_decipher+='Z'
32         else:
33             o_decipher+=chr(ord(i)-1).upper() 
34     elif i.isdigit():
35         if int(i)==0:
36             o_decipher+="9"
37         else:
38             o_decipher+=str(int(i)-1)
39     else:
40         o_decipher+=i
41 print(o_encrypt)
42 print(o_decipher)

 

这是查表解密的代码:

 1 o_encrypt=''
 2 o_decipher=''
 3 l1="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 4 l2="1234567890bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA"
 5 for i in input():
 6     if i in l1:
 7         o_encrypt+=l2[l1.index(i)]
 8     else:
 9         o_encrypt+=i
10 for i in input():
11     if i in l2:
12         o_decipher+=l1[l2.index(i)]
13     else:
14         o_decipher+=i
15 print(o_encrypt)
16 print(o_decipher)

 

posted @ 2023-03-25 13:28  Aneverforget  阅读(62)  评论(0编辑  收藏  举报