Question

I know there have been several other questions asking the exact same thing but when I run: import commands from pyDes import *

def encrypt(data, password,):
    k = des(password, CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5,)
    d = k.encrypt(data,)
    return d
def decrypt(data, password,):
    k = des(password, CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5,)
    d = k.decrypt(data,)
    return d
command1 = commands.getstatusoutput('ifconfig',)
encrypted = encrypt(command1, '12345678',)

I get this:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in encrypt
  File "build/bdist.macosx-10.6-universal/egg/pyDes.py", line 658, in encrypt
  File "build/bdist.macosx-10.6-universal/egg/pyDes.py", line 195, in _padData
TypeError: can only concatenate tuple (not "str") to tuple

Again I know this has been asked several times but I can't seem to make this work by putting commas in the right places like the other questions.

Was it helpful?

Solution

The function was expecting a one-element tuple as an argument. To specify a one-element tuple unambiguously in Python, you need parentheses:

tuple_containing_only_zero = (0,)   # note the comma!

The comma is the actual constructor for the tuple in Python, so you can specify longer tuples without the need for parentheses:

>>> longer_tuple = 1, 2, 3, 4, 5
>>> longer_tuple
(1, 2, 3, 4, 5)

but if you're calling a function:

f(x, y,)   # is y a 1-tuple or a SyntaxError?

one-tuples without parens would be ugly and ambiguous.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top