Basic XOR encrypt question , please help


#1

Question - write a program that asks the user to enter a string and also ask the user to enter an integer key between 0-255. If the key is not of the correct range, keep asking the user to enter an appropriate key. Now write a function that can XOR encrypt the string given. Then print the encrypted string. Verify that you can decrypt the string as well.



plaintext = int(raw_input("enter string"))
while True:
      b = int(raw_input("enter number between 0-255"))
      if b < 0 or b > 255:
          print "please renter number between 0-255="
   else: 
          break

cipher_text = (plaintext) 'XOR encryption'
for ch in plaintext:
     val = ord(ch) 
     cipher_val = ...
     cipher_ch = chr(cipher_val)

print (cipher_text)

I have left a few blanks because I just don’t know what to do , if I could get some help I would appreciate it , thank you


#2

Your conditional can be written the same way…

if b not in range(256):

Be sure your else lines up with if or you will get an indentation error.

while True:
    b = int(raw_input("enter number between 0-255"))
    if b not in range(256):
        print "please enter number between 0-255: "
    else: 
        break

Is that meant to be comment following the statement? Why the parens?

… = val ^ b

>>> ord('a')
97
>>> 97 ^ 128
225
>>> 225 ^ 128
97
>>> 

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.