unsupported operand type(s) for pow(): 'unicode', 'long', 'long': Pycrypto

Kayode Odeyemi dreyemi at gmail.com
Sat Oct 8 05:55:23 EDT 2011


On Sat, Oct 8, 2011 at 10:37 AM, Peter Otten <__peter__ at web.de> wrote:

> Kayode Odeyemi wrote:
>
> > On Sat, Oct 8, 2011 at 12:50 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> >
> >> That latter function probably want integers code in range(256).
> >
> >
> > Yes! Non-unicode. The source reads:
> >
> > def _encrypt(self, m):
> >         # compute m**d (mod n)
> >         return pow(m, self.e, self.n)
> >
> >>From the source, it is provided as is.
> >
> > The arguments must have numeric types
> >>
> >
> > How come it accepts str type?
>
> pow() does not accept a str. Most likely there is conversion code similar
> to
>
> if isinstance(m, str):
>    m = convert_to_int(m)
>

Ah! I see the reason for the unsupported operand type error. It wants a str,
so
it can do a conversion to int.

But since unicode, long etc is not allowed, shouldn't that been taken care
of in
_encrypt? Or is this by design?


> that precedes the _encrypt() method call and lets unicode slip through.
> Grepping through the PyCrypto source for isinstance indeed finds a few
> candidates. Example:
>
> $ find . -name \*.py | xargs grep isinstance -A5
> [...]
> ./PublicKey/pubkey.py:        if isinstance(plaintext, types.StringType):
> ./PublicKey/pubkey.py-            plaintext=bytes_to_long(plaintext) ;
> wasString=1
> ./PublicKey/pubkey.py:        if isinstance(K, types.StringType):
> ./PublicKey/pubkey.py-            K=bytes_to_long(K)
> ./PublicKey/pubkey.py-        ciphertext=self._encrypt(plaintext, K)
> ./PublicKey/pubkey.py-        if wasString: return tuple(map(long_to_bytes,
> ciphertext))
> ./PublicKey/pubkey.py-        else: return ciphertext
> ./PublicKey/pubkey.py-
> [...]
>
> Thanks. This make things clearer

>  --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111008/d42ccbf5/attachment.html>


More information about the Python-list mailing list