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

Peter Otten __peter__ at web.de
Sat Oct 8 05:37:41 EDT 2011


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)

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-
[...]




More information about the Python-list mailing list