Question

I've researched this topic for the last two days and have tried multiple ways of trying to solve this issue I am having with my program. This program is suppose to take a 16 bit random key and XOR it with a message typed in by the user. There is currently no errors in my program but I'm having on issue.

import string
import random

message = raw_input("Enter your message: ").split(",")
size = 2    # or whatever lenght you want your random string to be
allowed = string.ascii_letters # add any other allowed characters here
randomstring = ''.join([allowed[random.randint(0, len(allowed) - 1)] for x in xrange(size)])

print
print("This is the key used for encryption:")
print randomstring #prints out key used
print
print("APPLYING XOR METHOD TO MESSAGE AND KEY")
print("--------------------------------------")

for i in xrange(0,len(message)-1):
    l += [ord(message) ^ ord(randomstring) for message,randomstring in zip(message,randomstring)]
print l

print

#BRUTE FORCE TO FIND OUT RANDOMSTRING
key= []
count =0
while(key!=randomstring):
    key = ''.join([allowed[random.randint(0, len(allowed) - 1)] for x in xrange(size)])
    count = count + 1
    if(key==randomstring):
        print ("FOUND KEY USED BY BRUTE FORCE: "+key)
        print (count)
        break

This code generates the random key and find out the same key after the for loops finishes. For example my output would be,

Enter your message: hi

This is the key used for encryption:
ng

APPLYING XOR METHOD TO MESSAGE AND KEY
--------------------------------------

FOUND KEY USED BY BRUTE FORCE: ng
1052

I need a way to print the encrypted XOR message to the screen. Thank you in advance!

Was it helpful?

Solution

First, fix your call to raw_input:

message = raw_input("Enter your message: ")

When you called .strip(",") on your input string, you created a list of strings. So, if you had originally entered "Hello, world", the result would have been ['Hello', ' world'].

Later in your program you require message to be a string, not a list.

Next, initialize the variable l and print its value after the loop:

l=[]
for i in xrange(0,len(message)-1):
    l += [ord(message) ^ ord(randomstring) for message,randomstring in zip(message,randomstring)]
print l
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top