密码学---常见的其他密码
✨费纳姆密码:加解密都需要密钥,以二进制形式表示的密码。(密钥多是一次性的,称位一次性密码本)
加密过程:
char_num = {'A': '1000001', 'B': '1000010', 'C': '1000011', 'D': '1000100',
'E': '1000101', 'F': '1000110', 'G': '1000111', 'H': '1001000',
'I': '1001001', 'J': '1001010', 'K': '1001011', 'L': '1001100',
'M': '1001101', 'N': '1001110', 'O': '1001111', 'P': '1010000',
'Q': '1010001', 'R': '1010010', 'S': '1010011', 'T': '1010100',
'U': '1010101', 'V': '1010110', 'W': '1010111', 'X': '1011000',
'Y': '1011001', 'Z': '1011010'}
num_char = {'1000001': 'A', '1000010': 'B', '1000011': 'C', '1000100': 'D',
'1000101': 'E', '1000110': 'F', '1000111': 'G', '1001000': 'H',
'1001001': 'I', '1001010': 'J', '1001011': 'K', '1001100': 'L',
'1001101': 'M', '1001110': 'N', '1001111': 'O', '1010000': 'P',
'1010001': 'Q', '1010010': 'R', '1010011': 'S', '1010100': 'T',
'1010101': 'U', '1010110': 'V', '1010111': 'W', '1011000': 'X',
'1011001': 'Y', '1011010': 'Z', }
# 转换
def txt_num(txt):
txt = txt.upper()
result = ""
for i in txt:
result = result + char_num[i]
return result
# 最终数字转换为字母
def num_list(txt):
num = 0
s = []
while True:
s.append(txt[num:num + 7])
num += 7
if num > len(txt) - 7:
break
return s
# 加密 文本内容
def encipher(txt, k):
result = ''
if txt.isalpha() and k.isalpha():
txt = txt_num(txt)
k = txt_num(k)
j = len(k)
for i in range(0, len(txt)):
if txt[i] == k[i % j]:
result += '0'
else:
result += '1'
else:
result = "明文与密钥应为纯字母"
return result
# 解密 文本为数字格式
def decipher(txt, k):
result = ''
if k.isalpha():
k = txt_num(k)
j = len(k)
for i in range(0, len(txt)):
if txt[i] == k[i % j]:
result += '0'
else:
result += '1'
result = num_list(result)
result_char = ""
for i in result:
try:
result_char = result_char + num_char[i]
except KeyError:
result_char = result_char + '?'
result = result_char
else:
result = "密钥应为纯字母"
return result
if __name__ == '__main__':
# 加解密,密文text,明文text_2
text = ''
# 读文本文件
key = "polar"
text_2 = "polarCTF"
print(encipher(text_2, key))
# print(decipher(text, key))
解密过程:
char_num = {'A': '1000001', 'B': '1000010', 'C': '1000011', 'D': '1000100',
'E': '1000101', 'F': '1000110', 'G': '1000111', 'H': '1001000',
'I': '1001001', 'J': '1001010', 'K': '1001011', 'L': '1001100',
'M': '1001101', 'N': '1001110', 'O': '1001111', 'P': '1010000',
'Q': '1010001', 'R': '1010010', 'S': '1010011', 'T': '1010100',
'U': '1010101', 'V': '1010110', 'W': '1010111', 'X': '1011000',
'Y': '1011001', 'Z': '1011010'}
num_char = {'1000001': 'A', '1000010': 'B', '1000011': 'C', '1000100': 'D',
'1000101': 'E', '1000110': 'F', '1000111': 'G', '1001000': 'H',
'1001001': 'I', '1001010': 'J', '1001011': 'K', '1001100': 'L',
'1001101': 'M', '1001110': 'N', '1001111': 'O', '1010000': 'P',
'1010001': 'Q', '1010010': 'R', '1010011': 'S', '1010100': 'T',
'1010101': 'U', '1010110': 'V', '1010111': 'W', '1011000': 'X',
'1011001': 'Y', '1011010': 'Z', }
# 转换
def txt_num(txt):
txt = txt.upper()
result = ""
for i in txt:
result = result + char_num[i]
return result
# 最终数字转换为字母
def num_list(txt):
num = 0
s = []
while True:
s.append(txt[num:num + 7])
num += 7
if num > len(txt) - 7:
break
return s
# 加密 文本内容
def encipher(txt, k):
result = ''
if txt.isalpha() and k.isalpha():
txt = txt_num(txt)
k = txt_num(k)
j = len(k)
for i in range(0, len(txt)):
if txt[i] == k[i % j]:
result += '0'
else:
result += '1'
else:
result = "明文与密钥应为纯字母"
return result
# 解密 文本为数字格式
def decipher(txt, k):
result = ''
if k.isalpha():
k = txt_num(k)
j = len(k)
for i in range(0, len(txt)):
if txt[i] == k[i % j]:
result += '0'
else:
result += '1'
result = num_list(result)
result_char = ""
for i in result:
try:
result_char = result_char + num_char[i]
except KeyError:
result_char = result_char + '?'
result = result_char
else:
result = "密钥应为纯字母"
return result
if __name__ == '__main__':
# 加解密,密文text,明文text_2
text = '00000000000000000000000000000000000001001100110110001010'
# 读文本文件
key = "polar"
text_2 = "polarCTF"
# print(encipher(text_2, key))
print(decipher(text, key))
✨杰弗逊转轮加密:需要密钥并用很多行乱序英文字母表示密文的密码。首先要将密文按密钥顺序进行调整,再根据密文顺序整每行字符串即可得到明文。
方法1:
除了每行第一个字母组成的,依次往后向下寻找有意义的字符串,直到找到为止!!!
方法2:
import re
PasswordList = '''
1: < ZWAXJGDLUBVIQHKYPNTCRMOSFE <
2: < KPBELNACZDTRXMJQOYHGVSFUWI <
3: < BDMAIZVRNSJUWFHTEQGYXPLOCK <
4: < RPLNDVHGFCUKTEBSXQYIZMJWAO <
5: < IHFRLABEUOTSGJVDKCPMNZQWXY <
6: < AMKGHIWPNYCJBFZDRUSLOQXVET <
7: < GWTHSPYBXIZULVKMRAFDCEONJQ <
8: < NOZUTWDCVRJLXKISEFAPMYGHBQ <
9: < XPLTDSRFHENYVUBMCQWAOIKZGJ <
10: < UDNAJFBOWTGVRSCZQKELMXYIHP <
11: < MNBVCXZQWERTPOIUYALSKDJFHG <
12: < LVNCMXZPQOWEIURYTASBKJDFHG <
13: < JZQAWSXCDERFVBGTYHNUMKILOP <
'''
ciphertext = "UOFJLBLSXJPJY" # 存放密文
content = re.findall(r'<(.*?)<', PasswordList, re.S) # 匹配所有的密码表字符串
key = [2, 3, 7, 5, 13, 12, 9, 1, 8, 10, 4, 11, 6] # 密钥
list = [] # 定义空列表list,和空串ans
ans = ""
for i in range(13):
index = content[key[i]-1].index(ciphertext[i])
list.append((index))
for i in range(0, 26):
flag = ""
for j in range(13):
flag += content[key[j]-1][(list[j]+i) % 26]
print(flag)
从出来的结果找出有意义的那串字符即为明文!!!
✨常见图形密码—猪圈密码
变种形式1:
变种形式2:
✨圣堂武士密码:
✨旗语密码:
✨盲文:
✨精灵语密码:
✨跳舞的人密码:
✨夏多密码:(下面的四个是方向指示符号,向上,右,下,左旋转,碰到表示将密文整体进行该方向的旋转)
✨银河密码: