Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.1k views
in Technique[技术] by (71.8m points)

python - GROK LEARNING: Cryptography Multi-key

I am trying to write a program to decrypt an XOR cipher with a key that has multiple characters in python and then print it out. This is my main file:

from crypto import decrypt
if __name__ == "__main__":

INTERCEPT_1="UDAXIHHEXDVXRCSNBACGHQTARGWUWRNHOSIZAYZFWNKIEGYKDCMDLLTIZBXORDMCRJUTLSGWCBVHYJCHDMIOULFL"

INTERCEPT_2 =";!7=;h/*6*7x5*%+b8,2h$$mr)2#2 n/ =';a5?2w7$<e#6<*om*):1;z%7!<%m1'$u5><29'b7&=j'-7(;;u5)9"


for i in range(len(INTERCEPT_1)):
  print(INTERCEPT_1, INTERCEPT_2)
  print(decrypt(INTERCEPT_1, INTERCEPT_2))


My crypto file:

from functions import or
def encrypt(message, key):
  encrypted = ''
for character in message:
  encrypted = encrypted + xor(character, key)
return encrypted

def decrypt(message, key):
  return encrypt(message, key)


And my functions file:

def xor(character, key):
  code = ord(character) ^ ord(key)
  character = chr(code)
  return character

I get this error:

Traceback (most recent call last):
File "program.py", line 11, in <module>
print(decrypt(INTERCEPT_1, INTERCEPT_2))
File "/tmp/tmpZ59DTD/crypto.py", line 10, in decrypt
return encrypt(message, key)
File "/tmp/tmpZ59DTD/crypto.py", line 6, in encrypt
encrypted = encrypted + xor(character, key)
File "/tmp/tmpZ59DTD/functions.py", line 2, in xor
code = ord(character) ^ ord(key)
TypeError: ord() expected a character, but string of length 88 found

How should I fix this? Any help is appreciated, thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

2 Answers

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
0 votes
by (100 points)
from crypto import decrypt

if __name__ == "__main__":

  message = "UDAXIHHEXDVXRCSNBACGHQTARGWUWRNHOSIZAYZFWNKIEGYKDCMDLLTIZBXORDMCRJUTLSGWCBVHYJCHDMIOULFL"

  key = ";!7=;h/*6*7x5*%+b8,2h$$mr)2#2 n/ =';a5?2w7$<e#6<*om*):1;z%7!<%m1'$u5><29'b7&=j'-7(;;u5)9"

  count = 0

  total = ''

  for thing in message:

    total += decrypt(thing, key[count])

    count += 1

  print(total)

#its a rickroll btw
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...