Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69633 > unrolled thread

converting strings to hex

Started bydave em <daveandem2000@gmail.com>
First post2014-04-03 19:10 -0700
Last post2014-04-04 10:07 +0000
Articles 12 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  converting strings to hex dave em <daveandem2000@gmail.com> - 2014-04-03 19:10 -0700
    Re: converting strings to hex Tim Chase <python.list@tim.thechases.com> - 2014-04-03 21:31 -0500
      Re: converting strings to hex dave em <daveandem2000@gmail.com> - 2014-04-03 20:10 -0700
        Re: converting strings to hex Mark H Harris <harrismh777@gmail.com> - 2014-04-03 22:20 -0500
          Re: converting strings to hex dave em <daveandem2000@gmail.com> - 2014-04-03 20:22 -0700
            Re: converting strings to hex Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-04-04 09:14 +0100
    Re: converting strings to hex Mark H Harris <harrismh777@gmail.com> - 2014-04-03 21:38 -0500
      Re: converting strings to hex "James Harris" <james.harris.1@gmail.com> - 2014-04-04 07:16 +0100
        Re: converting strings to hex Mark H Harris <harrismh777@gmail.com> - 2014-04-04 16:33 -0500
          Re: converting strings to hex Chris Angelico <rosuav@gmail.com> - 2014-04-05 09:36 +1100
            Re: converting strings to hex Mark H Harris <harrismh777@gmail.com> - 2014-04-04 18:00 -0500
      Re: converting strings to hex alister <alister.nospam.ware@ntlworld.com> - 2014-04-04 10:07 +0000

#69633 — converting strings to hex

Fromdave em <daveandem2000@gmail.com>
Date2014-04-03 19:10 -0700
Subjectconverting strings to hex
Message-ID<c23e9bfd-63f4-4381-b3e2-a75cb87c8293@googlegroups.com>
Hello,

I am taking a cryptography class and am having a tough time with an assignment similar to this.

Given plain text message 1 and cipher 1 compute cipher 2 for message 2

Work flow will be:
- figure out the key
- use key to compute c2

So this is what I have so far and am getting nowhere fast.  I could use a little help on this one.

So my first step is to compute the key.  I suspect my error below is because c1 is a float and m1 is a string but I don't know how to turn the string into a float.


####  Python 2.7###

m1text="my test message"
print( m1text + ' in hex is ')
print m1text.encode("hex")
m1 = m1text.encode("hex")
c1=0x6c73d5240a948c86981bc294814d 

k=m1^c1
print( 'the key = ' )
print hex(k)

This code yields the following:

my test message in hex is 
6d792074657374206d657373616765
Traceback (most recent call last):
  File "/media/.../Crypto/Attackv2.py", line 10, in <module>
    k=m1^c1
TypeError: unsupported operand type(s) for ^: 'str' and 'long'

Any help is most appreciated.

Dave

[toc] | [next] | [standalone]


#69635

FromTim Chase <python.list@tim.thechases.com>
Date2014-04-03 21:31 -0500
Message-ID<mailman.8874.1396578707.18130.python-list@python.org>
In reply to#69633
On 2014-04-03 19:10, dave em wrote:
> So my first step is to compute the key.  I suspect my error below
> is because c1 is a float and m1 is a string but I don't know how to
> turn the string into a float.

For the record, "c1" in your example should be an integer/long

It sounds like you want the optional parameter to int() so you'd do

>>> hex_string = "My text message".encode("hex")
>>> hex_string
'4d792074657874206d657373616765'
>>> m1 = int(hex_string, 16)  # magic happens here
>>> m1
402263600993355509170946582822086501L
>>> c1=0x6c73d5240a948c86981bc294814d 
>>> c1
2199677439761906166135512011931981L
>>> k = m1 ^ c1
>>> k
400239414552556350237329405469124136L
>>> hex(k) # as a string
'0x4d1553a14172e0acebfd68b1f5e628L'


-tkc



[toc] | [prev] | [next] | [standalone]


