Assistance needed with cyphertext in python

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

46 Replies - 3705 Views - Last Post: 06 April 2013 - 08:31 PM Rate Topic: -----

#1 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Assistance needed with cyphertext in python

Posted 25 March 2013 - 07:22 AM

AS my homework assignment in Criptography i have ben given 11 cyphertext cyphers. in each is a hidden message. all togeather are the answers to the homework. The instructer has given me the python script used to create these cyphertexts (pasted below). Can someone show or help me manipulate this code to de-cypher rather than encript? By The Way: I am new to python.
Thanks for all the help
-Tristan

Sorry forgot to add above:

import sys

MSGS = ( ---  11 secret messages  --- )

def strxor(a, B)/>:     # xor two strings of different lengths
    if len(a) > len(B)/>:
        return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(B)/>], B)/>])
    else:
        return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])

def random(size=16):
    return open("/dev/urandom").read(size)

def encrypt(key, msg):
    c = strxor(key, msg)
    print
    print c.encode('hex')
    return c

def main():
    key = random(1024)
    ciphertexts = [encrypt(key, msg) for msg in MSGS]



Is This A Good Question/Topic? 0
  • +

Replies To: Assistance needed with cyphertext in python

#2 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Assistance needed with cyphertext in python

Posted 26 March 2013 - 07:32 AM

Does anyone know how to do this? I just need a small tutorial on how to use the source code to decypher the End code. Is there a string or snippet of code i could enter at a specific place to make it decode instead of encode?
Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 09:48 AM

Sorry no one got to you sooner.

Would it helped if I told you:
a ^ b = c
c ^ b = a

Here's some working code if you need it
Spoiler

Was This Post Helpful? 1
  • +
  • -

#4 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 10:51 AM

I appreciate your help. How did you come up eith that code or did you already know it?
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 11:14 AM

Well, most if it I copied from you :-P

As far as the minor change in your main method, it came from the concept I mentioned above.

a = original text
b = key
c = ciphered text

a ^ b = c
c ^ b = a

This tells us that if you cipher the text using the key, you have ciphered text. If you cipher the ciphered text against the same key, you have the original text again.

This post has been edited by atraub: 27 March 2013 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3176
  • View blog
  • Posts: 10,638
  • Joined: 12-December 12

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 11:27 AM

I don't follow this:

a ^ b = c
c ^ b = a

Mathematically, with exponentation, it would be:

a ^ b = c
c ^( 1/ b) = a

??

This post has been edited by andrewsw: 27 March 2013 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#7 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 12:01 PM

So how would i put an encryped message into this to then get the original message back out?
And with the
a ^ b = c
method:
Can this be used to decypher a cyphertext without this script? Or is having the code nessesary?
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7562
  • View blog
  • Posts: 12,680
  • Joined: 19-March 11

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 12:09 PM

In case anyone's interested to know this, the OP is taking Dan Boneh's Cryptography course on coursera, and is asking for help on the first week's extra-credit programming challenge assignment.

@andrewsw: the ^ here is a bitwise xor operator, not exponentiation. ^ does not actually indicate exponentiation in any programming language that I know, and certainly not in python...

The idea is to xor a message bitwise against a completely random string to create a one-time pad. In this case, the eleven messages are all encrypted against the same one-time pad, which allows the messages to be compared against each other to reveal the original pad.

To the OP: This is not a trivial piece of work if you're new to crypto, but the method of solving it is explained quite well in the course. You don't need the original python to decrypt, you just need to understand the OTP method as described in the lectures.
To use this to decrypt, you'll just apply the encrypt function with the original key and the encrypted message. The problem is to discover the one-time pad... :)/>

This post has been edited by jon.kiparsky: 27 March 2013 - 12:09 PM

Was This Post Helpful? 1
  • +
  • -

#9 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 12:18 PM

Well Yes i am in that course. I am trying to get help on this site because i cant understand him. He talks about the OTP and how to xor the K and the C to get M but i feel like a Blantent Idiot listening to him because i know nothing. I took the course to better myself and hoping to learn in the field of cryptography (Wanting to do Nework Security next). Now that i started i want to finish it. I dont give up easily and dont plan to. This is the reason i am asking for help here on the D.I.C. Forums.
Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 12:32 PM

Look at what I wrote again.

a = original text
b = key
c = ciphered text

a ^ b = c
c ^ b = a

(the original text) xor (the key) yields ciphered text
(ciphered text) xor (the key) yields the original text

Let's look at a simpler example.

5 in binary is 0101
2 in binary is 0010


5 ^ 2 = 7 or 0111
7 ^ 2 = 5

a = 5
b = 2
c = 7

notice:
a ^ b = c
AND
c ^ b = a

thus, we could say 5 is our secret number, 2 is our key, and 7 is our ciphered number. By taking out ciphered number and xor'ing it against the key, we get our original secret number.

Does that make sense?

This post has been edited by atraub: 27 March 2013 - 12:33 PM

Was This Post Helpful? 1
  • +
  • -

#11 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3176
  • View blog
  • Posts: 10,638
  • Joined: 12-December 12

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 12:32 PM

View Postjon.kiparsky, on 27 March 2013 - 07:09 PM, said:

@andrewsw: the ^ here is a bitwise xor operator, not exponentiation. ^ does not actually indicate exponentiation in any programming language that I know, and certainly not in python...

It does in VB, but thank you. Andy.
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7562
  • View blog
  • Posts: 12,680
  • Joined: 19-March 11

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 12:34 PM

He does go fast, it's true, but it's all there. You really want to get solid on what it means to apply a one-time pad to a message, because really all you're going to be doing is generating surrogates for the one-time pad and considering the cryptographic implications of any particular approach.

So get down to the basics: what does it mean to xor "thread" against "string"?
What does it mean to xor 'a' against 'b'?

What happens if you xor 'A' against 'a'?

If you have

a = "thread"
b = "string"
c = "%sH~af"
d = a ^ c
e = b ^ c

is there any relationship between

a ^ b and d ^ e ??

Have you tried using the strxor function to xor the different strings against each other?
Was This Post Helpful? 1
  • +
  • -

#13 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 12:38 PM

VB syntax should never be used as a model for understanding how other languages work lol
Was This Post Helpful? 0
  • +
  • -

#14 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 12:40 PM

Knowint this will and trying not to sound stupib but how do you xor? I thought i understood but it seems i dont. I love that fact that you are helping me Jon, but i feel i need to know this befor i can answer your questions
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3176
  • View blog
  • Posts: 10,638
  • Joined: 12-December 12

Re: Assistance needed with cyphertext in python

Posted 27 March 2013 - 12:45 PM

View Postatraub, on 27 March 2013 - 07:38 PM, said:

VB syntax should never be used as a model for understanding how other languages work lol

He, he.. Yeah, I know :)
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »