程序在解密方法上输出暴力破解结果失败


def asciiShift(character, aShift):
asciiNum = ord(character) + (int(aShift))%26
return asciiNum
def stringShift(string, shift):
k=0
newstring = ''
string_len = len(string)
while k<string_len:
newstring = newstring + string[int(((int(shift)%int(string_len))+k))%int(string_len)]
k=k+1
if k==string_len:
break
return newstring

def encrypt(message, aShift, sShift):
i=0
length = len(message)
asciiChars = []
while i<length:
asciiChars.append(asciiShift(message[i].upper(), aShift))
i=i+1
if i==length:
break
string = ''
j=0
while j<length:
string = string + str(chr(asciiChars[j]))
j=j+1
shifted_string = stringShift(string, sShift)
print(shifted_string)
def decrypt(message):
x=0
mlength = len(message)
mstring = ''
while x<mlength:
d=0
while d<mlength:
mstring = mstring + message[int(x+d)%(mlength)]
d=d+1
e=0
mstring = mstring.upper()
mlist = []
while e<mlength:
mlist.append(ord(mstring[e]))
e=e+1
f=0
while f<26:
g=0
newstring = ''
while g<mlength:
newstring = newstring + chr(mlist[g] + f%26)
g=g+1
print(newstring)
f=f+1
x=x+1
if x==mlength:
break
# todo list
# shift string around (use.upper())
# convert shifted string to ascii number list
# add numbers from 0-25 to the ascii number list
# print out the results


typeConfirm = input("Encrypt or Decrypt? (E/D): ")
if typeConfirm[0].upper() == 'D':
decryptBool = True
elif typeConfirm[0].upper()== 'E':
decryptBool = False
else:
print("You probably said an incorrect choice or read the question incorrectly. Exiting program for now...")
exit()
if decryptBool == True:
decrypt_msg = input("What is the encoded message?: ")
decrypt(decrypt_msg)
else:
message = input("What is your message?: ")
aShift = input("ASCII shift?: ")
sShift = input("String shift?: ")

a=0
b=len(message)
while a<b:
if message[a] == '~':
print("Sorry, the encryptor doesn't support any special characters (other than the ones on a normal keyboard, or these characters that are on a keyboard: '~', '{', and '}' only these 3 characters in the quotation marks don't work. Sorry for the inconvenience!")
exit()
elif message[a] == '{':
print("Sorry, the encryptor doesn't support any special characters (other than the ones on a normal keyboard, or these characters that are on a keyboard: '~', '{', and '}' only these 3 characters in the quotation marks don't work. Sorry for the inconvenience!")
exit()
elif message[a] == '}':
print("Sorry, the encryptor doesn't support any special characters that aren't on a normal office keyboard. However, these characters that are on a keyboard don't work: '~', '{', and '}' only these 3 characters in the quotation marks don't work. Sorry for the inconvenience!")
exit()
a=a+1
if a==b:
break
encrypt(message, aShift, sShift)

到目前为止,我制作了这个代码,但是由于某种原因,解密器不能与加密器的输出一起工作。(也就是说,蛮力根本不输出输入)

在上下文中,此程序的目的是通过将所有字符转换为ascii码并在其上添加一个数字来加密字符串。然后,新的数字将被转换回一个字符,字符串将被移动一定的量。例如,"abc";Shift 1 ->[66,67,68] ->"BCD"Shift 2 ->"DBC">

如果有人可以帮助指出为什么它不能工作(或者如果有一个更好的解决方案,例如"你写错了这个区域",或者"加密结果是错误的",等等),将不胜感激!

由于您表示只想从执行蛮力算法开始解密加密的短语或单词,因此直观的解决方案似乎是为ASCII移位和字符串移位函数创建互补函数,这些函数为解密函数执行相反方向的移位。然后,构建解密函数,使其几乎是加密函数的副本,除了它利用互补函数循环通过可能的移位。

下面是修改后的代码,你可以尝试一下。

def asciiShift(character, aShift):
asciiNum = ord(character) + (int(aShift))%26
return asciiNum