#69639

Fromdave em <daveandem2000@gmail.com>
Date2014-04-03 20:10 -0700
Message-ID<77b50721-3206-4b03-8d49-1cd73ba46d66@googlegroups.com>
In reply to#69635
On Thursday, April 3, 2014 8:31:42 PM UTC-6, Tim Chase wrote:
> On 2014-04-03 19:10, dave em wrote:
> 
> > So my first step is to compute the key.  I suspect my error below
> 
> > is because c1 is a float and m1 is a string but I don't know how to
> 
> > turn the string into a float.
> 
> 
> 
> For the record, "c1" in your example should be an integer/long
> 
> 
> 
> It sounds like you want the optional parameter to int() so you'd do
> 
> 
> 
> >>> hex_string = "My text message".encode("hex")
> 
> >>> hex_string
> 
> '4d792074657874206d657373616765'
> 
> >>> m1 = int(hex_string, 16)  # magic happens here
> 
> >>> m1
> 
> 402263600993355509170946582822086501L
> 
> >>> c1=0x6c73d5240a948c86981bc294814d 
> 
> >>> c1
> 
> 2199677439761906166135512011931981L
> 
> >>> k = m1 ^ c1
> 
> >>> k
> 
> 400239414552556350237329405469124136L
> 
> >>> hex(k) # as a string
> 
> '0x4d1553a14172e0acebfd68b1f5e628L'
> 
> 
> 
> 
> 
> -tkc

Thanks, got it.  Sometimes the simple things can be difficult.

Dave

[toc] | [prev] | [next] | [standalone]


#69641

FromMark H Harris <harrismh777@gmail.com>
Date2014-04-03 22:20 -0500
Message-ID<lhl8e9$fof$2@speranza.aioe.org>
In reply to#69639
On 4/3/14 10:10 PM, dave em wrote:
> Thanks, got it.  Sometimes the simple things can be difficult.
>
> Dave
>

You haven't seen nothing yet, wait till M.L. catches you on the flip 
side for using gg.   {running for cover}

marcus

[toc] | [prev] | [next] | [standalone]


#69642

Fromdave em <daveandem2000@gmail.com>
Date2014-04-03 20:22 -0700
Message-ID<5c9bd34f-b9ba-4c2f-b72e-ca72982ad0c9@googlegroups.com>
In reply to#69641
> You haven't seen nothing yet, wait till M.L. catches you on the flip 
> 
> side for using gg.   {running for cover}


Who is ML?

[toc] | [prev] | [next] | [standalone]


#69653

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-04-04 09:14 +0100
Message-ID<mailman.8887.1396599317.18130.python-list@python.org>
In reply to#69642
On 04/04/2014 04:22, dave em wrote:
>
>> You haven't seen nothing yet, wait till M.L. catches you on the flip
>>
>> side for using gg.   {running for cover}
>
>
> Who is ML?
>

Good morning :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [next] | [standalone]


#69636

FromMark H Harris <harrismh777@gmail.com>
Date2014-04-03 21:38 -0500
Message-ID<533E1B2E.5040608@gmail.com>
In reply to#69633
On 4/3/14 9:10 PM, dave em wrote:
>
> I am taking a cryptography class and am having a
> tough time with an assignment similar to this.
>

hi Dave, if your instructor wanted you to work on this with other people 
she would have made it a group project and ordered pizza for everyone.

I'll give you some credit, that last couple of folks that wanted help 
with their homework here could not bring themselves to admit they wanted 
help with their homework.   :)

    "HAL, please help me with my homework!"

    "I'm sorry, Dave, I can't do that..."

    "HAL!!?"

    "I'm sorry, Dave, I just can't do that..."


marcus

[toc] | [prev] | [next] | [standalone]


#69646

