手机键盘密码
-
加密对象: 字母
-
原理:
-
就如同密码名字一样,根据手机九宫格键盘来加密的,比如a在第二个格子的第一个位置,故对应密文为"21", 同理,“m"密文文为"61”,如下:
-
将密文的每个字符转为两个数字后,按顺序用空格隔开组成密文
-
-
特点:
- 密文是数字组成
- 每组数字第一个范围为2~9,第二个范围为1~4
-
代码
# write by 2021/7/23 # 手机键盘密码 import re DIC = ["", "", " abc", " def", " ghi", " jkl", " mno", " pqrs", " tuv", " wxyz"] def encrypt_p_keyboard(string): ciphertext = "" string = string.replace(" ", "") for i in string: for j in DIC: if i in j: ciphertext += str(DIC.index(j)) + str(j.index(i)) + " " break else: return -1 return ciphertext[:-1] def decrypt_p_keyboard(string): plaintext = "" string = string.replace(" ", "") test = re.findall("\d+", string) if not test or test[0] != string: return -1 ciphertext_lis = re.findall("\d{2}", string) try: for i in ciphertext_lis: plaintext += DIC[int(i[0])][int(i[1])] except: return -1 return plaintext if __name__ == '__main__': ciphertext_ = encrypt_p_keyboard("keyboard") plaintext_ = decrypt_p_keyboard(ciphertext_) print(f"{plaintext_}: {ciphertext_}")