user3431573 user3431573 - 20 days ago 8
Python Question

Sage SDES Value Error

For some reason I am getting an error when I try to run the Sage implementation of SDES. I did not make any changes to the source code. I did add this to encrypt a plaintext.

Y = SDESEncrypt("10111101", "1010000010")
. I get an error from this function:

def XorBlock(block1, block2):
r"""
Xors two blocks together.
"""
l = len(block1);
if (l != len(block2)):
raise ValueError, "XorBlock arguments must be same length"
return [(block1[j]+block2[j]) % 2 for j in xrange(l)];


The error is:
TypeError: not all arguments converted during string formatting


I'm really unsure why I get this error. I retrieved the source code from the Stallings Sage examples Appendix. It can be found here: http://faculty.mu.edu.sa/public/uploads/1360993259.0858Cryptography%20and%20Network%20Security%20Principles%20and%20Practice,%205th%20Edition.pdf It begins on page 689.

DSM DSM
Answer

That error message is a sign that something is getting strings when it's not expecting to. SDESEncrypt starts

def SDESEncrypt(plaintext_block, K):
    r"""
    Performs a single SDES plaintext block encryption.
    (Given plaintext and key as bit lists.)
    """

You're not passing bit lists, you're passing strings. While I don't know anything about these functions or how they're supposed to work, it's pretty easy to turn your strings into bit lists and get a result:

sage: Integer("10111101",2).bits()
[1, 0, 1, 1, 1, 1, 0, 1]
sage: SDESEncrypt(Integer("10111101",2).bits(),Integer("1010000010",2).bits())
[0, 0, 1, 0, 1, 1, 1, 0]

assuming I copied the code correctly. Which I might not have done, so don't trust the result, but I'm pretty sure this that you're meant to pass a list of 0s and 1s, not a string.