记录顺带保存一次课堂作业,部分参考了现有的实现,结合需求进行了一波码,能用就行,人和代码能跑一个就好,替换密码暴力破解实在懒得搞了,或许以后会更?
import getopt
import sys
#接收参数,规定参数名
opts, args = getopt.getopt(sys.argv[1:], '-h-v-m:-a:-p:-c:-k:-A:', ['help', 'version', 'mode=', 'action=', 'plaintext=', 'ciphertext=', 'key=', 'admin='])
for opt_name, opt_value in opts:
if opt_name in ('-h', '--help'):
print("Help Info: \n"
"-m or --mode, value can use caesar or permute \n"
"-a or --action, value can use encode or decode or bruteAttack \n"
"-p or --plaintext \n"
"-c or --ciphertext \n"
"-k or --key \n"
"example-1:-m caesar -a encode -p helloworld -k 3 \n"
"example-2:--mode permute --action=decode --ciphertext=helloworld -k word \n")
sys.exit()
elif opt_name in ('-v', '--version'):
print("Version is 1.0, Update in 2022/4/26 ")
sys.exit()
elif opt_name in ('-m', '--mode'):
mode = opt_value
elif opt_name in ('-a', '--action'):
action = opt_value
elif opt_name in ('-p', '--plaintext'):
plaintext = opt_value
elif opt_name in ('-c', '--ciphertext'):
ciphertext = opt_value
elif opt_name in ('-k', '--key'):
key = opt_value
elif opt_name in ('-A', '--admin'):
if(opt_value == "admin"):
print("Developer:SZPT-AI")
print("Mode =", mode)
print("Action =", action)
#凯撒加密
def caesarEncode(plaintext, key):
encodeText = []
for word in plaintext:
encodeText.append(chr(ord('a') + ((ord(word) - ord('a')) + int(key)) % 26))
ciphertext = "".join(encodeText)
print(ciphertext)
#凯撒解密
def caesarDecode(ciphertext, key):
DecodeText = []
for word in ciphertext:
DecodeText.append(chr(ord('a') + ((ord(word) - ord('a')) - int(key)) % 26))
plaintext = "".join(DecodeText)
print(plaintext)
#凯撒暴力破解
def caesarBruteAttack(ciphertext):
#遍历所有偏移量
for key in range(1,26):
DecodeText = []
for word in ciphertext:
DecodeText.append(chr(ord('a') + ((ord(word) - ord('a')) - int(key)) % 26))
plaintext = "".join(DecodeText)
print(plaintext)
def keyIntFormat(key):
# 将key转化为数字
tmp_key1 = []
for keyword in key:
tmp_key1.append(ord(keyword) - 96)
# 建立临时列表进行排序
tmp_key2 = sorted(tmp_key1)
tmp_key = [] * len(key)
for num in range(len(tmp_key2)):
for keynum in range(len(tmp_key1)):
if (tmp_key1[num] == tmp_key2[keynum]):
tmp_key.append(str(keynum + 1))
key = "".join(tmp_key)
return key
#置换加密
def permuteEncode(plaintext, key):
keylen = len(key)
ciphertext = ""
key = keyIntFormat(key)
#根据key长度对明文进行分段
for part in range(0, len(plaintext), keylen):
tmp_ciphertext = [""] * keylen
#超长度内容处理
if part + keylen > len(plaintext):
tmp_plaintext = plaintext[part:]
#内容处理
else:
tmp_plaintext = plaintext[part:part + keylen]
for word in range(len(tmp_plaintext)):
tmp_ciphertext[int(key[word]) - 1] = tmp_plaintext[word]
ciphertext += "".join(tmp_ciphertext)
print(ciphertext)
#置换解密
def permuteDecode(ciphertext, key):
keylen = len(key)
plaintext = ""
key = keyIntFormat(key)
#根据key长度对密文进行分段
for part in range(0, len(ciphertext), keylen):
#构筑矩阵
tmp_plaintext = [""] * keylen
#超长度内容处理
if part + keylen >= len(ciphertext):
tmp_ciphertext = ciphertext[part:]
re_key = []
#根据key确认位置
for word in range(len(tmp_ciphertext)):
re_key.append(int(key[word]) - 1)
re_key.sort()
for word in range(len(tmp_ciphertext)):
tmp_plaintext[word] = tmp_ciphertext[re_key.index(int(key[word]) - 1)]
#内容处理
else:
#分段内容
tmp_ciphertext = ciphertext[part:part + keylen]
for word in range(len(tmp_ciphertext)):
tmp_plaintext[word] = tmp_ciphertext[int(key[word]) - 1]
plaintext += "".join(tmp_plaintext)
print(plaintext)
#置换暴力破解
def permuteBruteAttack(ciphertext):
#其实就是太麻烦了我不想写
#此处思路可以根据出现频率高的单词字母去匹配字典有可能的日常高频单词(就是随缘撞库)
print("Develop in future release")
if(mode == "caesar"): #凯撒加密
if(action == "encode"):
caesarEncode(plaintext, key)
elif(action == "decode"):
caesarDecode(ciphertext, key)
elif(action == "bruteAttack"):
caesarBruteAttack(ciphertext)
elif(mode == "permute"): #置换加密
if(action == "encode"):
permuteEncode(plaintext, key)
elif(action == "decode"):
permuteDecode(ciphertext, key)
elif(action == "bruteAttack"):
permuteBruteAttack(ciphertext)