From"James Harris" <james.harris.1@gmail.com>
Date2014-04-04 07:16 +0100
Message-ID<lhlioc$b4s$1@dont-email.me>
In reply to#69636
"Mark H Harris" <harrismh777@gmail.com> wrote in message 
news:533E1B2E.5040608@gmail.com...
> On 4/3/14 9:10 PM, dave em wrote:
>>
>> I am taking a cryptography class and am having a
>> tough time with an assignment similar to this.
>>
>
> hi Dave, if your instructor wanted you to work on this with other people 
> she would have made it a group project and ordered pizza for everyone.
>
> I'll give you some credit, that last couple of folks that wanted help with 
> their homework here could not bring themselves to admit they wanted help 
> with their homework.   :)

YMMV but I thought the OP had done a good job before asking for help and 
then asked about only a tiny bit of it. Some just post a question!

>    "HAL, please help me with my homework!"
>
>    "I'm sorry, Dave, I can't do that..."
>
>    "HAL!!?"
>
>    "I'm sorry, Dave, I just can't do that..."

You might find this interesting.

  http://sundry.wikispaces.com/transcript-2001

James

[toc] | [prev] | [next] | [standalone]


#69679

FromMark H Harris <harrismh777@gmail.com>
Date2014-04-04 16:33 -0500
Message-ID<lhn8eg$d7v$1@speranza.aioe.org>
In reply to#69646
On 4/4/14 1:16 AM, James Harris wrote:
> YMMV but I thought the OP had done a good job before asking for help and
> then asked about only a tiny bit of it. Some just post a question!

    Indeed they do. Its a little like negotiating with terrorists. As 
soon as you negotiate with the first one, you then must negotiate with 
all of them.  Bad plan.
    The OP was soooo close, that to give him the help is immoral for two 
reasons: 1) it deprives him of the satisfaction of accomplishing the 
solution to the puzzle himself, and 2) it deprives the instructor 
(whoever she is) of the teachable moment. There is something she is 
trying to get Dave to learn, and she really *does* want him to go 
through the entire exercise on his own.
    Other than that, I give the OP credit for honesty and the good 'ol 
college try. But next time I'd like to see Dave post the problem (and 
his own solution) and then let the professional Cpython community pipe 
up on enhancements, or challenges. That way the OP learns twice.

> You might find this interesting.
>
>    http://sundry.wikispaces.com/transcript-2001

Indeed I do. I was thirteen when Odyssey came out. I was so impressed 
with the ship, and yet so disappointed with HAL. My favorite line, "I'm 
sorry, Dave, I'm afraid I can't do that," *must* have the word 'afraid' 
in there;  the HAL 9000 not only had mental illness, &will, but also 
classic controlled emotion...  but HAL's soft confident assured voice 
pattern was fabulous, wasn't it?  My second favorite line was, "Look 
Dave, I can see you're really upset about this," as Dave was unplugging 
HAL's neural net. Classic.

It was not until my later career at IBM did I realize that 'HAL' was one 
letter -0ff from 'IBM'; just never thought about it before.  Hilarious. 
Wasn't it interesting that Kubrick and Clarke were concerned about 
machines acquiring will and emotion (or mental illness) before anyone 
got those von Neumann processors to 'think' in the first place?
Which, of course, because of the negative answer to the 
Entscheidungsproblem is not possible.

This was a passion of Alan Turing; machines thinking I mean. We're going 
to find that thinking machine IS possible, but that the system is going 
to require multiple quantum processors running together (out of phase 
with one another) in order to be self-aware.  I think it will happen in 
my life-time; I'm encouraged with the work so far...


I'm sorry, Dave, I'm afraid I can't do that...

... Look Dave, I can see you're really upset about this...




marcus

[toc] | [prev] | [next] | [standalone]


#69684

FromChris Angelico <rosuav@gmail.com>
Date2014-04-05 09:36 +1100
Message-ID<mailman.8902.1396651007.18130.python-list@python.org>
In reply to#69679
On Sat, Apr 5, 2014 at 8:33 AM, Mark H Harris <harrismh777@gmail.com> wrote:
> On 4/4/14 1:16 AM, James Harris wrote:
>>
>> YMMV but I thought the OP had done a good job before asking for help and
>> then asked about only a tiny bit of it. Some just post a question!
>
>
>    Indeed they do. Its a little like negotiating with terrorists. As soon as
> you negotiate with the first one, you then must negotiate with all of them.
> Bad plan.

