2

I am working on DES algorithm in Python.

In one of the functions namely Fiestle function, I have to XOR elements to get the cypher.

Is there any way I can perform shifting operation in python ,Below is the code that I tried:

C0 is 1111000011001100101010101111

D0 is 0101010101100110011110001111

c0_d0=[]
for i in range(1):
    t=[]
    t.append(C0)
    t.append(D0)
    c0_d0.append(t)

#print c0_d0 

def str_to_bin(strr,shifts):
for i in range(1,17):
    temp=[]
    for j in range(1):
        temp.append(int(c0_d0[i-1][0])<<1)
        temp.append(int(c0_d0[i-1][1])<<1)
    c0_d0.append(temp)

return c0_d0

The output I got is like:enter image description here

I want the elements to be a binary string obtained by shifting bits.That is first element pair is obtained by shifting the previous element pair and next element pair is obtained by previous element and so on.Only 0th index element are given.

Example C0 is 1111000011001100101010101111 D0 is 0101010101100110011110001111

C1 should be C0<<1 which is equal to 1110000110011001010101011110

D1 should be D0<<1 which is equal to 1010101011001100111100011110

2
  • you want to shift a binary string? I suppose that the size is 28 bits (you did not specify the size, in python you cannot rely on int internal representation). In that case try this: C1 = C0[1:]+"0". But your question is really unclear. Commented Oct 29, 2016 at 7:20
  • To perform binary operations use integers or bytearrays (a list of integers effectivly). Using string operations is neither comfortable nor effective. Commented Oct 29, 2016 at 7:26

1 Answer 1

2

Try not using decimal or string encoded binary; it's much more efficient and consistent to use numbers as is, and Python supports converting them to or from other bases.

>>> C0=0b1111000011001100101010101111
>>> bin(C0<<1)
'0b11110000110011001010101011110'

You'll want to restrict your size at some point though, as Python has support for arbitrarily large integers (called long in Python 2, which is where the L comes from in your output, or simply int in Python 3). This is commonly done by bit masking. You can also do formatting with specific width if you want to present results in a particular base.

>>> bin(C0 & ((1<<16)-1))  # truncate to 16 bits
'0b1100101010101111'
>>> '{:032b}'.format(C0)   # format as 32 digit binary string
'00001111000011001100101010101111'

Particularly impractical is that your example output mixes string and numeric forms of data.

Here's an example of how to parse binary from a string, in case your program receives that as input, and a more direct way to do formatting.

>>> D = int('0101010101100110011110001111', 2)  # parse as binary = base 2
>>> D
89548687
>>> format(D, '032b')
'00000101010101100110011110001111'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.