第一行输入一个长度为
的字符串
,代表给定的明文字符串;
第二行输入一个长度为
的字符串
,代表给定的密文字符串。
除此之外,保证字符串
和
中仅包含英文字母和数字。
第一行输出一个字符串,代表加密后的
。
第二行输出一个字符串,代表解密后的
。
abcdefg1 0BCDEFGH
BCDEFGH2 9abcdefg
alpha_upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" alpha_lower = alpha_upper.lower() number = "0123456789" jiami_origin = input() jiemi_origin = input() jiami = "" jiemi = "" for i in range(len(jiami_origin)): if jiami_origin[i].isupper(): index = alpha_upper.index(jiami_origin[i]) jiami += alpha_lower[0 if index == 25 else index + 1] if jiami_origin[i].islower(): index = alpha_lower.index(jiami_origin[i]) jiami += alpha_upper[0 if index == 25 else index + 1] if jiami_origin[i].isdigit(): index = int(jiami_origin[i]) + 1 jiami += str(0) if index == 10 else str(index) # print(jiami) for i in range(len(jiemi_origin)): if jiemi_origin[i].isupper(): index = alpha_upper[::-1].index(jiemi_origin[i]) jiemi += alpha_lower[::-1][0 if index == 25 else index + 1] if jiemi_origin[i].islower(): index = alpha_lower[::-1].index(jiemi_origin[i]) jiemi += alpha_upper[::-1][0 if index == 25 else index + 1] if jiemi_origin[i].isdigit(): index = number.index(jiemi_origin[i]) jiemi += number[9] if index == 0 else str(index-1) print(jiami) print(jiemi)
while True: try: def coded(string): new_str='' for x in string: if x.isdigit(): x=str((int(x)+1)%10) new_str+=x elif x.isupper(): if x!="Z": new_str+=chr(ord(x)+1).lower() else: new_str+="a" elif x.islower(): if x!="z": new_str+=chr(ord(x)+1).upper() else: new_str+="A" else: new_str+=x return new_str def uncoded(string): new_str='' for x in string: if x.isdigit(): x=str((int(x)+9)%10) new_str+=x elif x.isupper(): if x!="A": new_str+=chr(ord(x)-1).lower() else: new_str+="z" elif x.islower(): if x!="a": new_str+=chr(ord(x)-1).upper() else: new_str+="Z" else: new_str+=x return new_str print(coded(input())) print(uncoded(input())) except: break
class CryptoMachine: def __init__(self) -> None: self.plain_letters = "abcdefghijklmnopqrstuvwxyz" self.ciphr_letters = "bcdefghijklmnopqrstuvwxyza" self.revrs_letters = "zabcdefghijklmnopqrstuvwxy" self.plain_numbers = "0123456789" self.ciphr_numbers = "1234567890" self.revrs_numbers = "9012345678" self.ord_a = ord("a") self.ord_A = ord("A") def encode(self, char: str): if char.isnumeric(): return self.ciphr_numbers[int(char)] elif char.islower(): index = ord(char) - self.ord_a return self.ciphr_letters[index].upper() elif char.isupper(): index = ord(char) - self.ord_A return self.ciphr_letters[index] else: raise NotImplementedError def decode(self, char: str): if char.isnumeric(): return self.revrs_numbers[int(char)] elif char.islower(): index = ord(char) - self.ord_a return self.revrs_letters[index].upper() elif char.isupper(): index = ord(char) - self.ord_A return self.revrs_letters[index] else: raise NotImplementedError def encode_string(self, s: str): return "".join(map(self.encode, s)) def decode_string(self, s: str): return "".join(map(self.decode, s)) mc = CryptoMachine() print(mc.encode_string(input())) print(mc.decode_string(input()))
def solve(str1): res = [] for i in str1: if ord("a") <= ord(i) < ord("z"): res.append(chr(ord(i.upper())+1)) elif ord("A") <= ord(i) < ord("Z"): res.append(chr(ord(i.lower())+1)) elif i == "z": res.append("A") elif i == "Z": res.append("a") elif ord("0") <= ord(i) <= ord("8"): res.append(chr(ord(i)+1)) elif i =="9": res.append("0") else: res.append(i) return "".join(res) def solve2(str2): res = [] for j in str2: if ord("b") <= ord(j) <= ord("z"): res.append(chr(ord(j.upper())-1)) elif ord("B") <= ord(j) <= ord("Z"): res.append(chr(ord(j.lower())-1)) elif j == "a": res.append("Z") elif j == "A": res.append("z") elif ord("1") <= ord(j) <= ord("9"): res.append(chr(ord(j)-1)) elif j == "0": res.append("9") else: res.append(j) return "".join(res) str1 = input() str2 = input() print(solve(str1)) print(solve2(str2))
def jia_jie_mi(s, k): upper_word = [chr(ord("A") + i) for i in range(26)] lower_word = [chr(ord("a") + i) for i in range(26)] nums = [str(i) for i in range(10)] ss = [] def jia_mi(s): for str1 in s: if str1 in upper_word + lower_word: if str1 == "Z": ss.append("a") elif str1 == "z": ss.append("A") elif str1 in upper_word: j = upper_word.index(str1) ss.append(lower_word[j + 1]) elif str1 in lower_word: j = lower_word.index(str1) ss.append(upper_word[j + 1]) elif str1 in nums: if str1 == "9": ss.append("0") else: ss.append(str(int(str1) + 1)) s_jia_mi = "".join(ss) print(s_jia_mi) def jie_mi(s): for str2 in s: if str2 in upper_word + lower_word: if str2 == "A": ss.append("z") elif str2 == "A": ss.append("z") elif str2 in upper_word: j = upper_word.index(str2) ss.append(lower_word[j - 1]) elif str2 in lower_word: j = lower_word.index(str2) ss.append(upper_word[j - 1]) elif str2 in nums: if str2 == "0": ss.append("9") else: ss.append(str(int(str2) - 1)) s_jie_mi = "".join(ss) print(s_jie_mi) if k == 1: jia_mi(s) if k == 2: jie_mi(s) s1 = input() s2 = input() jia_jie_mi(s1, 1) jia_jie_mi(s2, 2)
def en_secret(secret: str): list_out = [] for s in secret: if s.isdigit(): if s == '9': list_out.append('0') else: list_out.append(str(int(s) + 1)) else: if s.upper() == 'Z': list_out.append('a' if s.isupper() else 'A') else: next_lower = chr(ord(s.lower()) + 1) list_out.append(next_lower if s.isupper() else next_lower.upper()) return ''.join(list_out) def de_secret(secret: str): list_out = [] for s in secret: if s.isdigit(): if s == '0': list_out.append('9') else: list_out.append(str(int(s) - 1)) else: if s.upper() == 'A': list_out.append('z' if s.isupper() else 'Z') else: before_lower = chr(ord(s.lower()) - 1) list_out.append(before_lower if s.isupper() else before_lower.upper()) return ''.join(list_out) print(en_secret(input())) print(de_secret(input()))
def Encryption(item): new_str = "" for i in item: if str(i).isalpha(): if i == "z": new_str = new_str + "A" elif i == "Z": new_str = new_str + "a" else: new_str = new_str + chr(ord(i) + 1).swapcase() elif i.isnumeric(): if i == "9": new_str = new_str + "0" else: new_str = new_str + str(int(i) + 1) else: pass return new_str def Decrypt(item): new_str = "" for i in item: if str(i).isalpha(): if i == "a": new_str = new_str + "Z" elif i == "A": new_str = new_str + "z" else: new_str = new_str + chr(ord(i) - 1).swapcase() elif i.isnumeric(): if i == "0": new_str = new_str + "9" else: new_str = new_str + str(int(i) - 1) else: pass return new_str if __name__ == "__main__": s1 = input() s2 = input() print(Encryption(s1)) print(Decrypt(s2))
alp_u = "ABCDEFGHIJKLMNOPQRSTUVWXYZA" alp_l = "abcdefghijklmnopqrstuvwxyza" num_l = "01234567890" # 编码函数 def code(s1: str): s1_d = "" for i in s1: if i in alp_u: s1_d += alp_l[alp_u.index(i) + 1] elif i in alp_l: s1_d += alp_u[alp_l.index(i) + 1] elif i in num_l: s1_d += num_l[num_l.index(i) + 1] return s1_d # 解码函数 def decode(s2: str): s2_c = "" for i in s2: if i in alp_u: if i == "A": s2_c += "z" else: s2_c += alp_l[alp_u.index(i) - 1] if i in alp_l: if i == "a": s2_c += "Z" else: s2_c += alp_u[alp_l.index(i) - 1] if i in num_l: if i == '0': s2_c += '9' else: s2_c += num_l[num_l.index(i) - 1] return s2_c print(code(input())) print(decode(input()))
比不上大佬们的映射做法,用了ascii码 def encrypt(pw): pw_new = '' for i in pw: a = ord(i) if a in range(48,57): pw_new += chr(a+1) elif a == 57: pw_new += chr(48) elif a in range(65,90): pw_new += chr(a+1).lower() elif a == 90: pw_new += chr(65).lower() elif a in range(97,122): pw_new += chr(a+1).upper() else: pw_new += chr(97).upper() return pw_new def decrypt(pw): pw_new = '' for i in pw: a = ord(i) if a in range(49,58): pw_new += chr(a-1) elif a == 48: pw_new += chr(57) elif a in range(66,91): pw_new += chr(a-1).lower() elif a == 65: pw_new += chr(90).lower() elif a in range(98,123): pw_new += chr(a-1).upper() else: pw_new += chr(122).upper() return pw_new while True: try: print(encrypt(input())) print(decrypt(input())) except: break
from string import ascii_lowercase,ascii_uppercase,digits def encrypt(s): res = '' for c in s: if c in ascii_lowercase: index = ascii_lowercase.index(c) res += ascii_uppercase[(index+1)%26] elif c in ascii_uppercase: index = ascii_uppercase.index(c) res += ascii_lowercase[(index+1)%26] else: res += str((int(c)+1)%10) return res def decrypt(s): res = '' for c in s: if c in ascii_lowercase: index = ascii_lowercase.index(c) res += ascii_uppercase[(index-1)%26] elif c in ascii_uppercase: index = ascii_uppercase.index(c) res += ascii_lowercase[(index-1)%26] else: res += str((int(c)-1)%10) return res s1=input() s2=input() print(encrypt(s1)) print(decrypt(s2))
import sys import string def encrypt(s): encrypted = "" for char in s: if char.isalpha(): if char.islower(): encrypted += chr((ord(char) - ord("a") + 1) % 26 + ord("A")) else: encrypted += chr((ord(char) - ord("A") + 1) % 26 + ord("a")) elif char.isdigit(): encrypted += str((int(char) + 1) % 10) else: encrypted += char return encrypted def decrypt(s): decrypted = "" for char in s: if char.isalpha(): if char.islower(): decrypted += chr((ord(char) - ord("a") - 1) % 26 + ord("A")) else: decrypted += chr((ord(char) - ord("A") - 1) % 26 + ord("a")) elif char.isdigit(): decrypted += str((int(char) - 1) % 10) else: decrypted += char return decrypted # 输入要加密的密码 password = input() # 加密密码 encrypted_password = encrypt(password) print(encrypted_password) # 输入加密过的密码 encrypted_input = input() # 解密密码 decrypted_password = decrypt(encrypted_input) print(decrypted_password)
对于输入字符串s中的每个字符char,我们按照以下规则进行加密:
对于输入字符串s中的每个字符char,我们按照以下规则进行解密:
最后,将解密后的字符拼接到decrypted字符串中,并返回最终的解密结果。