What, you treat student programmers like terrorists?!? Ouch.

>    The OP was soooo close, that to give him the help is immoral for two
> reasons: 1) it deprives him of the satisfaction of accomplishing the
> solution to the puzzle himself, and 2) it deprives the instructor (whoever
> she is) of the teachable moment. There is something she is trying to get
> Dave to learn, and she really *does* want him to go through the entire
> exercise on his own.

I strongly disagree. If someone is asking for a hint, it's because
s/he is trying to learn. I'm always willing to help someone learn,
regardless of whether they're going through a course or currently
employed or whatever. Sometimes a small hint can be obtained from the
interpreter itself; but often, it takes a measure of experience to
grok (one of the differences between the expert and the inexperienced
is how quickly a traceback can be read - an expert can often go
straight to the line with the problem); a hint from another Python
programmer can be immensely helpful, as it can include advice as well
as a hard "this works, that doesn't".

ChrisA

[toc] | [prev] | [next] | [standalone]


#69690

FromMark H Harris <harrismh777@gmail.com>
Date2014-04-04 18:00 -0500
Message-ID<lhndja$ohm$1@speranza.aioe.org>
In reply to#69684
On 4/4/14 5:36 PM, Chris Angelico wrote:
> If someone is asking for a hint, it's because
> s/he is trying to learn. I'm always willing to help someone learn,
> regardless of whether they're going through a course or currently
> employed or whatever. Sometimes a small hint can be obtained from the
> interpreter itself; but often, it takes a measure of experience to
> grok (one of the differences between the expert and the inexperienced
> is how quickly a traceback can be read - an expert can often go
> straight to the line with the problem); a hint from another Python
> programmer can be immensely helpful, as it can include advice as well
> as a hard "this works, that doesn't".

    I accept that.  It really depends case-by-case at what point the 
person asks for help ( tension is really a good teacher sometimes ) and 
at what level the help is provided ( give the hint, but maintain the 
tension ).

    I particularly agree with your statement above in every venue except 
the educational venue where the break-over moment (the teachable moment) 
is NOT the moment being queried !  If the student is NOT able to 
discover on their own that one 'moment' the teachable moment may be lost 
forever, and the student(s) never have the opportunity to visit that 
moment again.  Now, was this one of those for Dave, who knows. Beats me.

    I do appreciate your disagreement, and I think you have a good point.


marcus


[toc] | [prev] | [next] | [standalone]


#69662

Fromalister <alister.nospam.ware@ntlworld.com>
Date2014-04-04 10:07 +0000
Message-ID<Svv%u.1300$9I2.1281@fx07.am4>
In reply to#69636
On Thu, 03 Apr 2014 21:38:38 -0500, Mark H Harris wrote:

> On 4/3/14 9:10 PM, dave em wrote:
>>
>> I am taking a cryptography class and am having a tough time with an
>> assignment similar to this.
>>
>>
> hi Dave, if your instructor wanted you to work on this with other people
> she would have made it a group project and ordered pizza for everyone.
> 
> I'll give you some credit, that last couple of folks that wanted help
> with their homework here could not bring themselves to admit they wanted
> help with their homework.   :)
> 
>     "HAL, please help me with my homework!"
> 
>     "I'm sorry, Dave, I can't do that..."
> 
>     "HAL!!?"
> 
>     "I'm sorry, Dave, I just can't do that..."
> 
> 
> marcus

in general i would say:-
 "Can you 'HELP' with my homework?" is resonable
 "Can you 'DO' my homework for me" if wrong
 Not even mentioning it is homework is really taking the P**s

Dave has been open
Shown his existing work & received assistance on a minor correction, 
isn't that how we all learn :-)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web