def asciiShiftx(character, aShift):
asciiNum = ord(character) - (int(aShift))%26
return asciiNum 
def stringShift(string, shift):
k=0
newstring = ''
string_len = len(string)
while k<string_len:
newstring = newstring + string[int(((int(shift)%int(string_len))+k))%int(string_len)]
k=k+1
if k==string_len:
break
return newstring

def stringShiftx(string, shift):
k=0
newstring = ''
string_len = len(string)
while k<string_len:
newstring = newstring + string[int(((int(shift * -1)%int(string_len))+k))%int(string_len)]
k=k+1
if k==string_len:
break
return newstring
def encrypt(message, aShift, sShift):
i=0
length = len(message)
asciiChars = []
while i<length:
asciiChars.append(asciiShift(message[i].upper(), aShift))
i=i+1
if i==length:
break
string = ''
j=0
while j<length:
string = string + str(chr(asciiChars[j]))
j=j+1
shifted_string = stringShift(string, sShift)
print(shifted_string)
def decrypt(message):
for z1 in range(26):
for z2 in range(26):
i=0
lengthx = len(message)
asciiChars = []
while i<lengthx:
asciiChars.append(asciiShiftx(message[i].upper(), z1))
i=i+1
if i==lengthx:
break
string = ''
j=0
while j<lengthx:
string = string + str(chr(asciiChars[j]))
j=j+1
shifted_string = stringShiftx(string, z2)
print('ASCII shift: ', z1, 'String shift: ', z2, shifted_string)
typeConfirm = input("Encrypt or Decrypt? (E/D): ")
if typeConfirm[0].upper() == 'D':
decryptBool = True
elif typeConfirm[0].upper()== 'E':
decryptBool = False
else:
print("You probably said an incorrect choice or read the question incorrectly. Exiting program for now...")
exit()
if decryptBool == True:
decrypt_msg = input("What is the encoded message?: ")
decrypt(decrypt_msg)
else:
message = input("What is your message?: ")
aShift = input("ASCII shift?: ")
sShift = input("String shift?: ")

a=0
b=len(message)
while a<b:
if message[a] == '~':
print("Sorry, the encryptor doesn't support any special characters (other than the ones on a normal keyboard, or these characters that are on a keyboard: '~', '{', and '}' only these 3 characters in the quotation marks don't work. Sorry for the inconvenience!")
exit()
elif message[a] == '{':
print("Sorry, the encryptor doesn't support any special characters (other than the ones on a normal keyboard, or these characters that are on a keyboard: '~', '{', and '}' only these 3 characters in the quotation marks don't work. Sorry for the inconvenience!")
exit()
elif message[a] == '}':
print("Sorry, the encryptor doesn't support any special characters that aren't on a normal office keyboard. However, these characters that are on a keyboard don't work: '~', '{', and '}' only these 3 characters in the quotation marks don't work. Sorry for the inconvenience!")
exit()
a=a+1
if a==b:
break
encrypt(message, aShift, sShift)

我通过加密一个短短语("HELLO THERE")并使用"4"的ASCII移位来测试这一点。和"22"的字符串移位。

@Una:~/Python_Programs/Encrypt$ python3 Encrypt.py 
Encrypt or Decrypt? (E/D): E
What is your message?: HELLO THERE
ASCII shift?: 4
String shift?: 22
LIPPS$XLIVI

然后,我再次调用程序并要求它解密加密的短语("LIPPS$ xliv ")。我将跳到终端输出中可以看到原始文本的部分。

@Una:~/Python_Programs/Encrypt$ python3 Encrypt.py 
Encrypt or Decrypt? (E/D): D
What is the encoded message?: LIPPS$XLIVI
. . .
ASCII shift:  4 String shift:  18 O THEREHELL
ASCII shift:  4 String shift:  19 LO THEREHEL
ASCII shift:  4 String shift:  20 LLO THEREHE
ASCII shift:  4 String shift:  21 ELLO THEREH
ASCII shift:  4 String shift:  22 HELLO THERE
ASCII shift:  4 String shift:  23 EHELLO THER
ASCII shift:  4 String shift:  24 REHELLO THE
ASCII shift:  4 String shift:  25 EREHELLO TH

当人们看到正确的解密短语时,互补移位值与原始移位值匹配是有意义的。

无论如何,尝试一下。希望对你有帮助。

致意。

最新更新